Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

selectAll() in a JTextField

 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am invoking the method selectAl() on the object of JTextField class on a click of a button, but it is not selecting the text contained in the text field. Is it a known issue with JDK 1.4?
Is there sth else that I am missing?
- Manish
 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
What I understood from the question is, that you have misspelt the method.it is not "selectAl()". it should be double "l". selectAll() is the correct method signature. i guess it is the correct answer. hope this will help you.
I am also a beginer in java so please don't mind if the answer is incorrect.
cheers
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rasika,
The typo was in the post, not in the actual code. In the code it is indeed selectAll() (it wouldn't have compiled without it!)
- Manish
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still can't get this to work!
Now that the authors are here, I thought I might ask it again and try my luck.
TIA,
- Manish
 
Matt Robinson
author
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not aware of any issues with this in JDK1.4 but here are a couple things you can try:
1. After calling selectAll(), call revalidate() and repaint() on your JTextField component. Reasoning is it may actually be selected but may not be rendered properly.
2. If that doesn't work try using a different method, for example:
yourTextField.select(0,length-1)

If neither of these work do the following:
- after calling selectAll(), print getSelectionStart() and getSelectionEnd() and post the results here. In this case it would help to see your code
 
M.K.A. Monster
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I'm not the author you are looking for. But have you tried.

I think this will do exactly the same thing as the "selectAll()" method.
Regards,
Mark Monster
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Matt Robinson:
I'm not aware of any issues with this in JDK1.4 but here are a couple things you can try:
1. After calling selectAll(), call revalidate() and repaint() on your JTextField component. Reasoning is it may actually be selected but may not be rendered properly.
2. If that doesn't work try using a different method, for example:
yourTextField.select(0,length-1)

If neither of these work do the following:
- after calling selectAll(), print getSelectionStart() and getSelectionEnd() and post the results here. In this case it would help to see your code


Well,
Tried all of the above, but somehow it can't show selection as "selected" on my machine. Here are the results of getSelectionStart() and getSelectionEnd(), they show tect as properly selected.
0 3
0 5 etc
The code is -

When I used TextField instead of JTextField, it worked!!!
HELP!!!
- Manish
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... selectAll() is working for me with a JTextField in JDK 1.4.1_01 on Windows 2000...

What platform, JDK version, and L&F are you using?
 
Matt Robinson
author
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output you show above shows that your text is actually selected, so the problem is your text highlighting isn't being rendered properly.
As the other person said, looks like an L&F issue. Are you on Windows? If so are you using the "Standard Windows" Appearance? (right click your desktop and choose the "Appearance" tab - if "Standard Windows" isn't selected, select it and try your app again. Sometimes Swing gets confused when you have a different appearance set although I thought they had resolved this in 1.2.something
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also using JDK 1.4.1_01 on Windows 2000. The L&F is default (Metal theme), however I am using -
JFrame.setDefaultLookAndFeelDecorated(true);
Looks like this is a focus issue as few bugs in the bug parade showed.
- Manish
 
Lou Pelagalli
Ranch Hand
Posts: 150
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just posted this question myself, then five minutes later came up with the answer.
jTextField.requestFocus();
jTextField.selectAll(); or jTextField.select(int, int);
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying something very similar, and it finally worked to reset the text of the text box before selecting:


I don't know why that worked. I'm doing this from within a the focusGained method of a FocusListener.
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!