
Write a java swing program that takes name and marks as input with all validation. The name and marks will be displayed in message box when user clicks on a button.

import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.regex.*;
class ValidateData{
public static void main(String[] args){
JFrame f=new JFrame();
JLabel label1=new JLabel("Name: ");
JLabel label2=new JLabel("Marks: ");
final JTextField text1=new JTextField(20);
final JTextField text2=new JTextField(20);
JButton b=new JButton("Check");
text1.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent e){
String st=text1.getText();
Pattern p = Pattern.compile("^[a-zA-Z]+$");
Matcher m = p.matcher(st);
if(!m.find()){
JOptionPane.showMessageDialog(null, "Only characters A-Z, a-z are allowed!","Error!", JOptionPane.ERROR_MESSAGE);
text1.setText("");
}
}
});
text2.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent e){
String st=text2.getText();
Pattern p = Pattern.compile("(\\d)");
Matcher m = p.matcher(st);
if(!m.find()){
JOptionPane.showMessageDialog(null, "Only numbers are allowed!", "Error!", JOptionPane.ERROR_MESSAGE);
text2.setText("");
}
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String v1=text1.getText();
String v2=text2.getText();
JOptionPane.showMessageDialog(null,"Name is: "+v1+", Marks is: "+v2);
}
});
JPanel p=new JPanel(new GridLayout(3,2));
p.add(label1);
p.add(text1);
p.add(label2);
p.add(text2);
p.add(b);
f.add(p);
f.setVisible(true);
f.pack();
}
}

import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.regex.*;
class ValidateData{
public static void main(String[] args){
JFrame f=new JFrame();
JLabel label1=new JLabel("Name: ");
JLabel label2=new JLabel("Marks: ");
final JTextField text1=new JTextField(20);
final JTextField text2=new JTextField(20);
JButton b=new JButton("Check");
text1.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent e){
String st=text1.getText();
Pattern p = Pattern.compile("^[a-zA-Z]+$");
Matcher m = p.matcher(st);
if(!m.find()){
JOptionPane.showMessageDialog(null, "Only characters A-Z, a-z are allowed!","Error!", JOptionPane.ERROR_MESSAGE);
text1.setText("");
}
}
});
text2.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent e){
String st=text2.getText();
Pattern p = Pattern.compile("(\\d)");
Matcher m = p.matcher(st);
if(!m.find()){
JOptionPane.showMessageDialog(null, "Only numbers are allowed!", "Error!", JOptionPane.ERROR_MESSAGE);
text2.setText("");
}
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String v1=text1.getText();
String v2=text2.getText();
JOptionPane.showMessageDialog(null,"Name is: "+v1+", Marks is: "+v2);
}
});
JPanel p=new JPanel(new GridLayout(3,2));
p.add(label1);
p.add(text1);
p.add(label2);
p.add(text2);
p.add(b);
f.add(p);
f.setVisible(true);
f.pack();
}
}