What is the best way to listen for changes in JTable cell values and update database accordingly?

前端 未结 4 1637
攒了一身酷
攒了一身酷 2020-12-17 21:59

I\'m building and app with multiple JTables and I need to detect when cell value change occurs so I can update it in the database. I tried TableModelListe

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-17 22:03

    I'm agreeing with @mKorbel - unless all your input is checkboxes and dropdowns, you're going to want to wait until the cell editing is stopped (you don't want to commit to the database every time a letter is typed in a textbox).

    If the problem is that it's not committing after focus has gone to another component, add a FocusListener that stops editing the table when focus is lost on the table:

    Example:

    final JTable table = new JTable();
    table.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent e) {
            TableCellEditor tce = table.getCellEditor();
            if(tce != null)
                tce.stopCellEditing();
        }
    });
    

提交回复
热议问题