-1

I've created jTable as per below:

public void refTable(String jobNo) {
        Report rp = new Report();
        final String noJob = jobNo;    
        Map<Integer, String> jMap = rp.getReportInfo(jobNo);
        Map<Integer, String> sortedMap = new TreeMap<Integer, String>(jMap);    
        String[] row = new String[sortedMap.size()];
        Integer[] no = new Integer[sortedMap.size()];
        String[] stat = new String[sortedMap.size()];
        Boolean[] dev = new Boolean[sortedMap.size()];
        String[] remark = new String[sortedMap.size()];
        Boolean[] rem = new Boolean[sortedMap.size()];
        String userRemark[] = new String[sortedMap.size()];
        tabSize = sortedMap.size();
        int i = 0;

        for (Integer key : sortedMap.keySet()) {
            no[i] = key;
             String []val = sortedMap.get(key).split("###");
            if (val[0].trim().equals("DEV")) {
                stat[i] = "FAIL";
            } else {
                stat[i] = val[0].trim();
            }

             String []strRemark = val[1].split("No");
             //tempRemark = strRemark[0];
             RemarkDropDownList.devTempValue =val[1].split("No");

            row[i] = strRemark[0].trim().replaceAll("Yes", "");//
            //row[i] = val[1].trim();
            if(strRemark.length<2)
            dev[i] = false;
            else
            dev[i] = true;    
            remark[i] = "";
            if(strRemark.length<2)
            userRemark[i] = "";
            else
                if(RemarkDropDownList.userOthersReamrk!=null)
            userRemark[i] = RemarkDropDownList.userSelectedItem;   
            else
              userRemark[i] = strRemark[1];      
            //remark[i] = false;
           /*if(userRemark1[i]!=null)
            userRemark[i] = userRemark1[i];//RemarkDropDownList.userOthersReamrk;
            else
            userRemark[i] ="";   Use when drop down*/
            rem[i] = false;
            i++;
        }

        DefaultTableModel model = new DefaultTableModel();
        model.fireTableDataChanged();
        jTable1.setModel(model);    
        model.addColumn("No:", no);
        model.addColumn("Status:", stat);
        model.addColumn("Details:", row);
        model.addColumn("Non-Deviation", dev);
        model.addColumn("Remarks", remark);
        model.addColumn("Remove", rem);
        model.addColumn("UR", testRemark);

        TableColumn col1 = jTable1.getColumnModel().getColumn(0);
        col1.setPreferredWidth(30);

        TableColumn col2 = jTable1.getColumnModel().getColumn(1);
        col2.setPreferredWidth(30);

        TableColumn col3 = jTable1.getColumnModel().getColumn(2);
        TextRenderer renderer = new TextRenderer();
        col3.setCellRenderer(renderer);
        col3.setPreferredWidth(350);

        CellRenderer cellRender = new CellRenderer();
        TableColumn col4 = jTable1.getColumnModel().getColumn(3);
        col4.setCellEditor(jTable1.getDefaultEditor(Boolean.class));
        col4.setCellRenderer(cellRender);
        col4.setPreferredWidth(50);

        TableButton buttonEditor = new TableButton("Button");
        buttonEditor.addTableButtonListener(new TableButtonListener() {
            //@Override
            public void tableButtonClicked(int row, int col) {
                RemarkDropDownList rmk = new RemarkDropDownList(noJob, row);
            }
        });
        TableColumn col5 = jTable1.getColumnModel().getColumn(4);
        col5.setCellRenderer(buttonEditor);
        col5.setCellEditor(buttonEditor);  
        TableColumn col6 = jTable1.getColumnModel().getColumn(5);
        col6.setCellEditor(jTable1.getDefaultEditor(Boolean.class));
        col6.setCellRenderer(jTable1.getDefaultRenderer(Boolean.class));
        col6.setPreferredWidth(50);
        jTable1.setShowGrid(true);
        jTable1.setGridColor(Color.BLACK);
        jTable1.setAutoCreateRowSorter(true);
       }

I tried to clear jTable from different function in same class like this.

public void clear()
{
jTable1.setModel(new DefaultTableModel());
}

The jTable is not cleared as per expected. When i try to move jTable1.setModel(new DefaultTableModel()); to main function refTable() the jTable cleared. To add on, I not only able to clear the jTable, it looks like I don't have access to jTable at all.Please advice.

braX
  • 11,506
  • 5
  • 20
  • 33
chinna_82
  • 6,353
  • 17
  • 79
  • 134

1 Answers1

1

This will clear the table for you. You must first create the new default table model, then set it, and finally set the row count to 0.

public void clear(){

 DefaultTableModel tm=new DefaultTableModel();
 jTable1.setModel(tm);
 tm.setRowCount(0);
}

Hope this helped.

ghoulfolk
  • 326
  • 7
  • 17
  • nope..not helping..its looks like I cannot do any changes in Jtable – chinna_82 Aug 20 '13 at 08:43
  • @chinna_82 quite good direction +1, but nobody knows from your code posted here – mKorbel Aug 20 '13 at 08:56
  • I take it you are creating the jTable1 within a method? Could it be possible for you to first make the jTable into a variable, which you would then call within the refTable method? this way, you could reference the jTable variable and make changes to it on the fly. I am meaning to create for eg. a `private jTable table = new jTable();` then use the `table` variable within the method `refTable` as well as any other methods you need to call the `table` in. – ghoulfolk Aug 20 '13 at 09:13
  • @ghoulfolk i need to clear the table to write again with new value. I need to call the clear function in separate method. – chinna_82 Aug 20 '13 at 09:16
  • 1
    the model is empty on instantiation :-) Or in other words: no need for setRowCount – kleopatra Aug 20 '13 at 10:09
  • didn't know that.. guess I've been setting it to 0 out of bad habit – ghoulfolk Aug 20 '13 at 10:20