简体   繁体   中英

Updating database after cell selection (JTable)

I want to update the value of the cell I've selected in JTable , which should reflect the database ( HSQL ) as button's listener is called upon.

JButton button = new JButton("VIEW AND EDIT");
button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        int row = table.getSelectedRow();
        int column = table.getSelectedColumn();
        //(**********************)
        System.out.println(row + " : " + column);
        table.requestFocus();
    }
});

What piece of code should I replace with the * s?

PLEASE provide me with, either a sample code, or process to do the same.

My table has 4 columns with one PK .

If you want to update the table so it matches the current state of your DB, you should

  • retrieve the current state from your DB on a worker thread
  • create a new TableModel on the worker thread, and set it on the existing table on the Event Dispatch Thread OR
  • update the existing table model on the Event Dispatch Thread

If you want to edit the values in the table, and push that state to your DB

  • write/use a table editor
  • when your TableModel receives the update (in the setValueAt method), push that state to the database on a worker thread

Relevant links:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM