/* * CategoryAndItemManager.java */ package grader.UI.gradebook; import grader.UI.GraderApp; import javax.swing.*; import javax.swing.table.*; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /** * * @author Tyler * Note: A portion of the code was not written by myself. This includes * The portion that deals with allowing buttons to be within a table. * Rather, I found this hack online and utilized it within this GUI prototype. * I do not take credit for that portion of the code. * The hack can be found at: http://www.java2s.com/Code/Java/Swing-Components/ButtonTableExample.htm */ public class CategoryAndItemManager extends JDialog { private JButton jButton1; private JButton jButton2; private JButton jButton3; private JButton jButton4; private JScrollPane jScrollPane1; private JTable jTable1; private java.awt.Frame frame; //Create a new Category and Item Manager public CategoryAndItemManager(){ this(new java.awt.Frame(), false); } public CategoryAndItemManager(java.awt.Frame parent, boolean modal) { super(parent, modal); setTitle("Category and Item Manager"); setResizable(false); jButton1 = new JButton(); jButton1.setText("New Category"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2 = new JButton(); jButton2.setText("New Item"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3 = new JButton(); jButton3.setText("Cancel"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jCloseActionPerformed(evt); } }); jButton4 = new JButton(); jButton4.setText("Save"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jCloseActionPerformed(evt); } }); jTable1 = new JTable(); jTable1.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {"Edit", "Category", "HW", 15, 15, "Add", "X"}, {"Edit", "Category", "Quizzes", 20, 20, "Add", "X"}, {"Edit", "Category", "Projects", 20, 20, "Add", "X"}, {"Edit", "Category", "Tests", 30, 30, "Add", "X"}, {"Edit", "Item", "Participation", 10, 10, null, "X"}, {"Edit", "Item", "Brownie Points", 5, 5, null, "X"} }, new String [] { "Edit", "Type", "Name", "Weight", "Overal Weights", "Subs", "Delete" } ) { Class[] types = new Class [] { java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class }; boolean[] canEdit = new boolean [] { true, false, false, true, false, true, true }; public Class getColumnClass(int columnIndex) { return types [columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit [columnIndex]; } }); //Place buttons in the 3 different columns jTable1.getColumn("Edit").setCellRenderer(new ButtonRenderer()); jTable1.getColumn("Edit").setCellEditor(new ButtonEditor(new JCheckBox())); jTable1.getColumn("Subs").setCellRenderer(new ButtonRenderer()); jTable1.getColumn("Subs").setCellEditor(new ButtonEditor(new JCheckBox())); jTable1.getColumn("Delete").setCellRenderer(new ButtonRenderer()); jTable1.getColumn("Delete").setCellEditor(new ButtonEditor(new JCheckBox())); jTable1.setRowHeight(32); jTable1.setRowSelectionAllowed(false); jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jScrollPane1 = new JScrollPane(); jScrollPane1.setViewportView(jTable1); //Organize the GUI layout GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(jButton1) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton2) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 206, Short.MAX_VALUE) .addComponent(jButton4) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton3))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE) .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(jButton3) .addComponent(jButton4) .addComponent(jButton1) .addComponent(jButton2)) .addContainerGap()) ); pack(); } private void jButton1ActionPerformed(ActionEvent evt) { AddCategory ac = new AddCategory(GraderApp.getApplication().getMainFrame(), true); ac.show(); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { AddItem ai = new AddItem(GraderApp.getApplication().getMainFrame(), true); ai.show(); } private void jCloseActionPerformed(java.awt.event.ActionEvent evt) { this.hide(); this.dispose(); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new CategoryAndItemManager(new java.awt.Frame(), false).setVisible(true); } }); } } //These classes are used to place buttons within the table //This is somewhat of a hack. //Note: I did not write this code. Rather, I found this hack online //and utilized it within this GUI prototype class ButtonRenderer extends JButton implements TableCellRenderer { public ButtonRenderer() { setOpaque(true); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (isSelected) { setForeground(table.getSelectionForeground()); setBackground(table.getSelectionBackground()); } else { if(value != null && !((String)value).isEmpty()) { setForeground(table.getForeground()); setBackground(UIManager.getColor("Button.background")); } else { setForeground(java.awt.Color.WHITE); setBackground(java.awt.Color.WHITE); } } setText((value == null) ? "" : value.toString()); return this; } } class ButtonEditor extends DefaultCellEditor { protected JButton button; protected int row; protected JTable table; private String label; private boolean isPushed; public ButtonEditor(JCheckBox checkBox) { super(checkBox); button = new JButton(); button.setOpaque(true); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { fireEditingStopped(); } }); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { this.table = table; this.row = row; if (isSelected) { button.setForeground(table.getSelectionForeground()); button.setBackground(table.getSelectionBackground()); } else { button.setForeground(table.getForeground()); button.setBackground(table.getBackground()); } label = (value == null) ? "" : value.toString(); isPushed = true; return button; } public Object getCellEditorValue() { if (isPushed && !label.isEmpty()) { if(label.equals("Edit")) { if(table.getValueAt(row, 1).equals("Category")) { EditCategory ec = new EditCategory(GraderApp.getApplication().getMainFrame(), true); ec.jTextArea1.setText("This category is very important for the total grade."); ec.jTextField1.setText((String)table.getValueAt(row, 2)); ec.jTextField2.setText("45"); ec.setLocationRelativeTo(GraderApp.getApplication().getMainFrame()); GraderApp.getApplication().show(ec); // ec.show(); } else if(table.getValueAt(row, 1).equals("Item")) { EditItem ei = new EditItem(GraderApp.getApplication().getMainFrame(), true); ei.jTextArea1.setText("This item is a small part of the student's grade"); ei.jTextField1.setText((String)table.getValueAt(row, 2)); ei.setLocationRelativeTo(GraderApp.getApplication().getMainFrame()); GraderApp.getApplication().show(ei); // ei.show(); } } else if (label.equals("Add")) { System.out.println("Add pressed"); } else if (label.equals("X")) { System.out.println("Delete pressed"); } } isPushed = false; return new String(label); } public boolean stopCellEditing() { isPushed = false; return super.stopCellEditing(); } protected void fireEditingStopped() { super.fireEditingStopped(); } }