Rima Touya Rima Touya - 6 months ago 102
SQL Question

Get value from Jtable and set it to JcomboxBox in Netbeans

I want to ask if there's a way to get the value from Jtable and set it to JcomboBox.

I insert data into database using jcomboboxes, so i want to get the value of what i inserted to database.

Im using netbeans & mysql

Answer

Yes you can do it.

One Way

You need to get data from JTable using getValueAt(row,column) and them you can add that value to JComboBox using addItem(value) like:

comboBox.addItem(table.getValueAt(row,column).toString());

Second Way

As you are using mysql resultset to get fill the jtable, you can use that resultset to set the combobox value too like:

You can use a loadcombo() to load your combobox with database values.

void loadcombo() {
    try
    {
    // Your database connections 

rs= st.executeQuery("select Column from Table");
    while(rs.next()){                            
        jComboBox.addItem(rs.getString(1));
    }
    con.close();
    }
    catch(Exception e)
    {
        System.out.println("Error"+e);
    }    
}

I suggest you to use second way