10

複数のアプリをJTables作成していて、データベースで更新できるように、セル値の変更がいつ発生するかを検出する必要があります。TableModelListenerオーバーライドしようとしましたが、セルを編集した後でクリックして離れた(別の行をクリックした)場合にのみtableChanged起動します。

これを行う他の方法はありますか?

4

4 に答える 4

21

この例CellEditorListenerに示すように、インターフェースを実装できます。それ自体がであることに注意してください。JTableCellEditorListener

次に示すように、フォーカスが失われたときに編集を終了すると便利な場合もあります

table.putClientProperty("terminateEditOnFocusLost", true);

その他のSwingクライアントプロパティはここにあります。

于 2012-10-10T19:20:57.973 に答える
5

@mKorbelに同意します-すべての入力がチェックボックスとドロップダウンでない限り、セルの編集が停止するまで待つ必要があります(文字が入力されるたびにデータベースにコミットする必要はありませんテキストボックス)。

別のコンポーネントにフォーカスが移動した後、コミットされないことが問題である場合はFocusListener、テーブルでフォーカスが失われたときにテーブルの編集を停止するを追加します。

例:

final JTable table = new JTable();
table.addFocusListener(new FocusAdapter() {
    @Override
    public void focusLost(FocusEvent e) {
        TableCellEditor tce = table.getCellEditor();
        if(tce != null)
            tce.stopCellEditing();
    }
});
于 2012-10-10T19:19:47.467 に答える
0

私はEnterキーを使用しているので、ユーザーがEnterキーを押すたびに、セルが更新されます。

    DefaultTableModel dtm = new DefaultTableModel(data, columnNames);
    JTable table = new JTable(dtm);

    table.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {

                int row = table.getSelectedRow();
                int column = table.getSelectedColumn();

                // resul is the new value to insert in the DB
                String resul = table.getValueAt(row, column).toString();
                // id is the primary key of my DB
                String id = table.getValueAt(row, 0).toString();

                // update is my method to update. Update needs the id for
                // the where clausule. resul is the value that will receive
                // the cell and you need column to tell what to update.
                update(id, resul, column);

            }
        }
    });
于 2013-11-21T08:43:11.040 に答える
0

これは、選択変更または保存ボタンからイベントハンドラーの編集を停止する場合にも便利です。

if (table.isEditing())
    table.getCellEditor().stopCellEditing();
于 2015-08-18T09:15:34.320 に答える