Results 1 to 6 of 6
Thread: JTextField & selectAll() failure
- 04-21-2012, 10:31 PM #1
Member
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
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.
-
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.
- 04-21-2012, 10:46 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
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)
- 04-22-2012, 12:21 AM #4
Member
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
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!
- 04-22-2012, 12:31 AM #5
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!
- 04-22-2012, 12:46 AM #6
Member
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
New user needing help adding FocusListener event to selectAll in JTextField
By leroycorbid in forum AWT / SwingReplies: 6Last Post: 02-25-2012, 04:35 AM -
selectAll() method in a JFormattedTextField
By jiffi in forum New To JavaReplies: 4Last Post: 12-16-2011, 06:09 AM -
Press enter on JTextField to make focus on next JTextField?
By userno69 in forum AWT / SwingReplies: 3Last Post: 11-14-2011, 05:13 AM -
IF ELSE program failure.
By Domenatrice in forum New To JavaReplies: 4Last Post: 11-12-2011, 07:48 AM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks