Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

transferring focus in a JTabbedPane with JTables

807591Feb 28 2008 — edited Mar 1 2008
Hello,

I am having trouble with moving the focus in a JTabbedPane that contains a JTable under each tab. The tabbed pane has custom tab components that each have a JTextField (normally uneditable) that displays the tab title, and a close button. Besides its normal tabs, the tabbed pane has a special "new tab" tab that also has a custom tab component: a JPanel with a button in it (mostly just for the rollover effect). When this tab is selected or when its button is pressed, the tab's tab component is changed and a table is added under it -- basically the "new tab" tab is converted to a normal tab -- and a "new tab" tab is added to the tabbed pane. After the new tab is inserted, it is set as the selected tab in the tabbed pane (in case the button was pressed instead of the space around it) and focus is transferred to the now-editable JTextField (with requestFocusInWindow()) in the new tab's custom tab component, and the user should enter a title for the new tab. Title editing ends when the user presses the Enter key or when the JTextField loses focus (such as when another component in the application is clicked).

This works as intended when just a tab is selected in the tabbed pane. However, if I click on the "new tab" tab (or its button) while a cell is selected in the JTable under the currently selected tab, the JTextField of the resulting normal tab immediately loses focus and ends title editing. I tried clearing the table selection before moving the focus to the JTextField, and I tried using KeyBoardFocusManager's clearGlobalFocusOwner() method, but neither of these worked.

What is surprising is that this process works as intended when a cell is being edited but not when it is merely selected IF the "new tab" tab's button is pressed. (If the "new tab" tab is clicked outside of the button, the JTextField of the normal tab that is created immediately loses focus.) Before the new normal tab is created and added, the program checks all the tables for cell editing and stops it. (Checking all the tables is necessary because I haven't found out a way to find the tab that was selected before the "new tab" tab.) For some reason the JTextField of the new normal tab's tab component doesn't lose focus immediately if cell editing was stopped. And I have tried using JTable's editCellAt(int, int) method to set the selected cell editing and then stop editing immediately after that, but that didn't work.

Also, if I remove this line of code:
tabbedPane.setSelectedIndex(newTabIndex);
the new tab's title text field does not lose focus immediately even if a cell in the table is selected IF the "new tab" tab's button is pressed. But then the new tab does not get selected, which is a problem.

So I guess the main problem is that the title text field of the new normal tab loses focus immediately if that tab is selected, either by the user selecting the "new tab" tab without clicking its button or with JTabbedPane's setSelectedIndex(int) method. Maybe it's because the tab's contents are given focus when the tab is selected? But I do want the new normal tab to be selected as well as its title text field to retain focus. Any help would greatly appreciated. And sorry about the extremely long and confusing explanation :P Maybe I could link to some screenshots if you don't understand it.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 29 2008
Added on Feb 28 2008
4 comments
1,455 views