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

时间:2015-08-07 07:19:59

标签: java user-interface jtable jtree defaulttablemodel

我正在开发的GUI中有一个JTable。当选择JTree中的特定节点时,JTable中的行和列将被填充。当我第一次在JTree中选择一个节点时,表格会被填入值。当我选择其他节点时,表会更新,但对应于该节点的值将作为现有JTable中的附加行添加(其中包含前一节点的值)。我真正想要的是刷新整个表,因为新值属于与节点对应的不同文件。我试着寻找不同的解决方案,比如

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

        }

    }

有人可以帮我指出错误吗?

1 个答案:

答案 0 :(得分:1)

在向表中添加行之前,请使用以下代码删除所有行。

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

那就是说,在这种情况下,我建议你自己编写TableModel。而且,删除表模型上的所有调用。他们不需要。