Tue Jun 02, 2009 5:52 am
Fri Jun 19, 2009 3:49 am
//problem statement 5.P.1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//import java.awt.Container;
public class MyDealer extends JApplet implements KeyListener
{
//variable for the panel
JPanel panelObject;
//variables for labels
JLabel labelCustName;
JLabel labelCustCellNo;
JLabel labelCustAddress;
JLabel labelCustAge;
//variables for data entry controls
JTextField textCustName;
JTextField textCustCellNo;
JTextField textAddress;
JTextField textCustAge;
String name, no, add, age;
JButton btnSubmit;
//variables for the layout
GridBagLayout gbObject;
GridBagConstraints gbc;
public void init()
{
//initializes the layout variables
gbObject = new GridBagLayout();
gbc = new GridBagConstraints();
panelObject = (JPanel)getContentPane();
panelObject.setLayout(gbObject);
setSize(800,200);
//initializes label controls
labelCustName = new JLabel("Dealer Name");
labelCustCellNo = new JLabel("Cell Number ");
labelCustAddress= new JLabel("Address");
labelCustAge = new JLabel("Age");
//initializes data entry controls
textCustName = new JTextField(30);
textCustCellNo = new JTextField(15);
textCustAge = new JTextField(2);
textAddress = new JTextField(45);
btnSubmit = new JButton("SUBMIT");
//Add listeners to the controls
textCustName.addKeyListener(this);
textCustCellNo.addKeyListener(this);
textAddress.addKeyListener(this);
textCustAge.addKeyListener(this);
//add controls for customer name;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 1;
gbc.gridy = 5;
gbObject.setConstraints(labelCustName,gbc);
panelObject.add(labelCustName);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 4;
gbc.gridy = 5;
gbObject.setConstraints(textCustName,gbc);
panelObject.add(textCustName);
//add controls for cell number
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx =1;
gbc.gridy =8;
gbObject.setConstraints(labelCustCellNo,gbc);
panelObject.add(labelCustCellNo);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx =4;
gbc.gridy =8;
gbObject.setConstraints(textCustCellNo, gbc);
panelObject.add(textCustCellNo);
//add controls for package
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx =1;
gbc.gridy =11;
gbObject.setConstraints(labelCustAddress, gbc);
panelObject.add(labelCustAddress);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx =4;
gbc.gridy =11;
gbObject.setConstraints(textAddress, gbc);
panelObject.add(textAddress);
//add controls for customer age
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx =1;
gbc.gridy =14;
gbObject.setConstraints(labelCustAge, gbc);
panelObject.add(labelCustAge);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx =4;
gbc.gridy =14;
gbObject.setConstraints(textCustAge, gbc);
panelObject.add(textCustAge);
gbc.anchor = GridBagConstraints.SOUTH;
gbc.gridx = 4;
gbc.gridy = 16;
gbObject.setConstraints(btnSubmit,gbc);
panelObject.add(btnSubmit);
}
// BEGINNING OF KEY LISTENER FUNCTIONS
public void keyPressed(KeyEvent e)
{
Object obj = e.getSource();
if (obj == textCustName)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER)
{
String name = textCustName.getText();
if (name.length() == 0)
{
JOptionPane.showMessageDialog(this,"Name cannot be empty");
//showStatus("cannot be empty");
//return;
}
else
{
textCustCellNo.requestFocusInWindow();
}
}
}
if (obj == textCustCellNo)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER)
{
String no = textCustCellNo.getText();
if (no.length() != 10)
{
JOptionPane.showMessageDialog(this,"Mobile number entered should be of ten digits");
}
else
{
textAddress.requestFocusInWindow();
}
}
}
if (obj == textAddress)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER)
{
textCustAge.requestFocusInWindow();
}
}
if (obj == textCustAge)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER)
{
int age = Integer.parseInt(textCustAge.getText());
if (age <= 18 || age >= 100)
{
JOptionPane.showMessageDialog(this,"Wrong Age Input. Please Recheck!");
}
else
{
btnSubmit.requestFocusInWindow();
}
}
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
//showStatus(" Recently typed characters are : " + e.getKeyChar());
}
//END OF KEY LISTENER FUNCTIONS
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.