Hi everyone!. I have a problem with the table (JTable). I created a table model:
and develop JTable from this model:import java.util.ArrayList; import javax.swing.table.AbstractTableModel; @SuppressWarnings("serial") public class MyListTableModel extends AbstractTableModel { private ArrayList<String> columnNames = new ArrayList<String>(); private ArrayList<ArrayList<Object>> data = new ArrayList<ArrayList<Object>>(); public MyListTableModel(ArrayList<ArrayList<Object>> data, ArrayList<String> columnNames) { this.columnNames = columnNames; this.data = data; fireTableDataChanged(); } @SuppressWarnings({ "unchecked", "rawtypes" }) public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } public int getColumnCount() { return columnNames.size(); } public String getColumnName(int col) { return columnNames.get(col); } public int getRowCount() { synchronized (data) { return data.size(); } } public Object getValueAt(int row, int col) { synchronized (data) { return data.get(row).get(col); } } public boolean isCellEditable(int row, int col) { return false; } public void setValueAt(String obj, int row, int col) { synchronized (data) { data.get(row).set(col, obj); fireTableDataChanged(); } } }
private class CalcTableUI extends JScrollPane { CalcTableUI() throws Exception { calcTbl = new JTable(new MyListTableModel(createArrayListData(SQLQueriesBank.getCalcPosting), createArrayListColumns(SQLQueries.getPostTitle))); setViewportView(calcTbl); revalidate(); } }
public static ArrayList<ArrayList<Object>> createArrayListData(String sqlQuery) throws Exception { Connection conn = getConnectToMySQLDB(); ArrayList<ArrayList<Object>> dataArLst = new ArrayList<ArrayList<Object>>(); try { PreparedStatement st = conn.prepareStatement(sqlQuery); ResultSet rs = st.executeQuery(); ResultSetMetaData md = rs.getMetaData(); int columnCount = md.getColumnCount(); while (rs.next()) { ArrayList<Object> rowArr = new ArrayList<Object>(columnCount); for (int i = 1; i <= columnCount; i++) { rowArr.add(rs.getString(i)); } rowArr.trimToSize(); dataArLst.add(rowArr); } dataArLst.trimToSize(); } catch (Exception e) { JOptionPane.showMessageDialog(new JDialog(), e); } finally { if (conn != null) conn.close(); } return dataArLst; }but when i changing DB tables its not effect to the my CalcTableUI, when i rerun my app, its shows changing, why. where i wrong and/or what is missing ?public static ArrayList<String> createArrayListColumns(String sqlQuery) throws Exception { Connection conn = getConnectToMySQLDB(); ArrayList<String> columnsArr = new ArrayList<String>(); try { PreparedStatement st = conn.prepareStatement(sqlQuery); ResultSet rs = st.executeQuery(); while (rs.next()) { columnsArr.add(rs.getString(1)); } columnsArr.trimToSize(); } catch (Exception e) { JOptionPane.showMessageDialog(new JDialog(), e); } finally { if (conn != null) conn.close(); } return columnsArr; }