import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; /** * ListPanel displays list of entries in a table and offers buttons * for adding, deleting or modifying entries in table and database. * * @author Lars Appel * @version 0.2 */ public class ListPanel extends javax.swing.JPanel { javax.swing.JFrame parentFrame; /** Initializes the Form */ public ListPanel(JFrame f) { parentFrame = f; initComponents (); } /** * The dataset this panel is to interact with. */ private MasterSet myMaster; /** * The table that we use to show the list view. */ private javax.swing.JTable listTable; /** * This customized contructor is used by the application. */ public ListPanel(JFrame parent, MasterSet master) { initComponents (); parentFrame = parent; myMaster = master; listTable = new javax.swing.JTable (); listTable.setModel (myMaster); scrollPane.setViewportView (listTable); } /** 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 FormEditor. */ private void initComponents () {//GEN-BEGIN:initComponents setLayout (new java.awt.BorderLayout ()); bottomPanel = new javax.swing.JPanel (); bottomPanel.setLayout (new java.awt.FlowLayout ()); addButton = new javax.swing.JButton (); addButton.setText ("Add"); addButton.addActionListener (new java.awt.event.ActionListener () { public void actionPerformed (java.awt.event.ActionEvent evt) { addButtonActionPerformed (evt); } } ); bottomPanel.add (addButton); modifyButton = new javax.swing.JButton (); modifyButton.setText ("View / Modify"); modifyButton.addActionListener (new java.awt.event.ActionListener () { public void actionPerformed (java.awt.event.ActionEvent evt) { modifyButtonActionPerformed (evt); } } ); bottomPanel.add (modifyButton); deleteButton = new javax.swing.JButton (); deleteButton.setText ("Delete"); deleteButton.addActionListener (new java.awt.event.ActionListener () { public void actionPerformed (java.awt.event.ActionEvent evt) { deleteButtonActionPerformed (evt); } } ); bottomPanel.add (deleteButton); add (bottomPanel, "South"); scrollPane = new javax.swing.JScrollPane (); add (scrollPane, "Center"); }//GEN-END:initComponents private void deleteButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed // Add your handling code here: if (listTable.getSelectedRowCount() > 0) { int[] rows = listTable.getSelectedRows(); for (int k = rows.length - 1; k >= 0; k--) { String msg = myMaster.deleteEntry( rows[k], (String) listTable.getValueAt(rows[k],0) ); if (msg != null) JOptionPane.showMessageDialog(parentFrame, msg); } } else { JOptionPane.showMessageDialog(parentFrame, "No row(s) selected."); } }//GEN-LAST:event_deleteButtonActionPerformed private EntryDialog myUpdateDialog; private void modifyButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifyButtonActionPerformed // Add your handling code here: if (listTable.getSelectedRowCount() == 1) { int row = listTable.getSelectedRow(); String keyValue = (String) listTable.getValueAt(row, 0); if (myUpdateDialog == null) myUpdateDialog = new EntryDialog(parentFrame, true, myMaster, false); myUpdateDialog.fetchData(row, keyValue); myUpdateDialog.show(); } else { JOptionPane.showMessageDialog(parentFrame, "Need unique selection."); } }//GEN-LAST:event_modifyButtonActionPerformed private EntryDialog myAddDialog; private void addButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed // Add your handling code here: listTable.clearSelection(); if (myAddDialog == null) myAddDialog = new EntryDialog(parentFrame, true, myMaster, true); myAddDialog.show(); }//GEN-LAST:event_addButtonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel bottomPanel; private javax.swing.JScrollPane scrollPane; private javax.swing.JButton addButton; private javax.swing.JButton modifyButton; private javax.swing.JButton deleteButton; // End of variables declaration//GEN-END:variables }