/* * AlterCombos.java * * Created on April 16, 2002, 11:17 AM */ package subjectData; /** * * @author Joset A. Etzel */ import subjectData.*; import java.sql.*; import java.awt.event.*; import javax.swing.*; public class AlterCombos extends javax.swing.JInternalFrame { Connection conData; Statement stmSQL; //variables for sql database access /** Creates new form AlterCombos */ public AlterCombos() { initComponents(); try { Class.forName("org.gjt.mm.mysql.Driver"); conData = SubjectData.getDatabaseConnection(); //gets connection to the correct database } catch (Exception e) { e.printStackTrace(); } FillComboBox(); //call sub to put the subject's names in the combo box } private void FillComboBox() { //fill cboCombos with the names from the database String strSQL = ""; String strNames = ""; cboCombos.removeAllItems(); //just in case cboCombos.addItem(""); //on top strSQL = "SELECT DISTINCT boxID FROM comboBoxes ORDER BY boxID"; //sql statement to execute try { stmSQL = conData.createStatement(); //stmSQL is global - have to get it ready ResultSet rssSubjects = stmSQL.executeQuery(strSQL); //make the resultset on the sql statement while(rssSubjects.next()) { cboCombos.addItem(rssSubjects.getString("boxID")); } } catch(java.sql.SQLException e) { e.printStackTrace(); } } /** 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. */ private void initComponents() {//GEN-BEGIN:initComponents java.awt.GridBagConstraints gridBagConstraints; cmdOK = new javax.swing.JButton(); cmdCancel = new javax.swing.JButton(); lblCombos = new javax.swing.JLabel(); cboCombos = new javax.swing.JComboBox(); cboCombos.addItem(""); cboCombos.addItem("Condition"); cboCombos.addItem("Education"); cboCombos.addItem("Experimenter"); cboCombos.addItem("Movies"); cmdDelete = new javax.swing.JButton(); txtAdd = new javax.swing.JTextField(); lblAdd = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); lstPresent = new javax.swing.JList(); getContentPane().setLayout(new java.awt.GridBagLayout()); setMaximizable(true); setTitle("Alter Combobox Choices"); setIconifiable(true); setResizable(true); cmdOK.setText("Add New Choice"); cmdOK.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdOKActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3); getContentPane().add(cmdOK, gridBagConstraints); cmdCancel.setText("Close"); cmdCancel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdCancelActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 3; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0); getContentPane().add(cmdCancel, gridBagConstraints); lblCombos.setText("Select the Combobox to Alter: "); getContentPane().add(lblCombos, new java.awt.GridBagConstraints()); cboCombos.setPreferredSize(new java.awt.Dimension(200, 19)); cboCombos.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { cboCombosItemStateChanged(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridwidth = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 3, 5, 0); getContentPane().add(cboCombos, gridBagConstraints); cmdDelete.setText("Delete Selected Choice"); cmdDelete.setEnabled(false); cmdDelete.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdDeleteActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3); getContentPane().add(cmdDelete, gridBagConstraints); txtAdd.setPreferredSize(new java.awt.Dimension(200, 21)); txtAdd.setMinimumSize(new java.awt.Dimension(200, 21)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0); getContentPane().add(txtAdd, gridBagConstraints); lblAdd.setText("New choice to add: "); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(10, 3, 3, 3); getContentPane().add(lblAdd, gridBagConstraints); jScrollPane1.setPreferredSize(new java.awt.Dimension(200, 80)); jScrollPane1.setMinimumSize(new java.awt.Dimension(200, 80)); lstPresent.setVisibleRowCount(4); jScrollPane1.setViewportView(lstPresent); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.gridheight = 3; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3); getContentPane().add(jScrollPane1, gridBagConstraints); pack(); }//GEN-END:initComponents private void cmdOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdOKActionPerformed //add the new choice to the database for the combobox selected String strBoxID = ""; String strBoxChoice = ""; //get the highlighted box and text; make sure not blank strBoxChoice = txtAdd.getText(); strBoxID = (String)cboCombos.getSelectedItem(); if (strBoxChoice.equals("") == true) { JOptionPane.showMessageDialog(null, "Please type the text to be shown as a choice in the box in the spot labeled New Choice.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return; } if (strBoxID.equals("") == true) { JOptionPane.showMessageDialog(null, "Please select the box you want to add this item to from the list.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return; } try { //put the new choice into comboBoxes stmSQL = conData.createStatement(); //stmSQL & conData are global stmSQL.executeUpdate("INSERT INTO comboBoxes VALUES ('" + strBoxID + "', '" + strBoxChoice + "')"); } catch(java.sql.SQLException e) { e.printStackTrace(); } txtAdd.setText(""); UpdateListBox(); }//GEN-LAST:event_cmdOKActionPerformed private void cmdDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdDeleteActionPerformed //delete the choice highlighted in lstPresent int intAnswer = 0; String strBoxID = ""; String strBoxChoice = ""; strBoxChoice = (String)lstPresent.getSelectedValue(); //get the highlighted choice strBoxID = (String)cboCombos.getSelectedItem(); //System.out.println(strBoxChoice); //if (strBoxChoice.equals("") == true | strBoxChoice.equals("null") == true) { JOptionPane.showMessageDialog(null, "Please select the choice to delete.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return; } //ask the user if they are sure they want to delete the choice intAnswer = JOptionPane.showConfirmDialog(null, "The choice " + strBoxChoice + " will be deleted from the database. Are you sure you want to continue?", "Delete Confirmation", JOptionPane.YES_NO_OPTION); if (intAnswer == JOptionPane.NO_OPTION) { return; } //exit this sub try { //delete the existing record from the movieData table stmSQL = conData.createStatement(); //stmSQL & conData are global stmSQL.executeUpdate("DELETE FROM comboBoxes WHERE boxID = \"" + strBoxID + "\" AND boxChoice = \"" + strBoxChoice + "\""); } catch(java.sql.SQLException e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "There has been an error deleting the database entry.", "Database Deletion Error", JOptionPane.ERROR_MESSAGE); return; } UpdateListBox(); //call sub to fill up the list }//GEN-LAST:event_cmdDeleteActionPerformed private void cboCombosItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboCombosItemStateChanged //after the combo's name was selected, show the items in the list if ((String)cboCombos.getSelectedItem() != "") { if (evt.getStateChange() == ItemEvent.SELECTED) { UpdateListBox(); cmdDelete.setEnabled(true); } } }//GEN-LAST:event_cboCombosItemStateChanged private void UpdateListBox() { //show the items in the list String strBoxID = ""; int intSize = 0; String strList[]; strBoxID = (String)cboCombos.getSelectedItem(); //the combo box picked try { ResultSet rssSubjects = stmSQL.executeQuery("SELECT boxChoice FROM comboBoxes WHERE boxID = \"" + strBoxID + "\" ORDER BY boxChoice"); while(rssSubjects.next()) { intSize = intSize + 1; } //count the number of rows in the resultset rssSubjects.beforeFirst(); //move to the start of the resultset again strList = new String[intSize]; //get the array ready to hold the data intSize = 0; while(rssSubjects.next()) { //get the data out of the database strList[intSize] = rssSubjects.getString("boxChoice"); //store the choice intSize = intSize + 1; //update count for next time } lstPresent.setListData(strList); //done with the result set, so add the data to the list } catch(java.sql.SQLException e) { e.printStackTrace(); } } private void cmdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelActionPerformed try { this.setClosed(true); } catch(java.beans.PropertyVetoException e) { e.printStackTrace(); } }//GEN-LAST:event_cmdCancelActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JComboBox cboCombos; private javax.swing.JButton cmdCancel; private javax.swing.JButton cmdDelete; private javax.swing.JButton cmdOK; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JLabel lblAdd; private javax.swing.JLabel lblCombos; private javax.swing.JList lstPresent; private javax.swing.JTextField txtAdd; // End of variables declaration//GEN-END:variables }