Arun B R
Arun B R

Reputation: 21

Java swing table focus change

I want to change the focus of java swing table first cell to next when enter key pressed.

Can we change the focus of first cell to a specific cell when an event occurred.I tried setfocus but failed.Any help please

My table has three columns and one row as default. When the user enter values into the (0,0) cell, and then presses enter, it should move the focus and editor to next cell(i e, 0,1).

When reached 3rd column, after types values to the column and pressed enter key, it should add a new row to the table.And move the focus and editor to first cell of created row. It should repeat the same procedure along.

Upvotes: -1

Views: 164

Answers (1)

camickr
camickr

Reputation: 324167

I want to change the focus of java swing table first cell to next when enter key pressed

This is the default behaviour for the Tab key.

You can share this Action with the Enter key by changing the Key Bindings of the table by using:

KeyStroke tab = KeyStroke.getKeyStroke("TAB");
KeyStroke enter = KeyStroke.getKeyStroke("ENTER");
InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(enter, im.get(tab));

Read the section from the Swing tutorial on How to Use Key Bindings for more information.

Upvotes: 0

Related Questions