File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes validating awt textfields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "validating awt textfields" Watch "validating awt textfields" New topic
Author

validating awt textfields

Prudent Kay
Greenhorn

Joined: Nov 21, 2003
Posts: 5
I have created the following program that accepts values and adds them up when you click on the 'add' button. Can you pliz specify the condition to be used such that the user is not allowed to enter an alphabet or other character. if that happens an error message should be displayed on label l4.
import java.awt.*;
import java.awt.event.*;
class Myframe extends Frame implements ActionListener
{
TextField t1,t2,t3;
Button b1,b2;
Label l1,l2,l3,l4;
Myframe()
{
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
b1=new Button("Add");
b2=new Button("Exit");
l1=new Label("Enter num1");
l2=new Label("Enter num2");
l3=new Label("Result");
l4 = new Label(" ");
setLayout(new FlowLayout());
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);
add(b2);
add(l4);
b1.addActionListener(this);
b2.addActionListener(this);
setSize(300,300);
setVisible(true);
}
public static void main(String s[])
{
new Myframe();
}
public void actionPerformed(ActionEvent ae)
{
String str1 = t1.getText();
String str2 = t2.getText();
if(ae.getSource()==b1)
{
if(str1 <='a' || str1 >= 'z')
{
la.setText("Enter a number");
}

else
{
int x=Integer.parseInt(str1);
int y=Integer.parseInt(str2);
t3.setText(String.valueOf(x+y));
}

}
else if(ae.getSource()==b2);
{
System.exit(0);
}
}
}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

Welcome to the Ranch Kuisa, but please take the time to make sure that you are posting your question in the correct forum. Why you chose the JSP forum for this post is beyond me. Moving to the Swing forum.
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: validating awt textfields