• 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

validating awt textfields

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
}
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. Welcome to the Ranch Luisa.

Here is The Java Tutorial
[ November 22, 2003: Message edited by: Jose Botella ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic