• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about Textfield( simple isn't that)

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Surya
You can check the solution posted here.
http://www.javaranch.com/ubb/Forum23/HTML/000437.html
Suneel
 
Oh the stink of it! Smell my tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic