I'm building a database search for a CCG. I have the GUI built with ComboBoxes, TextFields and Buttons that queries a MySQL database when the user presses the search button. All of that is working and spitting the correct information back to the console. Now I'm trying to add a table to display the results to the GUI.
I used an AbstractTableModel because two of my columns (one for now) are going to display ImageIcons. The table displays correctly with all the information when I start the program and the GUI initially appears. However, I can't get the table to update when I press the search button, which I currently have doing a different search than the initial search. I have the query spitting back the information to the console and it is grabbing the correct and different data from the query, but I can't get the table to update.
I tried invoking the fireTableDataChanged() method as mentioned in the JavaDocs. I tried repainting, not creating the table initially, etc. None of it is working and I feel like there is a simple mistake or concept I am getting wrong.
public class SSCCE extends JFrame { String imageString; ImageIcon setIcon; static String dbUsername = "..."; static String dbPassword = "..."; static String dbName = "..."; static ResultSet rs; JButton buttonSearch = new JButton("Search"); JPanel panel = new JPanel(); Connection conn = null; List<SpiritData> sqlSearchResultsTable = new ArrayList<>(); //--------------------------------------------------------------------------------------------------------- public SSCCE() { panel.setLayout(new MigLayout( "debug 0, insets 15, gapy 5", "[80, right] 5 [105] 16 [80, right] 5 [105] 16" + "[80, right] 5 [60] 5 [60] 16" + "[80, right] 5 [60] 5 [60] 16" + "[80, right] 5 [105] 16 [80, right] 5 [105]", "[align center]")); try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + dbName, dbUsername, dbPassword); Statement sqlState = conn.createStatement(); String selectStuff = "SELECT * FROM swdbtest.spiritdatabase2 WHERE ActivationCost = 8"; rs = sqlState.executeQuery(selectStuff); while(rs.next()){ String sqlType = rs.getString("Type"); String sqlHomeland = rs.getString("Homeland"); String sqlName = rs.getString("Name"); String sqlReleaseSet = rs.getString("ReleaseSet"); System.out.println(sqlName); setIcon = getImage(sqlReleaseSet + ".png"); sqlSearchResultsTable.add(new SpiritData(sqlType, sqlHomeland, sqlName, setIcon)); } } catch (SQLException ex) { System.out.println("SQLException: " + ex.getMessage()); System.out.println("VendorError: " + ex.getErrorCode()); } catch (ClassNotFoundException e) { e.printStackTrace(); } SpiritTableModel model = new SpiritTableModel(sqlSearchResultsTable); JTable table = new JTable(model); panel.add(buttonSearch, "wrap"); buttonSearch.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonSearchActionPerformed(evt); //table.fireTableDataChanged(); //<---- doesn't work } }); panel.add(new JScrollPane(table), "growx, spanx"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(panel); pack(); setLocationRelativeTo(null); setVisible(true); } // -------------------------------------------------------------------------------------------------------------------- private void buttonSearchActionPerformed(ActionEvent evt) { try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + dbName, dbUsername, dbPassword); Statement sqlState = conn.createStatement(); String selectStuff = "SELECT * FROM swdbtest.spiritdatabase2 WHERE ActivationCost BETWEEN 9 AND 10"; rs = sqlState.executeQuery(selectStuff); while(rs.next()){ String sqlType = rs.getString("Type"); String sqlHomeland = rs.getString("Homeland"); String sqlName = rs.getString("Name"); String sqlReleaseSet = rs.getString("ReleaseSet"); System.out.println(sqlName); setIcon = getImage(sqlReleaseSet + ".png"); sqlSearchResultsTable.add(new SpiritData(sqlType, sqlHomeland, sqlName, setIcon)); //UPDATE TABLE ????????????????? //table.fireTableDataChanged(); //<---- doesn't work } } catch (SQLException ex) { System.out.println("SQLException: " + ex.getMessage()); System.out.println("VendorError: " + ex.getErrorCode()); } catch (ClassNotFoundException e) { e.printStackTrace(); } } // -------------------------------------------------------------------------------------------------------------------- private ImageIcon getImage(String path) { java.net.URL url = getClass().getResource(path); if (url != null) return (new ImageIcon(url)); else { return null; } } // -------------------------------------------------------------------------------------------------------------------- public class SpiritData { private String spiritType; private String spiritHomeland; private String spiritName; private ImageIcon spiritReleaseSet; public SpiritData(String spiritType, String spiritHomeland, String spiritName, ImageIcon spiritReleaseSet) { this.spiritType = spiritType; this.spiritHomeland = spiritHomeland; this.spiritName = spiritName; this.spiritReleaseSet = spiritReleaseSet; } public String getSpiritType() { return spiritType; } public String getSpiritHomeland() { return spiritHomeland; } public String getSpiritName() { return spiritName; } public ImageIcon getSpiritReleaseSet() { return spiritReleaseSet; } } // -------------------------------------------------------------------------------------------------------------------- public class SpiritTableModel extends AbstractTableModel { private List<SpiritData> spiritTable; public SpiritTableModel(List<SpiritData> spTable) { this.spiritTable = new ArrayList<>(spTable); } @Override public int getRowCount() { return spiritTable.size(); } @Override public int getColumnCount() { return 4; } @Override public String getColumnName(int column) { String name = "??"; switch (column) { case 0: name = "Type"; break; case 1: name = "Homeland"; break; case 2: name = "Name"; break; case 3: name = "Set"; break; } return name; } @Override public Class<?> getColumnClass(int columnIndex) { Class type = String.class; if(columnIndex == 3) { type = ImageIcon.class; } return type; } @Override public Object getValueAt(int rowIndex, int columnIndex) { SpiritData cellValue = spiritTable.get(rowIndex); Object value = null; switch (columnIndex) { case 0: value = cellValue.getSpiritType(); break; case 1: value = cellValue.getSpiritHomeland(); break; case 2: value = cellValue.getSpiritName(); break; case 3: value = cellValue.getSpiritReleaseSet(); break; } return value; } } // -------------------------------------------------------------------------------------------------------------------- public static void main(String[] args) { new SSCCE(); } }