Two Laptop Bag*
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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "about Textfield( simple isn Watch "about Textfield( simple isn New topic
Author

about Textfield( simple isn't that)

Surya Prabhakar
Greenhorn

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;
if(ae.getSource().equals(no))
{
try{
val = Integer.parseInt(no.getText());
}
catch(NumberFormatException ne){--codes to show error msg--}
}
}
}
-----
-----
}

RoshiniSridharan
Surya Prabhakar
Greenhorn

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 allowed.so I hope u understand the problem.
thx again
bye
Cindy Glass
"The Hood"
Sheriff

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: http://java.sun.com/docs/books/tutorial/uiswing/events/example-swing/KeyEventDemo.java


"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


regards
deeksha
Suneel Setlur
Ranch Hand

Joined: Nov 26, 2000
Posts: 202
Hey Surya
You can check the solution posted here.
http://www.javaranch.com/ubb/Forum23/HTML/000437.html
Suneel


Suneel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about Textfield( simple isn't that)
 
Similar Threads
Copy and paste a value from a text field
How to goto a particular row /column in JTable
How to place the cursor at the end of a textfield
set Focus in Border Layout
Why TextField added to Panel on Window not editable?