首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将DocumentListener添加到JCombobox

将DocumentListener添加到JCombobox
EN

Stack Overflow用户
提问于 2014-11-25 14:21:40
回答 1查看 2.4K关注 0票数 1

我试图在我的DocumentListener下拉列表中添加一个JComboBox类,但是我得到了一个正确的错误消息。

它允许我创建类,但是在添加/生成类的JComboBox上出现了一个错误。

错误消息:线程“主”java.lang.Error中的异常:未解决的编译问题:类型容器中的方法add(组件)不适用于test.TestGUI.(TestGUI.java:52) at test.TestGUI.main(TestGUI.java:20)的参数(TestGUI.java:52)

目标是使用documentlistener创建并添加一个自动完成到所有下拉列表中。显然,数组会更大,因此我试图实现自动完成。

我可以用DocumentListener来做这件事吗?或者还有别的方法吗?如果是这样的话,我是否需要一个独立的doculistener类来处理所有的下拉列表,或者我应该如何组织所有这些?(我不想使用像SwingX这样的东西,我想自己制作)。

代码语言:javascript
复制
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) {
        //////      
    }
}

代码语言:javascript
复制
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

    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-25 14:36:49

应该将文档侦听器添加到组合框的textfield,因此使用((JTextField)countyDropDown.getEditor().getEditorComponent()).getDocument().addDocumentListener(new CountyDocumentListener())

要帮助您实现目标,请参阅JComboBox AutoCompletion

或者使用Swingx自动完成Swingx自动完成

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27129155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档