Search code examples
javaswingjtabledefaulttablemodel

Re Use of DefaultTableModel


    DefaultTableModel dtm = new DefaultTableModel(new Object[]{"SR#", "Date", "Name", "Description", "Quantity", "Weight", "Rate", "Total",  "Balance","Paid","Net Pay"},0);
            table = new JTable();
            table.setModel(dtm); 
            JScrollPane scroller=new JScrollPane(table);

            table.setBackground(new java.awt.Color(255,226,226));
            scroller.setBounds(0,0,1335,380);
            p2.add(scroller);
                while(rs.next()){
        dtm.addRow(new Object[]{rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),rs.getString(11)});

How can I re use this code several times in my program without creating a new object every time when an event is occurred.


Solution

  • Finally I got the solution to my Problem. I just need to call 2 methods whenever I press a button before running the SQL Query. Those 2 methods are

    panel.removeAll(); and

    panel.validate();

    This erase everything on your previous panel from your screen and displays only the result which you desire to see.