简体   繁体   中英

Adding a row to a jTable on downarrow at bottom

I have a jTable which currently displays and allows editing of a database table, I am now trying to sort adding tuples.

I am trying to get it to automatically add a row on downarrow at the bottom. So if I am at the bottom on the table and click my down arrow a new row will appear below. I just can't figure out how to do it.

Thanks James

Action handling of JTable happens in javax.swing.plaf.basic.BasicTableUI . In your case, you probably need to register a new action for SCROLL_DOWN_CHANGE_SELECTION . In the action, check whether the current selection == last row of the table.

If that doesn't work, set a breakpoint in javax.swing.plaf.basic.BasicTableUI.Actions.actionPerformed(ActionEvent) to see which action is really executed.

JTable has a default Action for the down arrow key. If you want to change this behaviour then you need to create a custom Action. You can do this easily by using the Wrapping Actions concept to leverage the default code.

You can also look at Table Tabbing for a working example of wrapping an Action. You code for the Action would be much simpler and would be something like:

if (last row is selected)
    add a new row to the table

invoke the default down arrow action

You'll need to create a KeyListener and add this to your table:

public void keyReleased(KeyEvent e) {
  int keyCode = e.getKeyCode();
  if (keyCode == KeyEvent.VK_DOWN)
        // check if selected table row = last row and if so: add new row to table model
}

greetz,
Stijn

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