My Errors are appearing at call to a customer handler class.At the end of each panel there are components said to listen for event and send to my handler classes
cbName1.addItemListener(new ComboBoxHandler());
"Addressbook.java": Error #: 300 : class ComboBoxHandler not found in class Addressbook at line 542, column 31
How do I make these recognisable when asked to make reference to class in these events.
Should I make a seperate file for the all the handling of events on tab forms listened for.And create get methods for each field?
And Seperate file for data actions.
Thanks!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.io.*;
import java.util.Date;
import java.text.NumberFormat;
public class Addressbook extends JFrame{
// Define constant variables
final static
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
final static String url = "jdbc
dbc:addBKTAFE";
final static String login = "LOG-IN";
final static String tab_login = "Log-in";
final static String tab1 = "INQUIRE Personnel Details";
final static String tab2 = "UPDATE/DELETE Personnel Details";
final static String tab3 = "INSERT Personnel Details";
final static String insert = "SAVE RECORD";
final static String update = "UPDATE RECORD";
final static String delete = "DELETE RECORD";
final static String inquire = "INQUIRE RECORD";
final static String clear = " CLEAR ";
final static String relogin = "Log-in failed! Please relog-in!";
final static String norecfound = "No Record Found!";
final static String recinserted = "Record Inserted!";
final static String recupdated = "Record Updated!";
final static String recdeleted = "Record Deleted!";
final static String numericerror = "Age should be numeric!";
final static String information = "INFORMATION";
final static String error = "ERROR";
final static String genexception = "GENERAL EXCEPTION";
final static String sqlexception = "SQL EXCEPTION";
final static String confdelete = "CONFIRM DELETE";
final static String slash = "/";
final static String table1 = "persons";
final static String table2 = "Addresses";
// Events events = new Events(this);
// Define variablesfor general use
String sql = ""; // Used to store sql statements
int pane_number = 0; // Used to indicate what screen needs to be processed
// like resetting input fields and comboboxes
boolean abort = false;// Used to indicate if error found to avoid further
// processing/validations
// Define container, panels and tabbedpane
Container cntr = getContentPane();
JTabbedPane tpane = new JTabbedPane();
JPanel cbpanel1 , cbpanel2 , cbpanel3,
panel1 , panel2 , panel3;
// Setup constraints and type of layout
GridBagConstraints constraints = new GridBagConstraints();
GridBagConstraints constraints1 = new GridBagConstraints();
GridBagConstraints constraints2 = new GridBagConstraints();
GridBagConstraints constraints3 = new GridBagConstraints();
GridBagLayout layout = new GridBagLayout ();
// Define fonts to be used
Font labelFont = new Font("Arial",Font.PLAIN,12);
Font buttonFont = new Font("Arial",Font.BOLD,13);
// Define labels
JLabel lbUser = new JLabel("Enter User ID: " );
JLabel lbPassword = new JLabel("Enter Password: ");
JLabel lbSelectName = new JLabel("Search Name: " );
JLabel lbFirstName = new JLabel("First Name: " );
JLabel lbLastName = new JLabel("Last Name: " );
JLabel lbAddress = new JLabel("Address: " );
JLabel lbCity = new JLabel("City" );
JLabel lbState = new JLabel("State: " );
JLabel lbPostcode = new JLabel("Postcode" );
JLabel lbCountry = new JLabel("Country" );
JLabel lbEmailAddress = new JLabel("Email Address: " );
JLabel lbHomeNumber = new JLabel("Home Phone No.: " );
JLabel lbFaxNumber = new JLabel("Fax No.: " );
// Define combo boxes in third screen (insert pane)
JComboBox cbName1 = new JComboBox();
JComboBox cbPersonId1 = new JComboBox();
// Define combo boxes in second (update/delete pane)
JComboBox cbName2 = new JComboBox();
JComboBox cbPersonId2 = new JComboBox();
// Define buttons, text fields and password field
JButton btLogin = new JButton (login );
JButton btInsert = new JButton (insert );
JButton btUpdate = new JButton (update );
JButton btDelete = new JButton (delete );
JButton btInquire = new JButton (inquire);
JButton btClear = new JButton (clear );
JPasswordField jpPassword = new JPasswordField(10 );
JTextField tfUser = new JTextField("",10 );
// Inquiry fields on first screen (inquiry pane)
JTextField tfFirstName1 = new JTextField("",30);
JTextField tfLastName1 = new JTextField("",30);
JTextField tfAddress1 = new JTextField("",30);
JTextField tfCity1 = new JTextField("",15);
JTextField tfState1 = new JTextField("",15);
JTextField tfPostcode1 = new JTextField("",30);
JTextField tfCountry1 = new JTextField("",15 );
JTextField tfEmailAddress1 = new JTextField("",30);
JTextField tfHomeNumber1 = new JTextField("",15);
JTextField tfFaxNumber1 = new JTextField("",15 );
// Input fields on second screen (update/delete pane)
JTextField tfFirstName2 = new JTextField("",30);
JTextField tfLastName2 = new JTextField("",30);
JTextField tfAddress2 = new JTextField("",30);
JTextField tfCity2 = new JTextField("",15);
JTextField tfState2 = new JTextField("",15);
JTextField tfPostcode2 = new JTextField("",30);
JTextField tfCountry2 = new JTextField("",15 );
JTextField tfEmailAddress2 = new JTextField("",30);
JTextField tfHomeNumber2 = new JTextField("",15);
JTextField tfFaxNumber2 = new JTextField("",15 );
// Input fields on third screen (insert pane)
JTextField tfFirstName3 = new JTextField("",30);
JTextField tfLastName3 = new JTextField("",30);
JTextField tfAddress3 = new JTextField("",30);
JTextField tfCity3 = new JTextField("",15);
JTextField tfState3 = new JTextField("",15);
JTextField tfPostcode3 = new JTextField("",30);
JTextField tfCountry3 = new JTextField("",15 );
JTextField tfEmailAddress3 = new JTextField("",30);
JTextField tfHomeNumber3 = new JTextField("",15);
JTextField tfFaxNumber3 = new JTextField("",15 );
//------------------------------------------------------------------------------>>>
////////////////////////////////////////////////////////////////////////////////
//-----------------------Start Addressbook()------------------------------------>>>
Addressbook(){
}
// define listener after adding items to CB to avoid triggering it
//cbName1.addItemListener(new ItemListener());
// public void itemStateChanged(ItemEvent e){
//--------------------------END Addressbook constructor------------------------------------->>>
////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------>>>
//------------------------------------------------------------------------------>>>
////////////////////////////////////////////////////////////////////////////////
//--------------------Start setupLoginPanel()----------------------------------->>>
// Setup the login screen
public void setupLoginPanel(){
// set application title
setTitle("Address Book Application");
// center screen
setLocation((Toolkit.getDefaultToolkit().getScreenSize().width
- getWidth())/2,
(Toolkit.getDefaultToolkit().getScreenSize().height
- getHeight())/2);
panel1 = new JPanel();
// set screen border
panel1.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),""));
// add tabbedpane to panel
tpane.addTab(tab_login, panel1);
// add panel to container
cntr.add(tpane);
// setup layout as GridBagLayout
constraints.insets = new Insets(2,2,2,2);
panel1.setLayout(layout);
// setup User ID label in display area
lbUser.setFont(labelFont);
constraints.ipadx = 2;
constraints.ipady = 2;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbUser, constraints);
panel1.add(lbUser);
// setup User ID input field in display area
tfUser.setFont(labelFont);
constraints.ipadx = 2;
constraints.ipady = 2;
constraints.gridx = 1;
constraints.gridy = 0;
constraints.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(tfUser, constraints);
panel1.add(tfUser);
// setup Password label in display area
lbPassword.setFont(labelFont);
constraints.ipadx = 2;
constraints.ipady = 2;
constraints.gridx = 0;
constraints.gridy = 1;
constraints.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbPassword, constraints);
panel1.add(lbPassword);
// setup Password input field in display area
jpPassword.setEchoChar('*');
constraints.ipadx = 2;
constraints.ipady = 2;
constraints.gridx = 1;
constraints.gridy = 1;
layout.setConstraints(jpPassword, constraints);
panel1.add(jpPassword);
// setup Login button in display area
btLogin.setFont(buttonFont);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridy = 3;
constraints.gridx = 1;
layout.setConstraints(btLogin, constraints);
panel1.add(btLogin);
// setup login button listener
btLogin.addActionListener(new ButtonHandler());
// allow ALT L to press login button
btLogin.setMnemonic('l');
}
//--------------------End setupLoginPanel()------------------------------------->>>
////////////////////////////////////////////////////////////////////////////////
//--------------------Start login()--------------------------------------------->>>
// Validate user input from the login screen based on information from login
// table (note: manually create/update your login from table LOGIN).
public void login(){
String user = tfUser.getText();
user = user.trim();
char[] pw = jpPassword.getPassword();
String password = new String(pw).trim();
sql = "SELECT * FROM login WHERE user='"+
user+"' AND password='"+password+"'";
try{
// load MS Access driver
Class.forName(driver);
}catch(java.lang.ClassNotFoundException ex){
JOptionPane.showMessageDialog(null,ex.getMessage(), error ,
JOptionPane.PLAIN_MESSAGE);
}
try{
// setup connection to DBMS
Connection conn = DriverManager.getConnection(url);
// create statement
Statement stmt = conn.createStatement();
// execute sql statement
stmt.execute(sql);
ResultSet rs = stmt.getResultSet();
boolean recordfound = rs.next();
if (recordfound){
tpane.removeTabAt(0);
showPane1();
showPane2();
showPane3();
}
else{
// username/password invalid
JOptionPane.showMessageDialog(null,relogin, error,
JOptionPane.INFORMATION_MESSAGE);
//clear login and password fields
tfUser.setText ("");
jpPassword.setText("");
}
conn.close();
}catch(Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage(), genexception,
JOptionPane.INFORMATION_MESSAGE);
}
}
//--------------------End login()----------------------------------------------->>>
////////////////////////////////////////////////////////////////////////////////
//--------------------Start showPane1()----------------------------------------->>>
// Setup screen 1(inquiry pane) including labels, input fields, comboboxes.
// Table PERSONS is read to list inquiry parameters.
void showPane1(){
panel1 = new JPanel();
cbpanel1 = new JPanel();
// set screen border
panel1.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),""));
// add tabbedpane to panel
tpane.addTab(tab1, panel1);
// setup layout as GridBagLayout
constraints1.insets = new Insets(2,2,2,2);
panel1.setLayout (layout);
cbpanel1.setLayout (layout);
// setup Name combobox label
lbSelectName.setFont(labelFont);
constraints1.gridx = 0;
constraints1.gridy = 0;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbSelectName, constraints1);
panel1.add(lbSelectName);
// setup Name combobox as search key
cbName1.setFont(labelFont);
constraints1.ipady = 10;
constraints1.gridx = 1;
constraints1.gridy = 0;
constraints1.gridwidth = 3;
constraints1.anchor = GridBagConstraints.WEST;
constraints1.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(cbName1, constraints1);
panel1.add (cbName1);
// setup search combobox (Name and corresponding key)
cbName1.addItem ("Choose one:");
cbPersonId1.addItem("0");
// setup First Name label in display area
lbFirstName.setFont(labelFont);
constraints1.gridx = 0;
constraints1.gridy = 1;
constraints1.anchor = GridBagConstraints.WEST;
constraints1.fill = GridBagConstraints.NONE;
constraints1.gridwidth = 1;
layout.setConstraints(lbFirstName, constraints1);
panel1.add(lbFirstName);
// setup First Name input field in display area
tfFirstName1.setFont(labelFont);
tfFirstName1.setEditable(false);
constraints1.gridx = 1;
constraints1.gridy = 1;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFirstName1, constraints1);
panel1.add(tfFirstName1);
// setup Last Name label in display area
lbLastName.setFont(labelFont);
constraints1.gridx = 2;
constraints1.gridy = 1;
constraints1.anchor = GridBagConstraints.WEST;
constraints1.fill = GridBagConstraints.NONE;
layout.setConstraints(lbLastName, constraints1);
panel1.add(lbLastName);
// setup Last Name input field in display area
tfLastName1.setFont(labelFont);
tfLastName1.setEditable(false);
constraints1.gridx = 3;
constraints1.gridy = 1;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfLastName1, constraints1);
panel1.add(tfLastName1);
// setup Address label in display area
lbAddress.setFont(labelFont);
constraints1.gridx = 0;
constraints1.gridy = 2;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbAddress, constraints1);
panel1.add(lbAddress);
// setup Address input field in display area
tfAddress1.setFont(labelFont);
tfAddress1.setEditable(false);
constraints1.gridx = 1;
constraints1.gridy = 2;
constraints1.gridwidth = 3;
constraints1.anchor = GridBagConstraints.WEST;
constraints1.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(tfAddress1, constraints1);
panel1.add(tfAddress1);
// setup City label in display area
lbCity.setFont(labelFont);
constraints1.gridx = 0;
constraints1.gridy = 3;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbCity, constraints1);
panel1.add(lbCity);
// setup City input field in display area
tfCity1.setFont(labelFont);
tfCity1.setEditable(false);
constraints1.gridx = 1;
constraints1.gridy = 3;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfCity1, constraints1);
panel1.add(tfCity1);
// setup State label in display area
lbState.setFont(labelFont);
constraints1.gridx = 2;
constraints1.gridy = 3;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbState, constraints1);
panel1.add(lbState);
// setup State input field in display area
tfState1.setFont(labelFont);
tfState1.setEnabled(false);
constraints1.gridx = 3;
constraints1.gridy = 3;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfState1, constraints1);
panel1.add(tfState1);
// indicate inquiry pane
pane_number = 1;
// setup Address label in display area
lbPostcode.setFont(labelFont);
constraints1.gridx = 0;
constraints1.gridy = 4;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbPostcode, constraints1);
panel1.add(lbPostcode);
// setup Address input field in display area
tfPostcode1.setFont(labelFont);
tfPostcode1.setEditable(false);
constraints1.gridx = 1;
constraints1.gridy = 4;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfPostcode1, constraints1);
panel1.add(tfPostcode1);
// setup Country label in display area
lbCountry.setFont(labelFont);
constraints1.gridx = 2;
constraints1.gridy = 4;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbCountry, constraints1);
panel1.add(lbCountry);
// setup Country input field in display area
tfCountry1.setFont(labelFont);
tfCountry1.setEditable(false);
constraints1.gridx = 3;
constraints1.gridy = 4;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfCountry1, constraints1);
panel1.add(tfCountry1);
// setup Email Address label in display area
lbEmailAddress = new JLabel ("Email Address:");
lbEmailAddress.setFont(labelFont);
constraints1.gridx = 0;
constraints1.gridy = 5;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbEmailAddress, constraints1);
panel1.add(lbEmailAddress);
// setup Email Address input field in display area
tfEmailAddress1.setFont(labelFont);
constraints1.gridx = 1;
constraints1.gridy = 5;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfEmailAddress1, constraints1);
panel1.add(tfEmailAddress1);
// setup Home Phone Number label in display area
lbHomeNumber.setFont(labelFont);
constraints1.gridx = 2;
constraints1.gridy = 5;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbHomeNumber, constraints1);
panel1.add(lbHomeNumber);
// setup Home Phone Number input field in display area
tfHomeNumber1.setFont(labelFont);
tfHomeNumber1.setEditable(false);
constraints1.gridx = 3;
constraints1.gridy = 5;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfHomeNumber1, constraints1);
panel1.add(tfHomeNumber1);
// setup Fax Number label in display area
lbFaxNumber.setFont(labelFont);
constraints1.gridx = 0;
constraints1.gridy = 6;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbFaxNumber, constraints1);
panel1.add(lbFaxNumber);
// setup Fax Number input field in display area
tfFaxNumber1.setFont(labelFont);
tfFaxNumber1.setEditable(false);
constraints1.gridx = 1;
constraints1.gridy = 6;
constraints1.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFaxNumber1, constraints1);
panel1.add(tfFaxNumber1);
// read table get the list of names in CB search key
accessDBInit();
// define listener after adding items to CB to avoid triggering it
cbName1.addItemListener(new ComboBoxHandler());
}
//--------------------End showPane1()------------------------------------------->>>
////////////////////////////////////////////////////////////////////////////////
//--------------------Start showPane2()----------------------------------------->>>
// Setup screen 2(update and delete pane) including labels, input fields,
// comboboxes, and buttons. Table PERSONS is read to list inquiry parameters.
void showPane2(){
panel2 = new JPanel();
cbpanel2 = new JPanel();
labelFont = new Font("Arial",Font.PLAIN,12);
buttonFont = new Font("Arial",Font.BOLD,12);
// set screen border
panel2.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),""));
// add tabbedpane to panel
tpane.addTab(tab2, panel2);
// setup layout as GridBagLayout
constraints2.insets = new Insets(2,2,2,2);
panel2.setLayout (layout);
cbpanel2.setLayout (layout);
// setup Name combobox label
lbSelectName.setFont(labelFont);
constraints2.gridx = 0;
constraints2.gridy = 0;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbSelectName, constraints2);
panel1.add(lbSelectName);
// setup Name combobox as search key
cbName2.setFont(labelFont);
constraints2.ipady = 10;
constraints2.gridx = 1;
constraints2.gridy = 0;
constraints2.gridwidth = 3;
constraints2.anchor = GridBagConstraints.WEST;
constraints2.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(cbName1, constraints2);
panel1.add (cbName1);
// setup search combobox (Name and corresponding key)
cbName1.addItem ("Choose one:");
cbPersonId1.addItem("0");
// setup First Name label in display area
lbFirstName.setFont(labelFont);
constraints2.gridx = 0;
constraints2.gridy = 1;
constraints2.anchor = GridBagConstraints.WEST;
constraints2.fill = GridBagConstraints.NONE;
constraints2.gridwidth = 1;
layout.setConstraints(lbFirstName, constraints2);
panel1.add(lbFirstName);
// setup First Name input field in display area
tfFirstName2.setFont(labelFont);
tfFirstName2.setEditable(false);
constraints2.gridx = 1;
constraints2.gridy = 1;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFirstName2, constraints2);
panel1.add(tfFirstName2);
// setup Last Name label in display area
lbLastName.setFont(labelFont);
constraints2.gridx = 2;
constraints2.gridy = 1;
constraints2.anchor = GridBagConstraints.WEST;
constraints2.fill = GridBagConstraints.NONE;
layout.setConstraints(lbLastName, constraints2);
panel1.add(lbLastName);
// setup Last Name input field in display area
tfLastName2.setFont(labelFont);
tfLastName2.setEditable(false);
constraints2.gridx = 3;
constraints2.gridy = 1;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfLastName2, constraints2);
panel1.add(tfLastName2);
// setup Address label in display area
lbAddress.setFont(labelFont);
constraints2.gridx = 0;
constraints2.gridy = 2;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbAddress, constraints2);
panel1.add(lbAddress);
// setup Address input field in display area
tfAddress2.setFont(labelFont);
tfAddress2.setEditable(false);
constraints2.gridx = 1;
constraints2.gridy = 2;
constraints2.gridwidth = 3;
constraints2.anchor = GridBagConstraints.WEST;
constraints2.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(tfAddress2, constraints2);
panel1.add(tfAddress2);
// setup City label in display area
lbCity.setFont(labelFont);
constraints2.gridx = 0;
constraints2.gridy = 3;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbCity, constraints2);
panel1.add(lbCity);
// setup City input field in display area
tfCity2.setFont(labelFont);
tfCity2.setEditable(false);
constraints2.gridx = 1;
constraints2.gridy = 3;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfCity2, constraints2);
panel1.add(tfCity2);
// setup State label in display area
lbState.setFont(labelFont);
constraints2.gridx = 2;
constraints2.gridy = 3;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbState, constraints2);
panel1.add(lbState);
// setup State input field in display area
tfState2.setFont(labelFont);
tfState2.setEnabled(false);
constraints2.gridx = 3;
constraints2.gridy = 3;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfState2, constraints2);
panel1.add(tfState2);
// indicate inquiry pane
pane_number = 1;
// setup Address label in display area
lbPostcode.setFont(labelFont);
constraints2.gridx = 0;
constraints2.gridy = 4;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbPostcode, constraints2);
panel1.add(lbPostcode);
// setup Address input field in display area
tfPostcode2.setFont(labelFont);
tfPostcode2.setEditable(false);
constraints2.gridx = 1;
constraints2.gridy = 4;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfPostcode2, constraints2);
panel1.add(tfPostcode2);
// setup Country label in display area
lbCountry.setFont(labelFont);
constraints2.gridx = 2;
constraints2.gridy = 4;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbCountry, constraints2);
panel1.add(lbCountry);
// setup Country input field in display area
tfCountry2.setFont(labelFont);
tfCountry2.setEditable(false);
constraints2.gridx = 3;
constraints2.gridy = 4;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfCountry2, constraints2);
panel1.add(tfCountry2);
// setup Email Address label in display area
lbEmailAddress = new JLabel ("Email Address:");
lbEmailAddress.setFont(labelFont);
constraints2.gridx = 0;
constraints2.gridy = 5;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbEmailAddress, constraints2);
panel1.add(lbEmailAddress);
// setup Email Address input field in display area
tfEmailAddress2.setFont(labelFont);
constraints2.gridx = 1;
constraints2.gridy = 5;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfEmailAddress2, constraints2);
panel1.add(tfEmailAddress2);
// setup Home Phone Number label in display area
lbHomeNumber.setFont(labelFont);
constraints2.gridx = 2;
constraints2.gridy = 5;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbHomeNumber, constraints2);
panel1.add(lbHomeNumber);
// setup Home Phone Number input field in display area
tfHomeNumber2.setFont(labelFont);
tfHomeNumber2.setEditable(false);
constraints2.gridx = 3;
constraints2.gridy = 5;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfHomeNumber2, constraints2);
panel1.add(tfHomeNumber2);
// setup Fax Number label in display area
lbFaxNumber.setFont(labelFont);
constraints2.gridx = 0;
constraints2.gridy = 6;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbFaxNumber, constraints2);
panel1.add(lbFaxNumber);
// setup Fax Number input field in display area
tfFaxNumber2.setFont(labelFont);
tfFaxNumber2.setEditable(false);
constraints2.gridx = 1;
constraints2.gridy = 6;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFaxNumber2, constraints2);
panel1.add(tfFaxNumber2);
// setup UPDATE button in display area
btUpdate.setFont(buttonFont);
constraints2.gridx = 3;
constraints2.gridy = 7;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(btUpdate, constraints2);
panel2.add(btUpdate);
// setup DELETE button in display area
btDelete.setFont(buttonFont);
constraints2.gridx = 1;
constraints2.gridy = 7;
constraints2.anchor = GridBagConstraints.WEST;
layout.setConstraints(btDelete, constraints2);
panel2.add(btDelete);
btUpdate.addActionListener(new ButtonHandler());
btDelete.addActionListener(new ButtonHandler());
// allow ALT U to press update button
btUpdate.setMnemonic('u');
// allow ALT D to press delete button
btDelete.setMnemonic('d');
// read table get the list of names in combo box search key
accessDBInit();
// define listener after adding items to CB to avoid triggering it
cbName2.addItemListener(new ComboBoxHandler());
}
//--------------------End showPane2()------------------------------------------->>>
////////////////////////////////////////////////////////////////////////////////
//--------------------Start showPane3()----------------------------------------->>>
// Setup screen 2(insert pane) including labels, input fields, comboboxes,
// and buttons.
void showPane3(){
panel3 = new JPanel();
// set screen border
panel3.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),""));
// add tabbedpane to panel
tpane.addTab(tab3, panel3);
// setup layout as GridBagLayout
constraints3.insets = new Insets(2,2,2,2);
panel3.setLayout (layout);
// setup First Name label in display area
JLabel lbFirstName = new JLabel("First Name:");
lbFirstName.setFont(labelFont);
constraints3.gridx = 0;
constraints3.gridy = 0;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbFirstName, constraints3);
panel3.add(lbFirstName);
// setup First Name input field in display area
tfFirstName3.setFont(labelFont);
constraints3.ipady = 8; // adjust heigth of input field
constraints3.gridx = 1;
constraints3.gridy = 0;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFirstName3, constraints3);
panel3.add(tfFirstName3);
// setup Last Name label in display area
lbLastName = new JLabel("Last Name: ");
lbLastName.setFont(labelFont);
constraints3.gridx = 2;
constraints3.gridy = 0;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbLastName, constraints3);
panel3.add(lbLastName);
// setup Last Name input field in display area
tfLastName3.setFont(labelFont);
constraints3.gridx = 3;
constraints3.gridy = 0;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfLastName3, constraints3);
panel3.add(tfLastName3);
// setup Middle Name label in display area
lbAddress = new JLabel("Address: ");
lbAddress.setFont(labelFont);
constraints3.gridx = 0;
constraints3.gridy = 1;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbAddress, constraints3);
panel3.add(lbAddress);
// setup Middle Name input field in display area
tfAddress3.setFont(labelFont);
constraints3.gridx = 1;
constraints3.gridy = 1;
constraints3.gridwidth = 3;
constraints3.anchor = GridBagConstraints.WEST;
constraints3.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(tfAddress3, constraints3);
panel3.add(tfAddress3);
// setup City label in display area
lbCity.setFont(labelFont);
constraints3.gridx = 0;
constraints3.gridy = 3;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbCity, constraints3);
panel1.add(lbCity);
// setup City input field in display area
tfCity3.setFont(labelFont);
tfCity3.setEditable(false);
constraints3.gridx = 1;
constraints3.gridy = 3;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfCity3, constraints3);
panel1.add(tfCity3);
// setup State label in display area
lbState.setFont(labelFont);
constraints3.gridx = 2;
constraints3.gridy = 3;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbState, constraints3);
panel1.add(lbState);
// setup State input field in display area
tfState3.setFont(labelFont);
tfState3.setEnabled(false);
constraints3.gridx = 3;
constraints3.gridy = 3;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfState3, constraints3);
panel1.add(tfState3);
// setup Address label in display area
lbPostcode.setFont(labelFont);
constraints3.gridx = 0;
constraints3.gridy = 4;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbPostcode, constraints3);
panel1.add(lbPostcode);
// setup Address input field in display area
tfPostcode3.setFont(labelFont);
tfPostcode3.setEditable(false);
constraints3.gridx = 1;
constraints3.gridy = 4;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfPostcode3, constraints3);
panel1.add(tfPostcode3);
// setup Country label in display area
lbCountry.setFont(labelFont);
constraints3.gridx = 2;
constraints3.gridy = 4;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbCountry, constraints3);
panel1.add(lbCountry);
// setup Country input field in display area
tfCountry3.setFont(labelFont);
tfCountry3.setEditable(false);
constraints3.gridx = 3;
constraints3.gridy = 4;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfCountry3, constraints3);
panel1.add(tfCountry3);
// setup Email Address label in display area
lbEmailAddress = new JLabel ("Email Address:");
lbEmailAddress.setFont(labelFont);
constraints3.gridx = 0;
constraints3.gridy = 5;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbEmailAddress, constraints3);
panel1.add(lbEmailAddress);
// setup Email Address input field in display area
tfEmailAddress3.setFont(labelFont);
constraints3.gridx = 1;
constraints3.gridy = 5;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfEmailAddress3, constraints3);
panel1.add(tfEmailAddress3);
// setup Home Phone Number label in display area
lbHomeNumber.setFont(labelFont);
constraints3.gridx = 2;
constraints3.gridy = 5;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbHomeNumber, constraints3);
panel1.add(lbHomeNumber);
// setup Home Phone Number input field in display area
tfHomeNumber3.setFont(labelFont);
tfHomeNumber3.setEditable(false);
constraints3.gridx = 3;
constraints3.gridy = 5;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfHomeNumber3, constraints3);
panel1.add(tfHomeNumber3);
// setup Fax Number label in display area
lbFaxNumber.setFont(labelFont);
constraints3.gridx = 0;
constraints3.gridy = 6;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(lbFaxNumber, constraints3);
panel1.add(lbFaxNumber);
// setup Fax Number input field in display area
tfFaxNumber3.setFont(labelFont);
tfFaxNumber3.setEditable(false);
constraints3.gridx = 1;
constraints3.gridy = 6;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(tfFaxNumber3, constraints3);
panel1.add(tfFaxNumber3);
// setup INSERT button in display area
btInsert.setFont(buttonFont);
constraints3.gridx = 3;
constraints3.gridy = 7;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(btInsert, constraints3);
panel3.add(btInsert);
// setup CLEAR button in display area
btClear.setFont(buttonFont);
constraints3.gridx = 1;
constraints3.gridy = 7;
constraints3.anchor = GridBagConstraints.WEST;
layout.setConstraints(btClear, constraints3);
panel3.add(btClear);
pane_number = 3;
btInsert.addActionListener(new ButtonHandler ());
btClear.addActionListener (new ButtonHandler ());
// allow ALT S to press insert button
btInsert.setMnemonic('s');
// allow ALT C to press clear button
btClear.setMnemonic ('c');
}
//--------------------End showPane3()------------------------------------------->>>
////////////////////////////////////////////////////////////////////////////////
//--------------------Start accessDBInit()-------------------------------------->>>
// This will read the table PERSONS to list the names of personnel that are
// exisiting from the database.
void accessDBInit(){
try{
sql = "SELECT person_id, FirstName, LastName FROM " +
table2 +" ORDER BY LastName";
// load MS Access driver
Class.forName(driver);
// Setup connection to DBMS
Connection conn=DriverManager.getConnection(url);
// Create statement
Statement stmt = conn.createStatement();
boolean hasResults = stmt.execute(sql);
if(hasResults){
ResultSet result = stmt.getResultSet();
displayResultsInit(result);
}
conn.close();
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage(),genexception,
JOptionPane.PLAIN_MESSAGE);
}
}
//--------------------End accessDBInit()---------------------------------------->>>
////////////////////////////////////////////////////////////////////////////////
//--------------------Start displayResultsInit()-------------------------------->>>
// Put the names of personnels read into the ComboBox Name which will be used
// as the search parameters for users.
void displayResultsInit(ResultSet rs) throws SQLException{
while(rs.next()){
// get values to display in Name combo box:
// concatenate last and first names
String name= rs.getString("LastName") +
"," +
rs.getString("FirstName");
// store person_id(unique identifier)
// to display corresponding person details later
String person_id = rs.getString("person_id");
switch(pane_number){
case 1: // inquiry screen
// put Names in search combo box
cbName1.addItem(name);
cbPersonId1.addItem(person_id);
break;
case 2: // update/delete screen
// put Names in search combo box
cbName2.addItem(name);
cbPersonId2.addItem(person_id);
}
}
}
//--------------------End displayResultsInit()---------------------------------->>>
////////////////////////////////////////////////////////////////////////////////
//--------------------Start updateRecord()-------------------------------------->>>
// Update personnel details.
void updateRecord(){
try{
// load MS Access driver
Class.forName(driver);
// Setup connection to DBMS
Connection conn = DriverManager.getConnection(url);
// Create statement
Statement stmt = conn.createStatement();
sql = "UPDATE "+
table2 +
" SET "+
"FirstName = '" + tfFirstName2.getText() + "'," +
"LastName = '" + tfLastName2.getText() + "'," +
"Address = '" + tfAddress2.getText() + "'," +
"City = '" + tfCity2.getText() + "'," +
"State = '" + tfState2.getText()+ "'," +
"Postcode= '" + tfPostcode2.getText()+ "'," +
"Country = " + tfCountry2.getText()+ "'," +
"HomePhone= '" + tfHomeNumber2.getText()+ "'," +
"Emailaddress= '" + tfEmailAddress2.getText()+ "'," +
"FaxNumber = '" + tfFaxNumber2.getText() + "'," +
"WHERE person_id = " + cbPersonId2.getSelectedItem().toString();
stmt.executeUpdate(sql);
conn.close();
JOptionPane.showMessageDialog(null,recupdated, information,
JOptionPane.INFORMATION_MESSAGE);
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage() + ex.toString(),genexception,
JOptionPane.INFORMATION_MESSAGE);
}
}
//--------------------End updateRecord()---------------------------------------->>>
////////////////////////////////////////////////////////////////////////////////
//--------------------Start deleteRecord()-------------------------------------->>>
// Delete personnel records.
void deleteRecord(){
try{
// load MS Access driver
Class.forName(driver);
// Setup connection to DBMS
Connection conn = DriverManager.getConnection(url);
// Create statement
Statement stmt = conn.createStatement();
sql = "DELETE FROM "+ table2+
" WHERE person_id = " + cbPersonId2.getSelectedItem().toString();
stmt.executeUpdate(sql);
conn.close();
JOptionPane.showMessageDialog(null,recdeleted, information,
JOptionPane.INFORMATION_MESSAGE);
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage() + ex.toString(),genexception,
JOptionPane.INFORMATION_MESSAGE);
}
}
//--------------------End deleteRecord()---------------------------------------->>>
////////////////////////////////////////////////////////////////////////////////
//--------------------Start insertRecord()-------------------------------------->>>
// Insert personnel details.
void insertRecord(){
try{
// load MS Access driver
Class.forName(driver);
// Setup connection to DBMS
Connection conn = DriverManager.getConnection(url);
// Create statement
Statement stmt = conn.createStatement();
sql = "INSERT INTO "+
table2+
" ("+
"FirstName,"+
"LastName,"+
"Addresses,"+
"City,"+
"State,"+
"Postcode,"+
"Country,"+
"EmailAddress,"+
"HomePhone,"+
"FaxNumber" +") VALUES ('"+
tfFirstName3.getText()+
"','"+
tfLastName3.getText()+
"','"+
tfAddress3.getText()+
"','"+
tfCity3.getText()+
"','"+
tfState3.getText()+
"','"+
tfHomeNumber3.getText()+
"','"+
tfFaxNumber3.getText()+
"','"+
tfEmailAddress3.getText()+
"')";
stmt.executeUpdate(sql);
conn.close();
JOptionPane.showMessageDialog(null,recinserted, information,
JOptionPane.INFORMATION_MESSAGE);
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage() + ex.toString(),genexception,
JOptionPane.INFORMATION_MESSAGE);
}
}
//--------------------End insertRecord()---------------------------------------->>>
////////////////////////////////////////////////////////////////////////////////
//--------------------Start selectRecord()-------------------------------------->>>
// Read personnel table based on the selected item from the Combobox Name.
void selectRecord(long person_id){
try{
// select person details based on primary key person_id
sql = "SELECT * FROM "+table2+" WHERE person_id = "+person_id;
// load MS Access driver
Class.forName(driver);
}catch (ClassNotFoundException x){
JOptionPane.showMessageDialog(null, x.getMessage(), "ClassNotFoundException",
JOptionPane.PLAIN_MESSAGE);
}
try{
// Setup connection to DBMS
Connection conn = DriverManager.getConnection(url);
// Create statement
Statement stmt = conn.createStatement();
// execute sql statement
boolean hasResults = stmt.execute(sql);
if(hasResults){
ResultSet rs = stmt.getResultSet();
displayPersonDetails(rs);
}
else {
// no record found
JOptionPane.showMessageDialog(null, norecfound, information,
JOptionPane.INFORMATION_MESSAGE);
}
conn.close();
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage(),genexception,
JOptionPane.PLAIN_MESSAGE);
}
}
//--------------------End selectRecord()---------------------------------------->>>
////////////////////////////////////////////////////////////////////////////////
//--------------------Start displayPersonDetails()------------------------------>>>
// Populate details screen with data from personnel database.
void displayPersonDetails(ResultSet rs)throws SQLException{
while (rs.next()){
// display details
switch(pane_number){
case
1:// inquiry screen
tfFirstName1.setText (rs.getString("FirstName" ));
tfLastName1.setText (rs.getString("LastName" ));
tfAddress1.setText (rs.getString("Addrress" ));
tfCity1.setText(rs.getString("City"));
tfState1.setText(rs.getString("State"));
tfPostcode1.setText (rs.getString("Postcode" ));
tfCountry1.setText (rs.getString("Country" ));
tfEmailAddress1.setText (rs.getString("EmailAddress" ));
tfHomeNumber1.setText (rs.getString("HomePhone" ));
tfFaxNumber1.setText (rs.getString("FaxNumber" ));
break;
case
2:// update/delete screen
tfFirstName2.setText (rs.getString("FirstName" ));
tfLastName2.setText (rs.getString("LastName" ));
tfAddress2.setText (rs.getString("Addrress" ));
tfCity2.setText (rs.getString("City" ));
tfState2.setText (rs.getString("State" ));
tfPostcode2.setText (rs.getString("Postcode"));
tfCountry2.setText (rs.getString("Country"));
tfEmailAddress2.setText (rs.getString("EmailAddress" ));
tfHomeNumber2.setText (rs.getString("HomePhone" ));
tfFaxNumber2.setText (rs.getString("FaxNumber" ));
}
}
}
//--------------------End displayPersonDetails()-------------------------------->>>
////////////////////////////////////////////////////////////////////////////////
//--------------------Start clearDetails()-------------------------------------->>>
// Reset screen input fields and initialize comboboxes.
void clearDetails(){
switch(pane_number){
case 1:
// clear date, gender and marital status comboboxes
//clearComboBoxes();
// clear search combobox
cbName1.removeAllItems();
cbPersonId1.removeAllItems();
// initialize search combobox
cbName1.addItem("Choose one:");
cbPersonId1.addItem("0");
cbName1.setSelectedItem("Choose one:");
// clear input fields
tfFirstName1.setText ("");
tfLastName1.setText ("");
tfAddress1.setText ("");
tfCity1.setText ("");
tfState1.setText ("");
tfPostcode1.setText ("");
tfCountry1.setText("");
tfEmailAddress1.setText("");
tfFaxNumber1.setText ("");
tfHomeNumber1.setText ("");
break;
case 2:
// clear date, gender and marital status comboboxes
//clearComboBoxes();
// clear search combobox
cbName2.removeAllItems();
cbPersonId2.removeAllItems();
// initialize search combobox
cbName2.addItem("Choose one:");
cbPersonId2.addItem("0");
cbName2.setSelectedItem("Choose one:");
// clear input fields
tfFirstName2.setText ("");
tfLastName2.setText ("");
tfAddress2.setText ("");
tfCity2.setText ("");
tfState2.setText ("");
tfPostcode2.setText ("");
tfCountry2.setText("");
tfEmailAddress2.setText("");
tfFaxNumber2.setText ("");
tfHomeNumber2.setText ("");
break;
case 3:
tfFirstName3.setText ("");
tfLastName3.setText ("");
tfAddress3.setText ("");
tfCity3.setText ("");
tfState3.setText ("");
tfPostcode3.setText ("");
tfCountry3.setText("");
tfEmailAddress3.setText("");
tfFaxNumber3.setText ("");
tfHomeNumber3.setText ("");
}
//--------------------End clearDetails()---------------------------------------->>>
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//--------------------Start ButtonHandler--------------------------------------->>>
// Action listener for the buttons on each screen
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
// get the label of the button
String action = e.getActionCommand();
if (action!=null){
if(action==login){
// call method accessDB()
login();
// if login error, set cursor to user name input field
tfUser.requestFocus();
}
else if (action==insert){
insertRecord();
// refresh list of names in combo box search key
// of inquiry screen
if (abort){
// error found in insert
abort = false;
return;
}
else{
pane_number = 1;
clearDetails();
accessDBInit();
// refresh list of names in combo box search key of
// update/delete screen
pane_number = 2;
clearDetails();
accessDBInit();
tfFirstName3.requestFocus();
}
}
else if (action==update){
updateRecord();
if (abort){
// error found in update
abort = false;
return;
}
else{
// refresh inquiry screen
pane_number = 1;
clearDetails();
accessDBInit();
tfFirstName2.requestFocus();
}
}
else if (action==delete){
// ask user to confirm delete
int reply =JOptionPane.showConfirmDialog(null,
"Are you sure you want to delete?",
confdelete, JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION){
// delete confirmed
deleteRecord();
// refresh all screens
pane_number = 1;
clearDetails();
// setup inquiry search key CB
accessDBInit();
pane_number = 2;
clearDetails();
// setup update/delete search key CB
accessDBInit();
}
}
else{
// clear button - refresh insert pane
pane_number = 3;
clearDetails();
}
}
}
}
//--------------------End ButtonHandler----------------------------------------->>>
////////////////////////////////////////////////////////////////////////////////
//--------------------Start ComboBoxHandler------------------------------------->>>
// Item listener for the Comboboxes on each screen
class ComboBoxHandler implements ItemListener{
public void itemStateChanged(ItemEvent e){
if (e.getItemSelectable().equals(cbName1)){
// combobox from inquiry pane (search name)
if (cbName1.getSelectedIndex() != 0){ // index 0 = "Choose one:"
// get index clicked
int num = (cbName1.getSelectedIndex());
// get corresponding primary key for sql purposes
cbPersonId1.setSelectedIndex(num);
// convert person_id text into integer(format in DB)
long person_id = Integer.parseInt(
cbPersonId1.getSelectedItem().toString());
pane_number = 1;
// call method to execute sql and display details on screen
selectRecord(person_id);
}
}
else if (e.getItemSelectable().equals(cbName2)){
// combobox from update/delete pane (search name)
if (cbName2.getSelectedIndex() != 0){ // index 0 = "Choose one:"
// get index clicked
int num = (cbName2.getSelectedIndex());
// get corresponding primary key for sql purposes
cbPersonId2.setSelectedIndex(num);
// convert person_id text into integer(format in DB)
long person_id = Integer.parseInt(
cbPersonId2.getSelectedItem().toString());
pane_number = 2;
// call method to execute sql and display details on screen
selectRecord(person_id);
}
}
}
}
}
//--------------------End ComboBoxHandler--------------------------------------->>>
////////////////////////////////////////////////////////////////////////////////
//--------------------Start main------------------------------------------------>>>
public static void main(String args[]){
System.out.println("loading.......please wait.");
Addressbook app = new Addressbook();
app.setSize(900,385);
app.setupLoginPanel();
app.setVisible(true);
app.tfUser.requestFocus();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//--------------------End main-------------------------------------------------->>>