Updating TableModel if changing data on DataBase(MySQL)
Hi everyone!. I have a problem with the table (JTable). I created a table model:
Code :
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();
}
}
}
and develop JTable from this model:
Code :
private class CalcTableUI extends JScrollPane {
CalcTableUI() throws Exception {
calcTbl = new JTable(new MyListTableModel(createArrayListData(SQLQueriesBank.getCalcPosting), createArrayListColumns(SQLQueries.getPostTitle)));
setViewportView(calcTbl);
revalidate();
}
}
Code :
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;
}
Code :
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;
}
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 ?
Re: Updating TableModel if changing data on DataBase(MySQL)
Are you saying you're updating the data stored in the model but the JTable isn't showing the change?
Re: Updating TableModel if changing data on DataBase(MySQL)
You shouldn't expect the table to change, as when you load the data into the table you have 2 copies of the data. How does one know to change when the other changes? This problem is a lot harder to solve than one might think, one solution would be to build a notification system (which in its simplest form would require an inner tier to keep track and notify all clients of a datachange, and all changes go through this tier). This is not trivial