如何在 Java GUI 中的每个 TreeSelectionEvent 上刷新 JTable?

How to refresh JTable on every TreeSelectionEvent in Java GUI?

我正在开发的 GUI 中有一个 JTable。当 JTree 中的特定节点被 selected 时,JTable 中的行和列被填充。当我第一次 select JTree 中的一个节点时,table 会填充值。当我 select 某个其他节点时,table 会更新,但与该节点对应的值将作为附加行添加到现有 JTable(其中包含前一个节点的值)中。我真正想要的是整个 table 被刷新,因为新值属于与节点对应的不同文件。我尝试查看不同的解决方案,例如

fireTableDataChanged()
repaint()
revalidate()

但到目前为止没有任何效果。这是执行操作的代码部分。

 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();
            }

        }

    }

谁能帮我指出错误?

使用以下代码删除所有行,然后再将行添加到 table。

    int rowCount = listTableModel.getRowCount();
    for(int i = 0; i < rowCount; i++) {
        listTableModel.removeRow(0);
    }

就是说,在这种情况下,我建议您自己编写 TableModel。此外,删除所有对 table 模型的调用。不需要它们。