How to add items in a combobox dynamically in Java? [closed]

0

I have a project on netbeans that uses Java Swing for the interface. I have some data in MYSQL that would like to show them as soon as the window is created.

Finally, how can I dynamically place items in a JComboBox in Java?

    
asked by anonymous 01.12.2016 / 16:45

2 answers

4

As you have no more details, I'll give you the most generic answer possible.

Just use the method addItem

combobox.addItem("Item 1");
combobox.addItem("Item 2");
combobox.addItem("Item 3");
    
01.12.2016 / 17:08
-2

1) First you have to make the connection this can be done in Pure JDBC or JPA

JDBC Connection

JPA Connection

2) You create a DAO based on one of the example

CRUD JDBC

CRUD JPA

3) In Your JFrame class you will do the following

public class SeuFrame(){

    public SeuFrame(){
        carregarCombo();
    }

    public void carregarCombo(){

          SeuDAO dao = new SeuDAO();
          List<SuaClasse> list = dao.lista();

          for(SuaClasse item: list){
               seuCombobox.addItem(item);
          }

    }        

}
    
01.12.2016 / 18:11