-1
    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.

Umair7
  • 11
  • 7
  • 2
    A comment on your code style, please use `{}` for all your blocks. It looks weird when you start all of that code with an if statement! – MyGGaN Aug 25 '15 at 16:08
  • Apologies for the style, it was too much indented, however, if you can help me with the issue it would be much appreciated. – Umair7 Aug 25 '15 at 16:14

2 Answers2

2

Seems like you have declared the JTable globally and initializing every time when some action event is triggered.

As like JTable, you can declare your DefaultTableModel globally and initialize both JTable and TableModel.

If you don't want to maintain the old records in JTable, you can clear the JTable every time when some action event is trigerred.

Loganathan Mohanraj
  • 1,736
  • 1
  • 13
  • 22
  • Thank you so much brother, for understanding my question, I really appreciate, However, I have done that as well, And I am not getting the proper out put, Even if I write the code to clear the tables even though table is not getting cleared. If you don't mind reading my program from start till end or compile and execute on your system I can show you my entire code, however, its large enough. I know somewhere I am going wrong but I am unable to track it. Please help me brother. – Umair7 Aug 31 '15 at 10:31
  • Run your program in debug mode and see what is actually going wrong. If you couldn't find it, please share your code to logumohan@gmail.com, I can check. – Loganathan Mohanraj Aug 31 '15 at 11:02
  • This is my code brother, I have updated it, Please have a look. You can run this code directly on your system, however, you need to set DSN first and u need to have an access database file with 11 columns in it and insert the records until you get the scroller. This is the main issue. Scroller is getting overlapped. Please help me brother. – Umair7 Aug 31 '15 at 12:12
  • One of my weak point is this, I have written this code on notepad each and every line is been typed by me. I have not used any CMS or other tool for it. – Umair7 Aug 31 '15 at 12:14
  • I have sent you an email, You need to do the following before you run the code to implement on your machine: 1. Set the system DSN with name MominDB.mdb (its an microsoft access file). 2. Fill up the table named as CTS ( table name is CTS) from where all the data is been fetched. 3. Fill up the table until you get the scroller down. (This is the main issue) Scroller is getting overlaped. – Umair7 Aug 31 '15 at 12:46
0

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.

Umair7
  • 11
  • 7