How to set jcombo box to what i want

70 Views Asked by At

I am doing a home work for my class in java, i am using NetBeans. When the frame opened i want that my combo box load data that is exactly to my database column. Exp... On my 7 column on my data base there's a column name that's name Color, and on the list of column there's yellow. I want that my jCombobox load with Yellow and get's all the others color on the model. Here is my code

private void formWindowOpened(java.awt.event.WindowEvent evt) {
    txt_id.setText(user);
    SQLiteConnection DB = new SQLiteConnection ();
    String question = DB.getQuestionUser();
    DB.getUtilisateur(user);
    cbx_question.addItem(question);
}

It keep on add item on the list of my model but do not show what is on the database column. Hope that you will understand

1

There are 1 best solutions below

0
On

The easiest way to populate a JComboBox is to supply the data when you call the constructor.

There are three different constructors you can call (supplying your data structure):

JComboBox(ComboBoxModel aModel)
Creates a JComboBox that takes its items from an existing ComboBoxModel.
JComboBox(E[] items)
Creates a JComboBox that contains the elements in the specified array.
JComboBox(Vector items)
Creates a JComboBox that contains the elements in the specified Vector.

You need to check what return type the database query has and convert it into one of the three previous data structures.

Later, if you wish to use the same JComboBox object to display other data (same type as before) you can do so by calling setModel( ComboBoxModel<E> model )

Source: Oracle documentation page