2014-11-25 165 views
0

我試圖將DocumentListener類添加到我的JComboBox下拉列表中,但收到錯誤信息框的右側。將文檔偵聽器添加到JCombobox

它讓我創建類,但是我在添加/製作類的JComboBox上出現錯誤。

錯誤消息: 異常在線程「主要」 java.lang.Error的:未解決問題彙編: 在類型容器的製造方法添加(成分)是不適用在test.TestGUI參數(CountyDocumentListener)。 (TestGUI.java:52) 在test.TestGUI.main(TestGUI.java:20)

的目標是製作和使用的DocumentListener添加自動完成所有的下拉菜單。 顯然,數組將會更大,因此我試圖讓自動完成發生。

我可以使用DocumentListener來做到這一點,或者有另一種方式嗎?如果是這樣,我需要一個單獨doculistener類的所有下拉菜單,或者我應該如何組織這些? (我不想使用像SwingX這樣的東西,我想自己做)。

public class TestGUI extends JFrame implements ActionListener { 

    private JPanel content; 


    String[] county = { " ", "Orange", "Placer", "Napa", "LA", "Kings" }; 
    String[] muni = { " ", "Anaheim", "Agoura Hills", "Auburn", "Avalon", "Calistoga" }; 
    String[] place = { " ", "Berkeley", "Calistoga", "El Toro", "Glendale", "Corcoran" }; 

    public static void main(String[] args) { 
     TestGUI frame = new TestGUI(); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    @SuppressWarnings("rawtypes") 
    public TestGUI() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLocationRelativeTo(null); 
     content = new JPanel(); 
     content.setLayout(new BorderLayout()); 
     setContentPane(content); 


     JPanel rightPanel = new JPanel(); 
     content.add(rightPanel, BorderLayout.EAST); 
     rightPanel.add(new TriGoButton()); 


     JPanel leftPanel = new JPanel(); 
     content.add(leftPanel, BorderLayout.WEST); 

     JPanel centerPanel = new JPanel(); 
     content.add(centerPanel, BorderLayout.CENTER); 
     centerPanel.setLayout(new GridLayout(3, 3, 0, 20)); 

     JLabel countyLbl = new JLabel("County"); 
     centerPanel.add(countyLbl); 

     JComboBox countyDropDown = new JComboBox(county); 
     centerPanel.add(countyDropDown); 
     countyDropDown.setEditable(true); 
     countyDropDown.add(new CountyDocumentListener()); // right here 

     JLabel muniLbl = new JLabel("Munipalicity"); 
     centerPanel.add(muniLbl); 

     JComboBox muniDropDown = new JComboBox(muni); 
     centerPanel.add(muniDropDown); 
     muniDropDown.setEditable(true); 

     JLabel placeLbl = new JLabel("City or place"); 
     placeLbl.setToolTipText("search"); 
     centerPanel.add(placeLbl); 

     JComboBox placeDropDown = new JComboBox(place); 
     centerPanel.add(placeDropDown); 
     placeDropDown.setEditable(true); 


     JPanel bottomPanel = new JPanel(); 
     content.add(bottomPanel, BorderLayout.SOUTH); 

     JPanel topPanel = new JPanel(); 
     content.add(topPanel, BorderLayout.NORTH); 

     JLabel headlineLbl = new JLabel("headline"); 
     topPanel.add(headlineLbl); 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     //////  
    } 
} 

public class CountyDocumentListener implements DocumentListener { 

    //public public CountyDocumentListener() { 
     // TODO Auto-generated constructor stub 
    //} 

    @Override 
    public void changedUpdate(DocumentEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void insertUpdate(DocumentEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void removeUpdate(DocumentEvent e) { 
     // TODO Auto-generated method stub 

    } 

} 
+2

countyDropDown.getEditor ..... addDocumentListener(XxxXxx) – mKorbel 2014-11-25 14:24:56

+1

'我們的目標是使用documentlistener創建並添加一個自動完成到所有的下拉菜單。'==不要這樣做,不要重新創建輪,你必須創建自己的(摘要)文檔,覆蓋所有事件並正確(保護視圖,插入符號,選擇),不僅僅是簡單的可編輯JCombobBox,很幸運 – mKorbel 2014-11-25 14:32:36

+0

好的。不知何故,我的頭腦聽起來很容易。 - 如果userInput startsWith A>在下拉列表中顯示Avalon,Auburn ...。按字母順序對數組進行排序,讓我們推薦輸入字段中的第一個字符串 – cashmeer 2014-11-25 14:39:06

回答

1

您應該將文件監聽器添加到組合框的文本字段,所以使用((JTextField)countyDropDown.getEditor().getEditorComponent()).getDocument().addDocumentListener(new CountyDocumentListener())

爲了幫助你實現你的目標看JComboBox AutoCompletion

或者使用Swingx自動完成Swingx Autocompletion

+0

好吧,我可能只需要使用Swingx .. – cashmeer 2014-11-25 15:12:16

相關問題