Submission Form with Input Validation
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.omg.CORBA.ExceptionList;
import java.awt.Toolkit;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import net.miginfocom.swing.MigLayout;
import javax.swing.ImageIcon;
import java.awt.Font;
import java.awt.Frame;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import java.awt.SystemColor;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class BillCounter extends JFrame {
//instance variables
private JPanel contentPane;
// for get input
public JTextField tfMeterNo;
public JTextField tfName;
public JTextField tfunit;
// for radio button
private JRadioButton rdbtnTypeD;
private JRadioButton rdbtnTypeC;
private JRadioButton rdbtnTypeI;
// for show error message
private String lblmeterNumberErrorMsg;
private String lblNameErrorMsg;
private String lblmeterTotalUnitErrorMsg;
private String lblMeterTypeErrorMsg;
// for ensure that our programs is error free
private boolean anyError;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BillCounter frame = new BillCounter();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public BillCounter() {
setTitle("LESCO.......Bill Counter");
setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Ali\\Desktop\\pic for project\\LESCO-Logo22-crop.gif"));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 479, 593);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.setLayout(null);
JLabel logo = new JLabel("");
logo.setBounds(10, 11, 453, 86);
logo.setIcon(new ImageIcon("C:\\Users\\Ali\\Desktop\\pic for project\\logo.png"));
contentPane.add(logo);
// for produce error message
JLabel lblmeterNumberError = new JLabel("");
lblmeterNumberError.setForeground(Color.RED);
lblmeterNumberError.setBounds(152, 174, 202, 19);
contentPane.add(lblmeterNumberError);
JLabel lblNameError = new JLabel("");
lblNameError.setForeground(Color.RED);
lblNameError.setBounds(152, 237, 202, 19);
contentPane.add(lblNameError);
JLabel lblTotalUnitError = new JLabel("");
lblTotalUnitError.setForeground(Color.RED);
lblTotalUnitError.setBounds(152, 306, 202, 19);
contentPane.add(lblTotalUnitError);
JLabel lblMeterTypeError = new JLabel("");
lblMeterTypeError.setForeground(Color.RED);
lblMeterTypeError.setBounds(152, 388, 261, 19);
contentPane.add(lblMeterTypeError);
JLabel lblMeterNo = new JLabel("Meter No");
lblMeterNo.setBounds(25, 153, 61, 19);
lblMeterNo.setFont(new Font("Tahoma", Font.PLAIN, 15));
contentPane.add(lblMeterNo);
tfMeterNo = new JTextField();
tfMeterNo.setBounds(152, 153, 202, 23);
tfMeterNo.setBackground(SystemColor.inactiveCaptionBorder);
tfMeterNo.setFont(new Font("Tahoma", Font.PLAIN, 14));
contentPane.add(tfMeterNo);
tfMeterNo.setColumns(10);
JLabel ConsumerName = new JLabel("Owner Name");
ConsumerName.setBounds(25, 216, 86, 19);
ConsumerName.setFont(new Font("Tahoma", Font.PLAIN, 15));
contentPane.add(ConsumerName);
tfName = new JTextField();
tfName.setBounds(152, 216, 202, 23);
tfName.setBackground(SystemColor.inactiveCaptionBorder);
tfName.setFont(new Font("Tahoma", Font.PLAIN, 14));
tfName.setColumns(10);
contentPane.add(tfName);
JRadioButton rdbtnTypeD =new JRadioButton("Domestic");
rdbtnTypeD.setBounds(134, 358, 86, 23);
JRadioButton rdbtnTypeC = new JRadioButton("Commercial");
rdbtnTypeC.setBounds(231, 358, 105, 23);
JRadioButton rdbtnTypeI = new JRadioButton("Industrial");
rdbtnTypeI.setBounds(348, 358, 86, 23);
ButtonGroup group = new ButtonGroup();
group.add(rdbtnTypeD);
group.add(rdbtnTypeC);
group.add(rdbtnTypeI);
getContentPane().add(rdbtnTypeD);
getContentPane().add(rdbtnTypeC);
getContentPane().add(rdbtnTypeI);
JLabel MeterType = new JLabel("MeterType");
MeterType.setBounds(25, 358, 86, 19);
MeterType.setFont(new Font("Tahoma", Font.PLAIN, 15));
contentPane.add(MeterType);
JLabel UnitConsumed = new JLabel("Total Units");
UnitConsumed.setBounds(25, 284, 86, 19);
UnitConsumed.setFont(new Font("Tahoma", Font.PLAIN, 15));
contentPane.add(UnitConsumed);
tfunit = new JTextField();
tfunit.setBounds(152, 284, 202, 23);
tfunit.setBackground(SystemColor.inactiveCaptionBorder);
tfunit.setFont(new Font("Tahoma", Font.PLAIN, 14));
tfunit.setColumns(10);
contentPane.add(tfunit);
JButton btnGetBill = new JButton("Get Bill");
btnGetBill.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
// for getting the values from text fields
long meterNo=0;
long units=0;
String name;
// for error message
try {
// type casting for data
String strMeterNo=tfMeterNo.getText();
meterNo=Long.parseLong(strMeterNo);
anyError=false;
lblmeterNumberErrorMsg="";
lblmeterNumberError.setText(lblmeterNumberErrorMsg);
}
catch (java.lang.NumberFormatException e1)
{
lblmeterNumberErrorMsg="Something Going wrong....";
lblmeterNumberError.setText(lblmeterNumberErrorMsg);
anyError=true;
}
try {
// type casting for data
String strUnit=tfunit.getText();
units=Long.parseLong(strUnit);
anyError=false;
lblmeterTotalUnitErrorMsg="";
lblTotalUnitError.setText(lblmeterTotalUnitErrorMsg);
}
catch (java.lang.NumberFormatException e1)
{
lblmeterTotalUnitErrorMsg="Something Going wrong..";
lblTotalUnitError.setText(lblmeterTotalUnitErrorMsg);
anyError=true;
}
// for get the value form text field
name=tfName.getText();
int l=tfName.getText().length();
String test []={"1","2","3","4","5","6","7","8","9","0","!","@","#","$","%","^","&","*","(",")","-","_","=","+","?","{","}"};
for (int c =0; c<27;c++)
{
if (name.startsWith(test[c]) ||name.endsWith(test[c]) || name.contains(test[c]) ||name.equals("") )
{
lblNameErrorMsg="Invalid input...";
lblNameError.setText(lblNameErrorMsg);
anyError=true;
break;
}
else
{
lblNameErrorMsg="";
lblNameError.setText(lblNameErrorMsg);
anyError=false;
continue;
}
}
// after pressing the button
if (anyError)
{
btnGetBill.setText("Check Again");
btnGetBill.setFont(new Font("Tahoma", Font.PLAIN, 23));
btnGetBill.setForeground(Color.RED);
setTitle("Something going wrong....");
}
else
{
btnGetBill.setText("Please Wait..");
btnGetBill.setFont(new Font("Tahoma", Font.PLAIN, 23));
btnGetBill.setForeground(Color.ORANGE);
setTitle("Please Wait..");
}
// what type of connection
boolean domestic=rdbtnTypeD.isSelected();
boolean commercial=rdbtnTypeC.isSelected();
boolean industrial=rdbtnTypeI.isSelected();
if (domestic==false && commercial==false && industrial==false)
{
lblMeterTypeErrorMsg="Please select anyone....";
lblMeterTypeError.setText(lblMeterTypeErrorMsg);
anyError=true;
}
else
{
lblMeterTypeErrorMsg="";
lblMeterTypeError.setText(lblMeterTypeErrorMsg);
anyError=false;
}
Methods m=new Methods();
//give enter value to the variables
m.Methods(meterNo, name, units, domestic, commercial, industrial);
PrintBill pb= new PrintBill();
//if there is no error
if (anyError==false)
{
System.out.println("No error");
pb.lblNewLabel_3NameOfCunsumer.setText(tfName.getText());
pb.lblTotalConsumeUnits.setText(tfunit.getText());
pb.lblAmount.setText("Rs. "+m.costOfelecticity()) ;
pb.lblTypeOfMeter.setText(m.connectionType());
pb.lblMeterNoFinal.setText(tfMeterNo.getText());
}
else
System.out.println("error");
pb.setVisible(false);
}
}
);
btnGetBill.setBounds(152, 443, 188, 50);
btnGetBill.setFont(new Font("Tahoma", Font.PLAIN, 25));
contentPane.add(btnGetBill);
JLabel lblTitle = new JLabel("Electric Bill Counter");
lblTitle.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 27));
lblTitle.setForeground(new Color(128, 128, 128));
lblTitle.setBounds(113, 95, 241, 34);
contentPane.add(lblTitle);
}
}
Comments
Post a Comment