This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes about Textfield( simple isn't that) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "about Textfield( simple isn Watch "about Textfield( simple isn New topic

about Textfield( simple isn't that)

Surya Prabhakar

Joined: Dec 21, 2000
Posts: 7
Hello friends!!!..,
I hope u can answer this.
1) In a text field the default alignment of the cursor is right side.Can we align it to the left side?
2) Unlike that can we make cursor invisible in the textfield but still the text can be entered from left?
3)can we restrict the textfield to accept only numbers?

Please answer me these q? I need to work on a project
thank u

I am a novice user.I hope people out there will help me.
Roshini Sridharan
Ranch Hand

Joined: Jan 16, 2001
Posts: 143
Try this coding to allow no's in the textfield.
class test
public void add()
TextField no = new TextField(20);
no.addFocusListener(new listener());
class listener extends FocusAdapter
public void focusLost(FocusEvent fe)
int val = 0;
val = Integer.parseInt(no.getText());
catch(NumberFormatException ne){--codes to show error msg--}

Surya Prabhakar

Joined: Dec 21, 2000
Posts: 7
Thank you Mr/Miss RoshiniSridharan for ur enthusiastic reply.But I think I should explain u the concept in detail actually I mean to say that ,consider the textfield as a display screen for calculator,in which text should not at all come,take example of a windows calculator in which all text keys are disabled so that no text can be entered.Your code is good enough but only verified after entering something into the textfield,which should not be I hope u understand the problem.
thx again
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
If you attach a KeyListener to the textField and have it check for keyPressed (this is before keyReleased happens) then you should be able to "catch" the char, check it for numeric and if it is alpha substitute a null.

Here is a whole tutorial on keyEvents:

"JavaRanch, where the deer and the Certified play" - David O'Meara
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
hi Surya,
If u r talking about JTextField then i'd like suggest yet another way to do so.i.e. use PlainDocument class.
here is the example code

Suneel Setlur
Ranch Hand

Joined: Nov 26, 2000
Posts: 202
Hey Surya
You can check the solution posted here.

jQuery in Action, 2nd edition
subject: about Textfield( simple isn't that)