2014-11-25 114 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

相关问题