Adding a DocumentListener to a JCombobox

2.6k views Asked by At

I'm trying to add a DocumentListener class to my JComboBox dropdowns but I'm getting an error message right of the box.

It let me create the class but I'm getting an error on the JComboBox where I'm adding/making the class.

Error message: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method add(Component) in the type Container is not applicable for the arguments (CountyDocumentListener) at test.TestGUI.(TestGUI.java:52) at test.TestGUI.main(TestGUI.java:20)

The goal is to make and add an autocomplete to all the dropdowns using documentlistener. Obviously the arrays will be alot bigger hence im trying to make autocomplete happen.

Can I do this with DocumentListener or is there another way? If so do I need a seperate doculistener class for all the dropdowns or how should I organize all this? (I don't want to use anything like SwingX, I want to make it myself).

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

    }

}
1

There are 1 answers

0
Naruto Biju Mode On BEST ANSWER

You should add your document listener to the textfield of the combobox, so use ((JTextField)countyDropDown.getEditor().getEditorComponent()).getDocument().addDocumentListener(new CountyDocumentListener())

To help you achieving your goal see JComboBox AutoCompletion

Or use Swingx autocompletion Swingx Autocompletion