Rahul D'Mello
Rahul D'Mello

Reputation: 100

How can i set the default selected value of a jcombobox which is inside a jtable?

model = new DefaultComboBoxModel<>();
    model.addElement("Absent");
    model.addElement("Present");
    model.addElement("On Leave");
    model.setSelectedItem("Absent");

    JComboBox cbox = new JComboBox(model);
    DefaultTableModel attModel = (DefaultTableModel)tableEmpAtt.getModel();
    TableColumn col = tableEmpAtt.getColumnModel().getColumn(3);
    col.setCellEditor(new DefaultCellEditor(cbox));

adding rows :

ResultSet rs = st.executeQuery("select Employee_ID,First_Name,Last_Name,Contact_No from Employee_Information");
        while(rs.next()){
            attModel.addRow(new Object[]{rs.getInt(1),rs.getString(2)+" "+rs.getString(3),rs.getString(4)});
        }

this is how it looks on load this is how it looks on load

this is how it looks when a cell under presence is clicked once this is how it looks when a cell under presence is clicked once

i want to set it so that when its loaded for the first time the column presence has a default value of absent

Upvotes: 1

Views: 1104

Answers (1)

camickr
camickr

Reputation: 324118

i want to set it so that when its loaded for the first time the column presence has a default value of absent

The combo box will only select the value that is in the TableModel.

So you need to add "Absent" to the table model when you add each row:

//attModel.addRow(new Object[]{rs.getInt(1),rs.getString(2)+" "+rs.getString(3),rs.getString(4)});

Vector<Object> row = new Vector<Object>();
row.addElement(rs.getInt(1));
row.addElement(rs.getString(2) + " " + rs.getString(3));
row.addElement(rs.getString(4));
row.addElement("Absent");
attModel.addRow( row );

Upvotes: 3

Related Questions