I have a JTable displaying rows from an SQL database. The table is relatively small (only 4 columns and up to 1000 rows).
I would like to give the user the ability to edit any cells in the table, but you should not limit it so much that they use the edit dialog box (this simplifies error checking and checking, but is less intuitive)
I tried several different ways of managing choices using the valueChanged method of my JTable, but didn't have much luck.
I want each row to be edited and written to the database when editing is complete. I would like that after a cell has been pressed to start editing this row, no other rows can be selected until the user has finished editing the row (other rows will be grayed out). After editing each cell and pressing enter, the selection of editing should go to the next column in the same row.
Can someone give pointers on how I can achieve this?
// Create table with database data table = new JTable(new DefaultTableModel(data, columnNames)) { public Class getColumnClass(int column) { for (int row = 0; row < getRowCount(); row++) { Object o = getValueAt(row, column); if (o != null){ return o.getClass(); } } return Object.class; } @Override public boolean isCellEditable(int row, int col){ return true; } @Override public boolean editCellAt(int row, int column) { boolean ans = super.editCellAt(row, column); if (ans) { Component editor = table.getEditorComponent(); editor.requestFocusInWindow(); } return ans; } @Override public void valueChanged(ListSelectionEvent source) { super.valueChanged(source); if (table!=null) table.changeSelection(getSelectedRow(), getSelectedColumn()+1, false, false); } };
Edit - custom cell editor with table pointer seems empty
public class ExchangeTableCellEditor extends AbstractCellEditor implements TableCellEditor { private JTable table; JComponent component = new JTextField(); public ExchangeTableCellEditor(JTable table) { this.table = table; } public boolean stopCellEditing() { boolean ans = super.stopCellEditing();
}
source share