1

I have a table that represented a lot data. This is the code to created that table

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(),

                });
            }

and then, I have a jbutton to reset that data. The code is like this :

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
    }
} 

I have to do this, because my jTable is connected to a jfreechart. If the data in jtable is not clear, the jfreechart is going to be wrong because previous data still there and make a bar in my bar chart. the bar chart code like this :

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

    }

For the help, thank you so much...

user2971238
  • 95
  • 1
  • 2
  • 9

2 Answers2

4

As the comment already says, DefaultTableModel#setRowCount is the way to go.

  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);
    }
} 
Pr0gr4mm3r
  • 6,170
  • 1
  • 18
  • 23
  • I'm sorry. I'd really want to help you out with this, but I can't reproduce that behaviour on my machine. Running the provided code successfully clears the table. Maybe there is another data manipulation after the clearing? It's just an assumption as only parts of the code is posted. – Pr0gr4mm3r Jul 04 '14 at 19:28
  • Oke, thanks. I have solve this trouble. Now, everything is well. Thanks Mr.Pr0gr4mm3r... – user2971238 Jul 04 '14 at 19:40
  • @user2971238, Don't forget to accept the answer when your problem is solved so everybody knows the solution has been found. – camickr Jul 04 '14 at 20:41
0

this could help you:

   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;
    }
AvB
  • 171
  • 1
  • 2
  • 12