Controlled editing of a row selection in JTable

前端 未结 2 939
没有蜡笔的小新
没有蜡笔的小新 2020-12-11 23:41

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 opportun

相关标签:
2条回答
  • 2020-12-11 23:52

    The behaviour you mention can be achieved by forcing your table to start editing again.

    First make sure you now yourRow and Column and that you add your own tablecelleditor that extands from the AbstractCellEditor then add this to your stopCellEditing method:

    EventQueue.invokeLater(new Runnable()
        {
    
          public void run()
          {
            yourTable.editCellAt( yourRow, yourColumn+1);
          }
        });
    
    0 讨论(0)
  • 2020-12-12 00:03

    The default renderer and editor is typically adequate for most data types, but you can define custom renderers and editors as needed.

    Addendum: I'm unfamiliar with the approach shown in your fragment. Instead, register a TableModelListener with your model, as shown below, and update the database with whatever granularity is warranted. See also How to Use Tables: Listening for Data Changes.

    Addendum: @kleopatra is correct about your TableCellEditor. One convenient way to notify listeners is to invoke the super implementation, as shown here. Note that the delegate invokes fireEditingStopped().

    /** @see https://stackoverflow.com/questions/9155596 */
    public class NewJavaGUI extends JPanel {
    
        private final JTable table;
    
        public NewJavaGUI() {
            String[] colNames = {"C1", "C2", "C3"};
            DefaultTableModel model = new DefaultTableModel(colNames, 0) {
    
                @Override
                public boolean isCellEditable(int row, int col) {
                    // return your actual criteria
                    return true;
                }
    
                @Override
                public Class getColumnClass(int col) {
                    // return your actual type tokens
                    return getValueAt(0, col).getClass();
                }
            };
            // Add data; note auto-boxing
            model.addRow(new Object[]{"A1", "A2", 42});
            model.addRow(new Object[]{"B1", "B2", 42d});
            model.addTableModelListener(new TableModelListener() {
    
                @Override
                public void tableChanged(TableModelEvent e) {
                    // DML as indicated
                }
            });
            table = new JTable(model);
            this.add(table);
        }
    
        private void display() {
            JFrame f = new JFrame("NewJavaGUI");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(this);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new NewJavaGUI().display();
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题