如何使用jbutton清除jtable中的数据

时间:2014-07-04 18:41:31

标签: java swing jtable jbutton jfreechart

我有一张代表大量数据的表格。这是创建该表的代码

DefaultTableModel model = (DefaultTableModel) Main_Menu.jTable3.getModel(); //Main menu is my form



            List<ReportCalculateSimiliarity> theListRCS = new ArrayList<ReportCalculateSimiliarity>();
            ReportCalculateSimiliarity rcs = new ReportCalculateSimiliarity();

            rcs.setNameOfMainFile(name);
            rcs.setNameOfComparingFile(enemy);
            rcs.setLevenstheins(resultOfLevenstheins);
            rcs.setSmithWaterman(resultOfSmithWaterman);

            theListRCS.add(rcs);


            for (ReportCalculateSimiliarity reportCalculateSimiliarity : theListRCS) {

                model.addRow(new Object[]{
                    reportCalculateSimiliarity.getMainFile(),
                    reportCalculateSimiliarity.getComparingFile(),
                    reportCalculateSimiliarity.getLevenstheins(),
                    reportCalculateSimiliarity.getSmithWaterman(),

                });
            }

然后,我有一个jbutton来重置该数据。代码是这样的:

private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {                                          
    int confirm = JOptionPane.showConfirmDialog(null, "clear ?", "end session", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    if (confirm == JOptionPane.YES_OPTION) {
        //this is to clear that jtable
    }
} 

我必须这样做,因为我的jTable连接到jfreechart。如果jtable中的数据不清楚,jfreechart将会出错,因为之前的数据仍然存在,并在条形图中显示条形图。条形码代码如下:

public final CategoryDataset createDataset() {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    dataset.clear();

    String algoritma1 = "Levensthein";
    String algoritma2 = "SmithWaterman";

    for (int i = 0; i < Main_Menu.jTable3.getRowCount(); i++) {

        Object fileName = Main_Menu.jTable3.getValueAt(i, 1);
        String fileNameToString = fileName.toString();

        Object nilaiLevensthein = Main_Menu.jTable3.getValueAt(i, 2); // Ambil Nilai Persentase Levensthein
        double count1 = Double.parseDouble(nilaiLevensthein.toString());

        Object nilaiSmithWaterman = Main_Menu.jTable3.getValueAt(i, 3);
        double count2 = Double.parseDouble(nilaiSmithWaterman.toString());

        dataset.addValue(count1, algorithm1, fileNameToString);
        dataset.addValue(count2, algorithm2, fileNameToString);

    }

求助,非常感谢你......

2 个答案:

答案 0 :(得分:4)

正如评论所说,DefaultTableModel#setRowCount是要走的路。

  private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {                                          
    int confirm = JOptionPane.showConfirmDialog(null, "clear ?", "end session", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    if (confirm == JOptionPane.YES_OPTION) {
        ((DefaultTableModel) Main_Menu.jTable3.getModel()).setRowCount(0);
    }
} 

答案 1 :(得分:0)

这可以帮到你:

   private void resetTable() {
        DefaultTableModel tableModel = getNewModel();
        jTable.setModel(tableModel);
   }

   private DefaultTableModel getNewModel() {
        DefaultTableModel model;
        model = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        model.addColumn("column 1");
        model.addColumn("column 2");
        model.addColumn("column 3");
        return model;
    }