This is the class I use to display my info..
Analyze the methods...
you will not be able to copy this and compile it because
the
java beans form file is not included
/* @author Shawn Stark
*/
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
public class contactFrame extends javax.swing.JFrame {
// curRec is used to control what record is displayed in displayDetails
//method
// also controls what record is updated in update Method in this class
// curRec should keep its aquired state throughout run of program.
// I just retrieve cursor position of resultSet
// curPage is used to control what records are preloaded from DB in
//dataLoad method in this class
// curPage is unused as of yet but has definate potential.
// I am working on an in and out type read and display process.
// Example; a 5 element array, e1 = e2, e2 = e3, e3 = e4, e4 = e5, e5
//nextRead
// initial data load would load all 5 elements and set var page = 5
//method nextRec would add 1, method
// nextpage would add 5. Though I could load one at a time it would
//probably be less efficient.
// If I had to ask my dataBase server every time I wanted another file I
//would wast effort !)
// The only time I need actual record is when editing thus record should
//be locked to all other
// edit access before editing in the system I am explaining, this should
//always be done.
// I have created a contact adaptor with methods to manipulate
//contactData database
// I add/ update and create an ID for new records based on the ID of the
//last record
// in the result set. When I create my delete I will not be changing ID
//#s of any other records.
// or I will have troubles with my joins..With the methods in my adaptor
//class one could create
// a page in page out type view structure of the data like I explain
//with curPage variable.
// JUST BECAUSE YOU DONT SEE the ID in the running program dose not meen
//I dont assign it.
// I am pondering if searching result set for a record is more
//appropriate than doing that work
// with a query.
// My next addition will be a table with loaded values and a page button.
// I will have buttons to sort by fName, ID, lName, city, state, zip
// After this is accomplished I will beginn work on result sets from
//joined tables.
// I intend to create a menu where you can select
// sales orders, purchase orders, inventory, clients, vendors, staff,
// sales and purchase orders are the views which require joins
int curRec = 0;
// int curPage = 0;
boolean recOps = true;
boolean recOps2 = true;
boolean addOp = false;
contactAdapt cntct = new contactAdapt();
Contact contactHold2 = new Contact();
Contact contactHold = new Contact();
/** Creates new form contactFrame */
public contactFrame() {
initComponents();
this.setTitle("Contact information");
// Initial data objects created and assigned to an object array
// element
// In the instance one would like to assign a large number of
//objects, one holding object and a loop would suffice.
cntct.setConnection();
addRec.setVisible(true);
edtRec.setVisible(true);
updRec2.setVisible(false);
contactHold = cntct.getFirstRec();
curRec = cntct.getCurRow();
displayDetails();
}
/**********************************************************************
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
*************************************************************************/
private void lastRecActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contactHold = cntct.getLastRec();
curRec = cntct.getCurRow();
displayDetails();
}
private void firstRecActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contactHold = cntct.getFirstRec();
curRec = cntct.getCurRow();
displayDetails();
}
private void updRec2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (addOp == true){
addRec();
addViewSwitch();
contactHold = cntct.getLastRec();
curRec = cntct.getCurRow();
addOp = false;
displayDetails();
}
else if (addOp == false){
updateRec();
editViewSwitch();
contactHold = cntct.getCurRec();
curRec = cntct.getCurRow();
displayDetails();
}
}
private void addRecActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
addViewSwitch();
}
private void updRecActionPerformed(java.awt.event.ActionEvent evt){}
private void edtRecActionPerformed(java.awt.event.ActionEvent evt) {
editViewSwitch();
}
private void addViewSwitch(){
if (recOps2 == true){
modeLbl.setText("****************Add*******************");
fNameTxtFld.setText("");
mInitTxtFld.setText("");
lNameTxtFld.setText("");
addrTxtFld.setText("");
cityTxtFld.setText("");
statecmbbx1.setSelectedIndex(1);
zipTxtFld.setText("");
statecmbbx1.setEnabled(true);
fNameTxtFld.setEditable(true);
mInitTxtFld.setEditable(true);
lNameTxtFld.setEditable(true);
addrTxtFld.setEditable(true);
cityTxtFld.setEditable(true);
zipTxtFld.setEditable(true);
lastRec.setEnabled(false);
prevRec.setEnabled(false);
nextRec.setEnabled(false);
firstRec.setEnabled(false);
updRec2.setText("Save Record");
addRec.setText("Cancel Add");
updRec2.setVisible(true);
addRec.setVisible(true);
edtRec.setVisible(false);
recOps2 = false;
addOp = true;
}
else{
modeLbl.setText("****************View*****************");
statecmbbx1.setEnabled(false);
fNameTxtFld.setEditable(false);
mInitTxtFld.setEditable(false);
lNameTxtFld.setEditable(false);
addrTxtFld.setEditable(false);
cityTxtFld.setEditable(false);
zipTxtFld.setEditable(false);
lastRec.setEnabled(true);
prevRec.setEnabled(true);
nextRec.setEnabled(true);
firstRec.setEnabled(true);
updRec2.setText("Update Record");
addRec.setText("Add Record");
addRec.setVisible(true);
edtRec.setVisible(true);
updRec2.setVisible(false);
recOps2 = true;
displayDetails();
}
}
private void editViewSwitch(){
if (recOps == true){
modeLbl.setText("****************Edit******************");
statecmbbx1.setEnabled(true);
fNameTxtFld.setEditable(true);
mInitTxtFld.setEditable(true);
lNameTxtFld.setEditable(true);
addrTxtFld.setEditable(true);
cityTxtFld.setEditable(true);
zipTxtFld.setEditable(true);
lastRec.setEnabled(false);
prevRec.setEnabled(false);
nextRec.setEnabled(false);
firstRec.setEnabled(false);
edtRec.setText("Cancel Edit");
updRec2.setVisible(true);
addRec.setVisible(false);
recOps = false;
addOp = false;
}
else{
modeLbl.setText("****************View*****************");
statecmbbx1.setEnabled(false);
fNameTxtFld.setEditable(false);
mInitTxtFld.setEditable(false);
lNameTxtFld.setEditable(false);
addrTxtFld.setEditable(false);
cityTxtFld.setEditable(false);
zipTxtFld.setEditable(false);
lastRec.setEnabled(true);
prevRec.setEnabled(true);
nextRec.setEnabled(true);
firstRec.setEnabled(true);
edtRec.setText("Edit Record");
addRec.setVisible(true);
updRec2.setVisible(false);
recOps = true;
addOp = false;
displayDetails();
}
}
private void prevRecActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (curRec > 1)
{
contactHold = cntct.getPrevRec();
curRec=cntct.getCurRow();
displayDetails();
}
}
private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void nextRecActionPerformed(java.awt.event.ActionEvent evt) {
// load next record
if (curRec < cntct.getLength())
{
contactHold = cntct.getNextRec();
curRec=cntct.getCurRow();
displayDetails();
}
}
// method to display details
private void displayDetails()
{
// picLbl.setIcon(new javax.swing.ImageIcon(contactHold.getPicture()));
String hold = Integer.toString(curRec);
jTextField2.setText(hold);
fNameTxtFld.setText(contactHold.getfName());
mInitTxtFld.setText(contactHold.getInit());
lNameTxtFld.setText(contactHold.getlName());
addrTxtFld.setText(contactHold.getAddr());
cityTxtFld.setText(contactHold.getCity());
statecmbbx1.setSelectedItem(contactHold.getState());
zipTxtFld.setText(contactHold.getZip());
}
private void updateRec()
{
contactHold2.setfName(fNameTxtFld.getText());
contactHold2.setlName(lNameTxtFld.getText());
contactHold2.setAddr(addrTxtFld.getText());
contactHold2.setInit(mInitTxtFld.getText());
contactHold2.setCity(cityTxtFld.getText());
contactHold2.setState(this.stateAbbr());
contactHold2.setZip(zipTxtFld.getText());
cntct.updateRec(contactHold2);
}
private String stateAbbr()
{
String stat;
switch(statecmbbx1.getSelectedIndex())
{
case 0: stat = "AL"; break;
case 1: stat = "AK"; break;
case 2: stat = "AS"; break;
case 3: stat = "AZ"; break;
case 4: stat = "AR"; break;
case 5: stat = "CA"; break;
case 6: stat = "CO"; break;
case 7: stat = "CT"; break;
case 8: stat = "DE"; break;
case 9: stat = "DC"; break;
case 10: stat = "FM"; break;
case 11: stat = "FL"; break;
case 12: stat = "GA"; break;
case 13: stat = "GU"; break;
case 14: stat = "HI"; break;
case 15: stat = "ID"; break;
case 16: stat = "IL"; break;
case 17: stat = "IN"; break;
case 18: stat = "IA"; break;
case 19: stat = "KS"; break;
case 20: stat = "KY"; break;
case 21: stat = "LA"; break;
case 22: stat = "ME"; break;
case 23: stat = "MH"; break;
case 24: stat = "MD"; break;
case 25: stat = "MA"; break;
case 26: stat = "MI"; break;
case 27: stat = "MN"; break;
case 28: stat = "MS"; break;
case 29: stat = "MO"; break;
case 30: stat = "MT"; break;
case 31: stat = "NE"; break;
case 32: stat = "NE"; break;
case 33: stat = "NV"; break;
case 34: stat = "NH"; break;
case 35: stat = "NJ"; break;
case 36: stat = "NM"; break;
case 37: stat = "NY"; break;
case 38: stat = "NC"; break;
case 39: stat = "ND"; break;
case 40: stat = "MP"; break;
case 41: stat = "OH"; break;
case 42: stat = "OK"; break;
case 43: stat = "OR"; break;
case 44: stat = "PW"; break;
case 45: stat = "PA"; break;
case 46: stat = "PR"; break;
case 47: stat = "RI"; break;
case 48: stat = "SC"; break;
case 49: stat = "SD"; break;
case 50: stat = "TN"; break;
case 51: stat = "TX"; break;
case 52: stat = "UT"; break;
case 53: stat = "VT"; break;
case 54: stat = "VI"; break;
case 55: stat = "VA"; break;
case 56: stat = "WA"; break;
case 57: stat = "WV"; break;
case 58: stat = "WI"; break;
case 59: stat = "WY"; break;
default: stat = "MN"; break;
}
return stat;
}
private void addRec(){
contactHold2.setfName(fNameTxtFld.getText());
contactHold2.setlName(lNameTxtFld.getText());
contactHold2.setAddr(addrTxtFld.getText());
contactHold2.setInit(mInitTxtFld.getText());
contactHold2.setCity(cityTxtFld.getText());
contactHold2.setState(this.stateAbbr());
contactHold2.setZip(zipTxtFld.getText());
cntct.addRec(contactHold2);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new contactFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton addRec;
private javax.swing.JLabel addrLbl;
private javax.swing.JTextField addrTxtFld;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JTextField cityTxtFld;
private javax.swing.JLabel ctyLbl;
private javax.swing.JButton edtRec;
private javax.swing.JLabel fNameLbl;
private javax.swing.JTextField fNameTxtFld;
private javax.swing.JButton firstRec;
private javax.swing.JDialog jDialog1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField jTextField2;
private javax.swing.JLabel lNameLbl;
private javax.swing.JTextField lNameTxtFld;
private javax.swing.JButton lastRec;
private javax.swing.JLabel mInitLbl;
private javax.swing.JTextField mInitTxtFld;
private javax.swing.JLabel modeLbl;
private javax.swing.JButton nextRec;
private javax.swing.JButton prevRec;
private javax.swing.JLabel stateLbl;
private javax.swing.JComboBox statecmbbx1;
private javax.swing.JButton updRec;
private javax.swing.JButton updRec2;
private javax.swing.JLabel zipLbl;
private javax.swing.JTextField zipTxtFld;
// End of variables declaration
}