Sofia
Sofia

Reputation: 13

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?

Upvotes: 1

Views: 1839

Answers (2)

camickr
camickr

Reputation: 324108

You can manually add an item to the combo box after the items from the table have been added to the combo box:

comboBox.addItemAt("All Items", 0);

will insert a new item at the top of the combo box.

Upvotes: 1

gerry
gerry

Reputation: 36

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);

Upvotes: 1

Related Questions