Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3980030
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:14:32+00:00 2026-05-20T05:14:32+00:00

try: F2 start editing and ENTER stop editing and the focus switches to the

  • 0

try: F2 start editing and ENTER stop editing and the focus switches to the next Component and goes back to the Tree-Node in milliseconds. In the example you can see the focusPainted rectangle flashing in the JTabbedPane-Header. With a FocusListener it shall be clearer.

Why I lose the focus for a short Time from JTree after editing a Node?

How to prevent this behavior?

public class Focus {

private static void createAndShowGUI() {
    final JTextArea text = new JTextArea("Tab Header gained focus");

    JTree tree = new JTree();
    tree.setEditable( true );
    int row = 0;
    while( row < tree.getRowCount() ) {
        tree.expandRow( row++ );
    }

    JTabbedPane tabp = new JTabbedPane();
    tabp.addTab( "Lorem", text );
    tabp.addFocusListener( new FocusListener() {
        @Override
        public void focusLost( FocusEvent e ) { }
        @Override
        public void focusGained( FocusEvent e ) {
            text.setText( text.getText() +"\nWoohoo, I got the focus!" );
        }
    });

    JFrame frame = new JFrame( "Focus" );
    frame.setLayout( new BorderLayout() );
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(tree, BorderLayout.WEST);
    frame.getContentPane().add(tabp, BorderLayout.CENTER);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    tree.startEditingAtPath( tree.getPathForRow( 0 ) );

}
public static void main(String[] x) {
    EventQueue.invokeLater(new Runnable() {
        @Override public void run() {
            createAndShowGUI();
        }
    });
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:14:33+00:00Added an answer on May 20, 2026 at 5:14 am

    The tree adds a DefaultCellEditor to the JTree hierarchy when editing starts. This textfield gains the focus. When editing stopped (Enter pressed), the BasicTreeUI.completeEditing() method checks whether the tree itself or the editor component is the focus owner. In that case, the tree will be focused again after the editing is finished.

    In the stop algorithm, the editor is removed from the tree. It was the focus owner before, so the focus will be transfered to the next focusable component (focus cycle). In your UI this will be the tabbed pane.

    Due to the fact that the editor was focused before, the BasicTreeUI requests the focus for the tree.

    These steps map perfectly on your described behaviour.

    The only solution (not complete, but shows the direction) is to set your own FocusTraversalPolicy:

    private static class TreeEditorFocusTraversalPolicy extends DefaultFocusTraversalPolicy {
        private final JTree tree;
        public TreeEditorFocusTraversalPolicy(JTree tree) {
            this.tree = tree;
        }
    
        @Override
        public Component getComponentAfter(Container aContainer, Component aComponent) {
            if (aComponent instanceof CellEditor) {
                return tree;
            }
            return super.getComponentAfter(aContainer, aComponent); 
        }
    }
    

    Set this instance to your tree:

    tree.setFocusTraversalPolicy(new TreeEditorFocusTraversalPolicy(tree));
    tree.setFocusCycleRoot(true);
    

    The problem so far: the focus traversal (Tab, Shift+Tab) does not work anymore. The FocusTraversalPolicy is a huge part of Swing and needs some time to create a working policy. Maybe have a look at the LegacyGlueFocusTraversalPolicy which is the default policy.

    Hope this helps to get further.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to start a new fragment from my fragmentActivity, but every time I
I try to start action with service (or activity) from an url. (with params)
when i try to start the rails server this error ever appears C:\Arthur\Ruby\Aplicações em
When I try to start my MYSQL server on XAMPP and a Mac computer.
When I try to start an IntentService over adb i get tho following error
getting error while try to start service
When I try to start the Sql Server (SqlExpress) service I got the following
When I try to start Firefox using Process.Start and ProcessStartInfo (.NET) everything seems to
When i try to start Thread (u) it does nothing! this is what i
When I try to start up a websphere portal server, it hangs at the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.