Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How can I avoid JTextArea from grabbing focus upon setting text

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I avoid JTextArea from grabbing focus upon setting text

    Hello,

    I have a view which contains a JTextArea and a JTree, and my JTextArea is updated to display information for the selected node in the tree upon every new selection.

    The problem i experience is that when the JTextArea is not visual to the user (e.g. when scroll bars are used) then the setText function cause to focus on the JTextArea, loosing the focus/view of the node in the tree.

    How can I use the setText option along with the option to ignore grabbing focus?

    Thanks!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How can I avoid JTextArea from grabbing focus upon setting text

    After calling setText, try calling requestFocus on your JTree.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I avoid JTextArea from grabbing focus upon setting text

    Thanks, but I am afraid that does not help :-(

    The problem is when the node and the jtextarea can not be displayed together on the view (i.e. when scroll bars are displayed since due to large amount of nodes in the tree, and the nodes and the jtextarea are located on opposite locations of the view therefore are not seen simultanousely on the screen) -
    If I can not force the jtextarea to avoid the request focus after setting the text - How can I return to the same selected node on the tree?
    I tried using:
    myTree.setSelectionPath(theSelectedPath);
    myTree.scrollPathToVisible(theSelectedPath);
    but it did not work.

    Thanks!

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: How can I avoid JTextArea from grabbing focus upon setting text

    To get better help sooner, post a SSCCE.
    SSCCE : Java Glossary

    db

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I avoid JTextArea from grabbing focus upon setting text

    This is partial of my code:

    JPanel myPanel = new JPanel();
    myPanel.setLayout( new GridBagLayout() );
    GridBagConstraints gbc= new GridBagConstraints();

    JTree myTree = new JTree();
    myTree.addMouseListener(this);
    myTree.getSelectionModel().setSelectionMode(TreeSe lectionModel.SINGLE_TREE_SELECTION);
    EMyObject basicRoot = new EMyObject();
    TreeModel treeModel = new DefaultTreeModel(new DefaultMutableTreeNode(basicRoot));

    myTree.setModel(treeModel);
    myTree.setCellRenderer(getTreeCellRenderer());
    myTree.addTreeSelectionListener(this);

    JScrollPane treeScrollPanel = new JScrollPane(myTree);
    myPanel.add(treeScrollPanel, gbc);
    gbc.gridy++;

    JTextArea myTextArea = new JTextArea();
    myPanel.add(myTextArea, gbc);

    Via this function I catch the change of the selection in the tree and update the JTextArea with the updated message, and then try to return the focus and slection to the selected node. The problem is that when scroll are displayed the selection and focus are done but the scroll do not move to display that node.

    public void valueChanged(TreeSelectionEvent e)
    {
    if (e.getSource() instanceof JTree)
    {
    String myText = "";
    if (e.getNewLeadSelectionPath() != null &&
    e.getNewLeadSelectionPath().getLastPathComponent() != null)
    {
    myText = e.getNewLeadSelectionPath().getLastPathComponent() .toString();
    }
    myTextArea.setText(myText);
    myTree.scrollPathToVisible(e.getNewLeadSelectionPa th());
    }
    }

    Thanks a lot!

Similar Threads

  1. Grabbing info from Java game
    By n0bekre in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2010, 10:32 AM
  2. [SOLVED] JTextPane focus problem
    By LeonLanford in forum AWT / Java Swing
    Replies: 3
    Last Post: June 21st, 2010, 11:50 PM
  3. How to avoid the Deadlock in the below program
    By murali1253 in forum Threads
    Replies: 0
    Last Post: April 15th, 2010, 05:35 PM
  4. How can I append text in JTextArea from another class
    By chikaman in forum AWT / Java Swing
    Replies: 2
    Last Post: December 10th, 2009, 10:26 AM
  5. switch focus to another window
    By tuansoibk in forum AWT / Java Swing
    Replies: 1
    Last Post: November 13th, 2009, 02:02 PM