Search code examples
javaswingjcombobox

How can add an "All Items" field to a jcombobox


I am trying to make a Swing GUI in Netbeans. I create a jcombobox and I bind it (using a query component, a list component and a renderer) to an entity called 'Item', so that the combobox shows the names of the items that currently exist in the table "Item" and so far it works fine. However, I need to add an "All Items" field to the combobox. Does anybody have any hints on where I should start?


Solution

  • Try

        List<String> listItems = classDAO.findElement();
    
        DefaultComboBoxModel<String> comboModel = new DefaultComboBoxModel();
    
        for(String string : listItems)
        {
            comboModel.addElement(string);
        }
    
        comboModel.addElement("All items");
    
        JComboBox<String> comboBox = new JComboBox<>(comboModel);