I have a JTable in the GUI I am developing. The rows and columns in the JTable get filled in when a specific node in the JTree is selected. When I select a node in the JTree for the first time, then the table gets filled in with values. When I select some other node, the table is updated, but the values corresponding to that node are added as additional rows in the existing JTable (which contains values from the previous node). What I really want is the whole table is refreshed, since the new values belong to a different file corresponding to the node. I tried looking at different solutions like
fireTableDataChanged()
repaint()
revalidate()
but nothing has worked so far. Here is the part of my code where the action is performed.
private static final Object[][] rowData = {};
private static final Object[] columnNames = {"m/z","Intensity"};
private static DefaultTableModel listTableModel = new DefaultTableModel(rowData,columnNames){
//make the cells in the JTable non-editable
@Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
};
private static final JTable spectralDataTable = new JTable(listTableModel);
private JPanel spectralContent;
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == OpenFileButton) {
int returnVal = fc.showOpenDialog(GUIMain.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
File[] filesInDirectory = file.listFiles();
SortFile sf = new SortFile();
// Calls sortByNumber method in class SortFile to list the files number wise
filesInDirectory = sf.sortByNumber(filesInDirectory);
tree = new JTree(addNodes(null, filesInDirectory, file));
// Add a listener
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e
.getPath().getLastPathComponent();
System.out.println("You selected " + node);
ReadFile rf = new ReadFile();
System.out.println(node.getParent().toString());
ArrayList<String> fileDataOfSelectedNode = rf.fileInput(node.getParent().toString() + "/" + node.toString());
// Call the listTo Array Method which converts the list containing coordinates + m/z and int columns to string array
InputData inputDataObject = new InputData();
String[] fileDataArrayI = inputDataObject.listToArray(fileDataOfSelectedNode);
float[] mzArray = new float[fileDataArrayI.length];
float[] intensityArray = new float[fileDataArrayI.length];
float[] mzList = new float[fileDataArrayI.length];
RawDataStorage dsr;
dsr = inputDataObject.extractMzIntensity(fileDataArrayI, mzArray, intensityArray, node.toString());
for(int i = 0; i<dsr.mzArray.length;i++)
{
System.out.println(dsr.mzArray[i]);
listTableModel.addRow(new Object[]{dsr.mzArray[i], dsr.intensityArray[i]});
}
// listTableModel.fireTableDataChanged();
// spectralDataTable.repaint();
listTableModel.fireTableDataChanged();
spectralDataTable.getModel().addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
spectralDataTable.repaint();
spectralDataTable.revalidate();
}
});
}
});
System.out.println("After the loops");
spectralDataTable.setCellEditor(null);
spectralDataTable.getTableHeader().setFont(new Font("SansSerif", Font.BOLD, 12));
spectralDataTable.setFocusable(false);
spectralDataTable.setRowSelectionAllowed(true);
spectralDataScrollPane = new JScrollPane(spectralDataTable);
spectralDataScrollPane.setPreferredSize(new Dimension(170, 250));
spectralDataScrollPane.setVisible(true);
spectralContent.add(spectralDataScrollPane);
spectralContent.validate();
spectralContent.repaint();
spectralFilesScrollPane = new JScrollPane();
spectralFilesScrollPane.getViewport().add(tree);
spectralFilesScrollPane.setPreferredSize(new Dimension(290, 465));
content.add(spectralFilesScrollPane);
// // content.invalidate();
content.validate();
content.repaint();
}
}
}
Can someone help me to point out the error?
Use the following code to remove all the rows, before you add rows to the table.
int rowCount = listTableModel.getRowCount();
for(int i = 0; i < rowCount; i++) {
listTableModel.removeRow(0);
}
That said, in this case I suggest you write your own TableModel
. And also, remove all that calls on the table model. They are not needed.