Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By doWhile

Thread: JTextField & selectAll() failure

  1. #1
    Tony_Harrison is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default JTextField & selectAll() failure

    Hi all:

    The following is code illustrating an issue I can't quite figure out in my code. I want to highlight (select all) text in a JTextField when the program starts up. When I add textField to the JFrame after toolBar selectAll() fails. However, when I add textField before toolBar, no problem. Could somebody explain why this is and a possible alternative.

    If I switch lines 24 and 25 in this example, the text will not select. As it is now, it works fine.

    Java Code:
    import java.awt.Dimension;
    import javax.swing.*;
    
    public class MyProblem {
    
        static void createAndShowGUI() {
    
            JFrame mainFrame = new JFrame("JTextField_SSCCE01");
            JTextField textField = new JTextField("My Text Here!");
            JPanel toolBar = new JPanel();
    
            JLabel label = new JLabel("Label");
            JCheckBox checkBox = new JCheckBox();
            JButton button = new JButton("Button");
    
            toolBar.setLayout(new BoxLayout(toolBar, BoxLayout.X_AXIS));
            toolBar.add(Box.createRigidArea(new Dimension(75, 0)));
            toolBar.add(button);
            toolBar.add(checkBox);
            toolBar.add(label);
    
            mainFrame.setLayout(new BoxLayout(mainFrame.getContentPane(),
                    BoxLayout.Y_AXIS));
            mainFrame.add(textField);
            mainFrame.add(toolBar);
            
            
            mainFrame.setSize(300, 100);
            textField.selectAll();
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainFrame.setVisible(true);
        }
    
        public static void main(String args[]) {
            createAndShowGUI();
        }
    }
    Last edited by Tony_Harrison; 04-21-2012 at 10:38 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: JTextField & selectAll() failure

    I think that key would be to call selectAll() after the JTextField has been rendered -- after the setVisible(true) or pack() has been called on the top level window that holds it.

    Edit: Ignore what I've said above. Your problem is one of misunderstanding about selected text. Your JTextField is in fact selecting all regardless of which line is first. The key to understanding what is going on is that with one trial (the one you think doesn't work), the JTextField doesn't have *focus*. It's text is fully selected which you will discover if you tab over to it so that it gains focus.
    Last edited by Fubarable; 04-21-2012 at 10:47 PM.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: JTextField & selectAll() failure

    The text is selected (use some print lines to print the selection), but not shown because the component does not have focus. Call request focus after the parent is validated (either call validate, or after the setVisible is called)
    Fubarable likes this.

  4. #4
    Tony_Harrison is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default Re: JTextField & selectAll() failure

    Placing .selectAll() after .setVisible(true) did not work for me.

    However, calling validate after select all and then request focus worked like a charm.

    Thanks for the quick response!

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: JTextField & selectAll() failure

    BTW, your call to createAndShowGUI() should be wrapped in a call to SwingUtilities.invokeLater(). That ensures that the GUI is created in the Swing thread instead of in the main thread.
    Get in the habit of using standard Java naming conventions!

  6. #6
    Tony_Harrison is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default Re: JTextField & selectAll() failure

    Good advice! I'm still at a bit of a loss when it comes to using threading and concurrency. Swing and AWT is so user friendly, it is easy to lose sight of all the stuff going on under the hood :)

Similar Threads

  1. Replies: 6
    Last Post: 02-25-2012, 04:35 AM
  2. selectAll() method in a JFormattedTextField
    By jiffi in forum New To Java
    Replies: 4
    Last Post: 12-16-2011, 06:09 AM
  3. Replies: 3
    Last Post: 11-14-2011, 05:13 AM
  4. IF ELSE program failure.
    By Domenatrice in forum New To Java
    Replies: 4
    Last Post: 11-12-2011, 07:48 AM
  5. Replies: 1
    Last Post: 01-30-2009, 06:44 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •