I'm creating a database search for a collectible card/board game. I built a GUI that searches a MySQL database and returns the results to a JTable. The GUI, search, and results returned and displayed in the JTable are working perfectly. Now I'm trying to add rendering to the JTable and am running into some issues.
2 of the 21 columns in the table are ImageIcons, so I am using an AbstractTableModel. Initially, the ImageIcons are displaying properly. Here's a screenshot...
Search1.jpg
When I try to change the background and foreground of each row based on the value of column 1 ("Homeland"), the background and foreground change properly, but the ImageIcons no longer display as images, but instead display their path. Here's a screenshot...
Search2.jpg
I added an Enumeration-loop for the cell rendering. Without the loop, I get the results from the first screenshot above where the images properly appear. With the Enumeration-loop, the foreground and backgrounds properly change based on the value of column 1 ("Homeland") but the images don't appear, just their path.
public void searchSpiritDatabase(String search) { try { tableSize = 0; sqlSearchResultsTable.clear(); Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + dbName, dbUsername, dbPassword); statement = connection.createStatement(); resultset = statement.executeQuery(search); while(resultset.next()) { tableSize++; .......... sqlSearchResultsTable.add(new SpiritData(sqlType, sqlHomeland, sqlActivationCost, sqlName, sqlAttackPower, sqlAttackRange, sqlDefense, sqlMovementRange, sqlMovementCost, sqlMaintenance, sqlVision, sqlBlockingPower, sqlBlockingRange, sqlNature, sqlClass, sqlFamily, setAttribute, sqlAbilityText, sqlAbilityCost, sqlMaxPerDungeon, setIcon)); } model = new SpiritTableModel(sqlSearchResultsTable); table.setModel(model); setTableProperties(); // row height, auto resize off, sorting, column selection, column width //--------------------------------------------------------------------------------------------------------------------- Enumeration<TableColumn> en = table.getColumnModel().getColumns(); while (en.hasMoreElements()) { TableColumn tc = en.nextElement(); tc.setCellRenderer(new MyTableCellRenderer()); } //--------------------------------------------------------------------------------------------------------------------- } catch (SQLException ex) { System.out.println("SQLException: " + ex.getMessage()); System.out.println("VendorError: " + ex.getErrorCode()); } catch (ClassNotFoundException e) { e.printStackTrace(); } }
Here's the cell renderer...
class MyTableCellRenderer extends DefaultTableCellRenderer implements TableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) { Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col); int VALIDATION_COLUMN = 1; String s = table.getModel().getValueAt(row,VALIDATION_COLUMN).toString(); if(s.equalsIgnoreCase("Flatlands")) { comp.setBackground(new Color(255,255,255)); comp.setForeground(new Color(0,0,0)); } if(s.equalsIgnoreCase("Drylands")) { comp.setBackground(new Color(255,255,0)); comp.setForeground(new Color(0,0,0)); } if(s.equalsIgnoreCase("Woodlands")) { comp.setBackground(new Color(0,153,0)); comp.setForeground(new Color(255,255,255)); } else if(s.equalsIgnoreCase("Wetlands")) { comp.setBackground(new Color(0,102,255)); comp.setForeground(new Color(255,255,255)); } else if(s.equalsIgnoreCase("Highlands")) { comp.setBackground(new Color(129,39,0)); comp.setForeground(new Color(255,255,255)); } else if(s.equalsIgnoreCase("Darklands")) { comp.setBackground(new Color(0,0,0)); comp.setForeground(new Color(255,255,255)); } if(col == 3 || col == 17) { setHorizontalAlignment(JLabel.LEFT); } else { setHorizontalAlignment(JLabel.CENTER); } if(col == 17) { setFont(new java.awt.Font("Cooper", 1, 10)); } System.out.println("OBJECTVALUE: "+value+" COLUMN CLASS: "+model.getColumnClass(col)+" ROW: "+row+" COLUMN"+col ); return(comp); } }
I used system.out.println to verify that the class wasn't being changed to a string or something else. It is still showing the class of columns 16 ("Attributes") and 20 ("RS") as ImageIcon.
I'm stuck and can't figure out why the images are no longer displaying when I change the foreground and background colors. Where am I going wrong?