• 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

JTextField on getText() method

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made some method to validate the JTextField to get only numbers. It is working already. My current problem is how to get the data in the text field. I have tried adding actionListener to get the text from the component when the user triggers enter and prints the value to the screen but won't work. Seems something is wrong with the code. Please help.
My source code below should tell you everything.
The source code is below is the code without the methods to get the text yet. If someone could help me change it, i would appreciate it very much.
class GUIFrame extends JFrame
{
public GUIFrame()
{
setSize(100, 100);
setTitle("KeyCheckTest");
Container container = getContentPane();
container.setLayout(new BorderLayout());
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
});
JTextField rangeTxtField = new JTextField(5);
rangeTxtField.addKeyListener(new KeyValidator());
JTextField txtField = new JTextField(5);
txtField.addKeyListener(new KeyValidator());
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(rangeTxtField);
panel.add(txtField);
container.add(panel,"Center");
}
}
class KeyValidator extends KeyAdapter
{
public void keyTyped(KeyEvent evt)
{
checkKey(evt);
}
public void keyPressed(KeyEvent evt)
{
checkKey(evt);
}
public void keyReleased(KeyEvent evt)
{
checkKey(evt);
}
private void checkKey(KeyEvent evt)
{
charkey= evt.getKeyChar();
switch (evt.getID())
{
case KeyEvent.KEY_TYPED:
{
/* Dot (.) key */
if ((int)key == 46)
{
if (start)
{
evt.consume();
}
}
/* Dash key */
else if ((int)key == 45)
{
if (start)
{
evt.consume();
start = false;
}
}
/* Enter key */
else if ((int)key == 10)
{

}
/* Backspace key */
else if ((int)key == 8)
{
}
/* numeric keys */
else if (((int)key >= 48) && ((int)key <= 57))
{
start = false;
}
else
{
evt.consume();
}
break;
}
default:
break;
}
}
private static boolean start = true;
}
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm, where to start.
I would suggest there are simpler ways to do what you are trying to do, but since it "almost works" we'll just work with what you have.
First, you need to have access to the two JTextFields in your KeyAdapter. The easiest way to do this is to make your KeyValidator an inner class. Just place the entire class definition for KeyAdapter inside the definition of GUIFrame. You actually only have to move two curly braces!
Next, you can't have static members in an inner class so make your boolean start non-static. It's not going to change anything in your algorithm so it's safe.
Next, you need to make rangeTxtField and txtField members of the GUIFrame class, NOT local variables as they are now. Declare them as JTextField member variables outside all the class' methods. They'll get initialized just like they are now in your GUIFrame construtor.
Finally, you have access to the text fields in your KeyValidator class. You can call txtField.getText() and rangeTxtField.getText() now in any method you choose.
Good luck!
 
jacq carballo
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi rob. tnx for the reply. i would be very happy to learn the easy way to do what i am doing. i know that i could make a class to check if the value of the JTextField is of integer value or not. The problem is that on the rangeTxtField, i should validate the following inputs:
0...999 (meaning all)
1 - 10 (meaning items 1 to 10)
and any other valid integer values. (single item entry)
So you see, my problem is with the first two types of entry.
Thanks in advance for your help and reply.
jacq
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the sun tutorial on JTextField. It will have the answers you seek.
reply
    Bookmark Topic Watch Topic
  • New Topic