kmart6700
asked on
Populating a JComboBox from a mdb database
I'm trying to populate a combo box from a mdb database. I feel I'm really close, but I keep getting the error " non-static variable jComboBox1 cannot be referenced from a static context" Here is my code. It errors on the line "jComboBox1.setModel(model );". I'm not that proficient in Java programming, so a low-level explanation would be appreciated :)
/*
* frmDelete.java
*
* Created on March 28, 2007, 4:25 PM
*/
/**
*
* @author ehrstein
*/
import java.sql.*;
import javax.swing.DefaultComboBo xModel;
public class frmDelete extends javax.swing.JFrame {
/** Creates new form frmDelete */
public frmDelete() {
initComponents();
}
/** 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 ">//GEN-BEGIN:initComponen ts
private void initComponents() {
jLabel13 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
btnOK = new javax.swing.JButton();
btnCancel = new javax.swing.JButton();
jComboBox1 = new javax.swing.JComboBox();
setDefaultCloseOperation(j avax.swing .WindowCon stants.EXI T_ON_CLOSE );
jLabel13.setFont(new java.awt.Font("Papyrus", 1, 24));
jLabel13.setHorizontalAlig nment(java x.swing.Sw ingConstan ts.CENTER) ;
jLabel13.setText("Delete Client");
jLabel1.setText("Please select a client to delete:");
btnOK.setText("OK");
btnOK.addActionListener(ne w java.awt.event.ActionListe ner() {
public void actionPerformed(java.awt.e vent.Actio nEvent evt) {
btnOKActionPerformed(evt);
}
});
btnCancel.setText("Cancel" );
btnCancel.addActionListene r(new java.awt.event.ActionListe ner() {
public void actionPerformed(java.awt.e vent.Actio nEvent evt) {
btnCancelActionPerformed(e vt);
}
});
org.jdesktop.layout.GroupL ayout layout = new org.jdesktop.layout.GroupL ayout(getC ontentPane ());
getContentPane().setLayout (layout);
layout.setHorizontalGroup(
layout.createParallelGroup (org.jdesk top.layout .GroupLayo ut.LEADING )
.add(layout.createSequenti alGroup()
.add(layout.createParallel Group(org. jdesktop.l ayout.Grou pLayout.LE ADING)
.add(jLabel13, org.jdesktop.layout.GroupL ayout.PREF ERRED_SIZE , 268, org.jdesktop.layout.GroupL ayout.PREF ERRED_SIZE )
.add(layout.createSequenti alGroup()
.addContainerGap()
.add(jLabel1))
.add(layout.createSequenti alGroup()
.add(48, 48, 48)
.add(btnOK, org.jdesktop.layout.GroupL ayout.PREF ERRED_SIZE , 76, org.jdesktop.layout.GroupL ayout.PREF ERRED_SIZE )
.add(26, 26, 26)
.add(btnCancel, org.jdesktop.layout.GroupL ayout.PREF ERRED_SIZE , 78, org.jdesktop.layout.GroupL ayout.PREF ERRED_SIZE ))
.add(layout.createSequenti alGroup()
.addContainerGap()
.add(jComboBox1, org.jdesktop.layout.GroupL ayout.PREF ERRED_SIZE , org.jdesktop.layout.GroupL ayout.DEFA ULT_SIZE, org.jdesktop.layout.GroupL ayout.PREF ERRED_SIZE )))
.addContainerGap(145, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup (org.jdesk top.layout .GroupLayo ut.LEADING )
.add(layout.createSequenti alGroup()
.add(jLabel13)
.addPreferredGap(org.jdesk top.layout .LayoutSty le.RELATED )
.add(jLabel1)
.addPreferredGap(org.jdesk top.layout .LayoutSty le.RELATED )
.add(jComboBox1, org.jdesktop.layout.GroupL ayout.PREF ERRED_SIZE , org.jdesktop.layout.GroupL ayout.DEFA ULT_SIZE, org.jdesktop.layout.GroupL ayout.PREF ERRED_SIZE )
.add(145, 145, 145)
.add(layout.createParallel Group(org. jdesktop.l ayout.Grou pLayout.BA SELINE)
.add(btnCancel)
.add(btnOK))
.addContainerGap(org.jdesk top.layout .GroupLayo ut.DEFAULT _SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:in itComponen ts
private void btnOKActionPerformed(java. awt.event. ActionEven t evt) {//GEN-FIRST:event_btnOKAc tionPerfor med
}//GEN-LAST:event_btnOKAct ionPerform ed
private void btnCancelActionPerformed(j ava.awt.ev ent.Action Event evt) {//GEN-FIRST:event_btnCanc elActionPe rformed
this.setVisible(false);
}//GEN-LAST:event_btnCance lActionPer formed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invoke Later(new Runnable() {
public void run() {
new frmDelete().setVisible(tru e);
try {//Start Try
//Database Path
String Db_Path = "C:\\sales.mdb";
//Create a connection
Connection conn = DriverManager.getConnectio n("jdbc:od bc:DRIVER= Microsoft Access Driver (*.mdb);Dbq=" + Db_Path);
//Made it this far? Connection Open!
System.out.println("Connec tion Open!");
//Create SQL statement container.
Statement st = conn.createStatement();
//Create SQL code/query that SQL statement container is going to run against database.
String str_SQL = "SELECT [ClientName] FROM Client";
//Create an empty ResultSet and fill it up with SQL code/query results.
ResultSet rs = st.executeQuery(str_SQL);
//Create an empty DefaultComboBoxModel. Similar to ArrayList...
DefaultComboBoxModel model = new DefaultComboBoxModel();
//While there are still records to be added...
while (rs.next()){ //Start While
//...continue to fill up DefaultComboBoxModel, converting each record found to String.
model.addElement(rs.getObj ect(1).toS tring());
}//End While
//Once there are no more records to be added,
//...add results populated into DefaultComboBoxModel to jComboBox1.
jComboBox1.setModel(model) ;
//Close ResultSet.
rs.close();
//Close Statement container.
st.close();
//Close Connection.
conn.close();
} //End Try
catch (SQLException ex) {ex.printStackTrace();}
// End Button
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variable s
private javax.swing.JButton btnCancel;
private javax.swing.JButton btnOK;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel13;
// End of variables declaration//GEN-END:varia bles
}
/*
* frmDelete.java
*
* Created on March 28, 2007, 4:25 PM
*/
/**
*
* @author ehrstein
*/
import java.sql.*;
import javax.swing.DefaultComboBo
public class frmDelete extends javax.swing.JFrame {
/** Creates new form frmDelete */
public frmDelete() {
initComponents();
}
/** 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 ">//GEN-BEGIN:initComponen
private void initComponents() {
jLabel13 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
btnOK = new javax.swing.JButton();
btnCancel = new javax.swing.JButton();
jComboBox1 = new javax.swing.JComboBox();
setDefaultCloseOperation(j
jLabel13.setFont(new java.awt.Font("Papyrus", 1, 24));
jLabel13.setHorizontalAlig
jLabel13.setText("Delete Client");
jLabel1.setText("Please select a client to delete:");
btnOK.setText("OK");
btnOK.addActionListener(ne
public void actionPerformed(java.awt.e
btnOKActionPerformed(evt);
}
});
btnCancel.setText("Cancel"
btnCancel.addActionListene
public void actionPerformed(java.awt.e
btnCancelActionPerformed(e
}
});
org.jdesktop.layout.GroupL
getContentPane().setLayout
layout.setHorizontalGroup(
layout.createParallelGroup
.add(layout.createSequenti
.add(layout.createParallel
.add(jLabel13, org.jdesktop.layout.GroupL
.add(layout.createSequenti
.addContainerGap()
.add(jLabel1))
.add(layout.createSequenti
.add(48, 48, 48)
.add(btnOK, org.jdesktop.layout.GroupL
.add(26, 26, 26)
.add(btnCancel, org.jdesktop.layout.GroupL
.add(layout.createSequenti
.addContainerGap()
.add(jComboBox1, org.jdesktop.layout.GroupL
.addContainerGap(145, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup
.add(layout.createSequenti
.add(jLabel13)
.addPreferredGap(org.jdesk
.add(jLabel1)
.addPreferredGap(org.jdesk
.add(jComboBox1, org.jdesktop.layout.GroupL
.add(145, 145, 145)
.add(layout.createParallel
.add(btnCancel)
.add(btnOK))
.addContainerGap(org.jdesk
);
pack();
}// </editor-fold>//GEN-END:in
private void btnOKActionPerformed(java.
}//GEN-LAST:event_btnOKAct
private void btnCancelActionPerformed(j
this.setVisible(false);
}//GEN-LAST:event_btnCance
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invoke
public void run() {
new frmDelete().setVisible(tru
try {//Start Try
//Database Path
String Db_Path = "C:\\sales.mdb";
//Create a connection
Connection conn = DriverManager.getConnectio
//Made it this far? Connection Open!
System.out.println("Connec
//Create SQL statement container.
Statement st = conn.createStatement();
//Create SQL code/query that SQL statement container is going to run against database.
String str_SQL = "SELECT [ClientName] FROM Client";
//Create an empty ResultSet and fill it up with SQL code/query results.
ResultSet rs = st.executeQuery(str_SQL);
//Create an empty DefaultComboBoxModel. Similar to ArrayList...
DefaultComboBoxModel model = new DefaultComboBoxModel();
//While there are still records to be added...
while (rs.next()){ //Start While
//...continue to fill up DefaultComboBoxModel, converting each record found to String.
model.addElement(rs.getObj
}//End While
//Once there are no more records to be added,
//...add results populated into DefaultComboBoxModel to jComboBox1.
jComboBox1.setModel(model)
//Close ResultSet.
rs.close();
//Close Statement container.
st.close();
//Close Connection.
conn.close();
} //End Try
catch (SQLException ex) {ex.printStackTrace();}
// End Button
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variable
private javax.swing.JButton btnCancel;
private javax.swing.JButton btnOK;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel13;
// End of variables declaration//GEN-END:varia
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.