Reputation: 1
I have created a JTable in NetBeans IDE. I want to add a new row to the table when the user gets to the lower right-hand corner of the table and presses the TAB key. I have tried NetBeans IDE options of changing the property for keyPressed, keyReleased and keyTyped, but nothing happens. Here is what I tried.
private void tblInterestIncomeKeyPressed(java.awt.event.KeyEvent evt)
{
if (evt.getKeyCode() == KeyEvent.VK_TAB)
{
System.out.println("Released tab key");
model.addRow(new Object[]
{
""
});
System.out.println("Got to this point");
}
}
I am trying to teach myself. I have seen other suggestions on this website, but they don't pertain to NetBeans IDE GUI creation. Thanks for any help.
Upvotes: 0
Views: 853
Reputation: 324098
Swing components use Key Bindings
to bind a KeyStroke
to an Action
. The default Action
for the Tab
key is to move to the next cell. So you will need to create a custom Action
.
You can create the custom Action
by extending AbstractAction
and adding your logic to the actionPerformed() method. You would then need to replace the current key binding to map to your own Action. Check out Key Bindings for example code on how to replace the bindings.
Or you can check out Table Tabbing. This uses a wrapper class to help make the key bindings process easier so all you need to implement is the logic in the actionPerformed()
method.
This example shows how to replace the default Tab Action
with a custom Action
that will only tab to cells that are editable. You would need to customize this Action to add a new row to the TableModel
.
Upvotes: 1