I have implemented a JTable and populated it using a 2D array. This is how I did it
public JTable executeTable( ) {
Object[] columns = new String[] {
"ID", "Room No", "Floor No", "CO2 Level", "Smoke Level", "Status"
};
ArrayList<FloorDetails> arrayList = clientMain.getSensors();
Object[][] data = new Object[arrayList.size()][6];
for(int i = 0; i < arrayList.size(); i++) {
data[i][0] = arrayList.get(i).getId();
data[i][1] = arrayList.get(i).getRoomNo();
data[i][2] = arrayList.get(i).getFloorNo();
data[i][3] = arrayList.get(i).getCo2Level();
data[i][4] = arrayList.get(i).getSmokeLevel();
data[i][5] = arrayList.get(i).getStatus();
}
table = new JTable(data,columns) {
@Override
public boolean isCellEditable(int row, int column) {
// TODO Auto-generated method stub
return column == 0 || column == 3 || column == 4 || column == 5 ? true : false;
}
};
frame.setTitle("Sensor Details");
return table;
}
private void initialize() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table = executeTable();
frame.getContentPane().add(table.getTableHeader(), BorderLayout.PAGE_START);
frame.getContentPane().add(table, BorderLayout.CENTER);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setTitle("Sensor Details");
}
Now I have been asked to refresh the table in every 30 seconds. Therefore, I am trying to implement the below timer to refresh the table. But what I don't understand is how to set the values (refresh the table) using a 2D array since I am getting all the values from the server in an ArrayList
public void refreshTimer() {
Timer timer = new Timer(0, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ArrayList<FloorDetails> arrayList = clientMain.getSensors();
Object[][] data = new Object[arrayList.size()][6];
for(int i = 0; i < arrayList.size(); i++) {
data[i][0] = arrayList.get(i).getId();
data[i][1] = arrayList.get(i).getRoomNo();
data[i][2] = arrayList.get(i).getFloorNo();
data[i][3] = arrayList.get(i).getCo2Level();
data[i][4] = arrayList.get(i).getSmokeLevel();
data[i][5] = arrayList.get(i).getStatus();
}
//I want to refresh the table with new data set received from the server as an array list
}
});
timer.setDelay(30000); // delay for 30 seconds
timer.start();
}
I found some examples on the internet using fireDataChanged
method. But all of them directly retrieve the data using ResultSet
. In my case, am receiving an ArrayList. How can I achieve this? Or is there any other way to do this? Thanks in advance!
Add an updateTable
method to the class with the initialize
and executeTable
methods.
public void updateTable(List<FloorDetails> arrayList) {
DefaultTableModel dataModel = new DefaultTableModel();
for (int i = 0; i < arrayList.size(); i++) {
Object[] rowData = new Object[6];
rowData[0] = arrayList.get(i).getId();
rowData[1] = arrayList.get(i).getRoomNo();
rowData[2] = arrayList.get(i).getFloorNo();
rowData[3] = arrayList.get(i).getCo2Level();
rowData[4] = arrayList.get(i).getSmokeLevel();
rowData[5] = arrayList.get(i).getStatus();
dataModel.addRow(rowData);
}
table.setModel(dataModel);
}
Pass an instance of the class to the anonymous class in your Timer
, and call the updateTable
method. Use a SwingUtilities invokeLater
method to call updateTable
so the JTable
update is done on the Event Dispatch Thread .
You may also want to modify your executeTable
method to use a DefaultTableModel
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.