package gradertool.gradebook; import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.table.*; import javax.swing.event.*; /** * GradeSpreadsheetDisplay class represents the GradeSpreadsheet GUI */ public class GradeSpreadsheetDisplay extends JFrame { /** * Statistics JTable data */ protected String[][] statistics = {{"Data Points", "15", "15", "14","15"}, {"Mean", "86.75", "96.5", "93.25", "93.25"}, {"Median", "89.5", "96", "93.5", "95"}, {"Std. Deviation", "13.5", "4.12", "5.32", "4.72"}}; /** * Main JTable column headers */ protected String[] colNames = {"Name","Student ID", "Empl ID", "Tests", "Programs", "Labs", "Total", "Grades"}; /** * Main JTable data */ protected String[][] data = {{"Cait Rahm", "crahm", "1234", "100", "100", "100", "100", "A+"}, {"Sally Miller", "smill", "1234", "91", "92", "87", "90", "A-"}, {"William Dugger", "wdugger", "1234", "88", "94", "93", "90", "A-"}, {"Doug Guastaferro", "dguastaf", "1234", "68", "100", "93", "93", "A"}, {"Steve Johnson", "sjohnson", "1234", "76", "76", "80", "77", "C"}, {"Joseph Simmons", "jsimmons", "1234", "86", "92", "87", "90", "A-"}, {"Connor Citron", "ccitron", "1234", "95", "98", "93", "95", "A"}, {"Alex Bozarth", "abozarth", "1234", "92", "96", "93", "93", "A"}, {"Richard Castle", "rcastle", "1234", "57", "70", "67", "65", "D"}, {"John Crichton", "jcricton", "1234", "90", "92", "100", "94", "A"}, {"Wash Washburns", "wwashbu", "1234", "76", "70", "67", "71", "C-"}, {"Malcolm Reynolds", "mreynol", "1234", "79", "64", "80", "74", "C"}, {"Jayne Cobb", "jcobb", "1234", "65", "48", "47", "53", "F"}, {"Kaylee Fryer", "kfrye", "1234", "86", "80", "80", "82", "B-"}, {"River Tam", "rtam", "1234", "82", "22", " ", "48", "F-"}}; /** * Statistics JTable */ JTable s = new JTable(statistics, (new String[] {"1", "2 ", "3 ", "4 ", "5 "})); /** * Panel for Statistics JTable */ JPanel sPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); /** * Constructs the display window for Grade Spreadsheet */ public GradeSpreadsheetDisplay() { JPanel panel = new JPanel(); //Detects click on header to sort column JTable table = new JTable(data, colNames) { { getTableHeader().addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent mouseEvent) { int index = convertColumnIndexToModel(columnAtPoint(mouseEvent.getPoint())); if (index >= 0) { System.out.println("Sort column " + (index + 1)); } }; }); } }; JPanel stats = new JPanel(new FlowLayout(FlowLayout.LEFT)); JScrollPane pane = new JScrollPane(table); //Detects change of a cell in the Main table table.getModel().addTableModelListener(new TableModelListener() { public void tableChanged(TableModelEvent e) { System.out.println("column:" + (e.getColumn() + 1) + " row:" + (e.getFirstRow() + 1) + " has been edited"); } }); TableCellRenderer renderer = new TableCellRenderer() { JLabel label = new JLabel(); @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { label.setOpaque(true); label.setText(" " + value); label.setFont(new Font("Tahoma", Font.LAYOUT_LEFT_TO_RIGHT, 14)); Color alternate = new Color(235, 235, 235); if (row % 2 == 1) { label.setBackground(alternate); } else { label.setBackground(Color.WHITE); } if (column == 7) { if (value.toString().contains("A")) { label.setBackground(Color.RED); } else if(value.toString().contains("B")) { label.setBackground(Color.YELLOW); } else if(value.toString().contains("C")) { label.setBackground(Color.GREEN); } else if(value.toString().contains("D")) { label.setBackground(Color.MAGENTA); } else { label.setBackground(Color.CYAN); } } return label; } }; table.setDefaultRenderer(Object.class, renderer); TableCellRenderer rendererStats = new TableCellRenderer() { JLabel label = new JLabel(); @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { label.setOpaque(true); label.setText(" " + value); label.setFont(new Font("Tahoma", Font.LAYOUT_LEFT_TO_RIGHT, 14)); if (column == 0) label.setBackground(new Color(214, 214, 214)); else label.setBackground(Color.WHITE); return label; } }; s.setDefaultRenderer(Object.class, rendererStats); pane.setPreferredSize(new Dimension(1000,263)); s.setPreferredSize(new Dimension(875, 65)); s.setEnabled(false); matchColumnSizes(s, table); panel.add(pane); pane.setVisible(true); panel.setVisible(true); JLabel lbl = new JLabel("Statistics"); lbl.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { showStatistics(); } }); stats.add(lbl); sPanel.add(s); sPanel.setVisible(false); stats.setVisible(true); stats.setBackground(Color.WHITE); stats.setBorder(BorderFactory.createLineBorder(Color.black)); panel.setBorder(BorderFactory.createLineBorder(Color.black)); Container content = getContentPane(); content.add(panel, BorderLayout.NORTH); content.add(stats); content.add(sPanel,BorderLayout.SOUTH); this.setTitle("Grade Spreadsheet - CPE 308"); this.pack(); } /** * Makes the size of the Statistics table columns match the size of the * Main tables columns */ private void matchColumnSizes(JTable stats, JTable table) { stats.getColumnModel().getColumn(0).setMinWidth(326); } /** * When Statistics button is clicked it will show or hide the Statistics * table */ private void showStatistics() { if(sPanel.isShowing()) { sPanel.setVisible(false); this.pack(); } else { sPanel.setVisible(true); this.pack(); } } }