i have a JTable and I'm adding three JComboBoxes to three different columns. Now i want to set the selected item for every row i have. The problem is, i need the ID of every row to do that. So i tried different Listeners and the best result came with a FocusListener, but then i always have to click at the row first and then at the JComboBox and this is laborious. Here is an example:
JTable table = new JTable();
Vector<ArrayList<Object>> data = new Vector<ArrayList<Object>>();
for (int i = 0; i < 5; i++)
{
ArrayList<Object> object = new ArrayList<Object>();
object.add(i);
object.add("name");
object.add(i+1);
object.add(i+1);
object.add(i+1);
data.add(object);
}
DefaultTableModel tableModel = new DefaultTableModel() {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int row, int column) {
if (column < 2)
return false;
return true;
}
};
tableModel.setColumnIdentifiers(new String[] {"ID", "Name", "OK", "Other", "Error"});
tableModel.addTableModelListener(new TableModelListener()
{
@Override
public void tableChanged(TableModelEvent e)
{
if (e.getType() == TableModelEvent.UPDATE)
{
int row = e.getFirstRow();
int column = e.getColumn();
TableModel table_model = (TableModel) e.getSource();
ArrayList<Object> changed_data = (ArrayList<Object>) table_model.getValueAt(row, column);
String row_id = String.valueOf(table_model.getValueAt(row, 0));
for (ArrayList<Object> list : data)
{
String compare_id = String.valueOf(list.get(0));
if (row_id.equals(compare_id))
{
list.set(column, String.valueOf(changed_data.get(0)));
for (int i = table_model.getRowCount()-1; i >= 0 ; i--)
{
tableModel.removeRow(i);
}
for (ArrayList<Object> object : data)
{
Vector<String> vector = new Vector<String>();
vector.addElement(String.valueOf(object.get(0)));
vector.addElement(String.valueOf(object.get(1)));
vector.addElement(String.valueOf(object.get(2)));
vector.addElement(String.valueOf(object.get(3)));
vector.addElement(String.valueOf(object.get(4)));
tableModel.addRow(vector);
}
TableColumn column_ok = table.getColumnModel().getColumn(2);
TableColumn column_other = table.getColumnModel().getColumn(3);
TableColumn column_error = table.getColumnModel().getColumn(4);
JComboBox<ArrayList<Object>> combobox_ok = new JComboBox<ArrayList<Object>>(data);
JComboBox<ArrayList<Object>> combobox_other = new JComboBox<ArrayList<Object>>(data);
JComboBox<ArrayList<Object>> combobox_error = new JComboBox<ArrayList<Object>>(data);
column_ok.setCellEditor(new DefaultCellEditor(combobox_ok));
column_other.setCellEditor(new DefaultCellEditor(combobox_other));
column_error.setCellEditor(new DefaultCellEditor(combobox_error));
break;
}
}
}
}
});
table.setModel(tableModel);
for (ArrayList<Object> object : data)
{
Vector<String> vector = new Vector<String>();
vector.addElement(String.valueOf(object.get(0)));
vector.addElement(String.valueOf(object.get(1)));
vector.addElement(String.valueOf(object.get(2)));
vector.addElement(String.valueOf(object.get(3)));
vector.addElement(String.valueOf(object.get(4)));
tableModel.addRow(vector);
}
TableColumn column_ok = table.getColumnModel().getColumn(2);
TableColumn column_other = table.getColumnModel().getColumn(3);
TableColumn column_error = table.getColumnModel().getColumn(4);
JComboBox<ArrayList<Object>> combobox_ok = new JComboBox<ArrayList<Object>>(data);
JComboBox<ArrayList<Object>> combobox_other = new JComboBox<ArrayList<Object>>(data);
JComboBox<ArrayList<Object>> combobox_error = new JComboBox<ArrayList<Object>>(data);
column_ok.setCellEditor(new DefaultCellEditor(combobox_ok));
column_other.setCellEditor(new DefaultCellEditor(combobox_other));
column_error.setCellEditor(new DefaultCellEditor(combobox_error));
JScrollPane scrollPane = new JScrollPane();
scrollPane.getViewport().add(table);
JFrame frame = new JFrame();
frame.add(scrollPane);
frame.setSize(400, 200);
frame.setVisible(true);
Now, in the first row, third column ("OK"), you can choose in the JComboBox different entries which represent a row. So a row has three JComboBoxes which reference to another row. If you click in such a JComboBox, you will notice it always chooses the first entry and not the entry with the number you saw before clicking at it.
Maybe now you understand what i want to do?