aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTextField on getText() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField on getText() method" Watch "JTextField on getText() method" New topic
Author

JTextField on getText() method

jacq carballo
Ranch Hand

Joined: Feb 10, 2002
Posts: 42
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;
}
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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!


Rob
SCJP 1.4
jacq carballo
Ranch Hand

Joined: Feb 10, 2002
Posts: 42
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
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Take a look at the sun tutorial on JTextField. It will have the answers you seek.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextField on getText() method