Main window class (extended JFrame
, built with GUI editor) holds reference to JTable
object (lets call it table). Which is empty new JTable()
at beginning. And is added to JPanel
(lets call it jpanel1), which is also in main window.
User now choose from menu what he want to do - like creating new table,editing colors in table etc..
If he choose ,that he want to create new table ,then particular extended JPanel
(lets call it customJPanel1) is shown with options. customJPanel1 receives reference to table from main window. This is table we are going to work with.
Now user has show different options for different constructors of JTable
. He picks one and fills out some info ,then press button.
After that according to what he has picked new JTable
is created.
For example if he choose empty constructor ,the this happen:
table=new JTable()
in customJPanel1. As you know you just can not do this to swing components while they are shown. So I do this:
JPanel panel=(JPanel) table.getParent().getParent().getParent() // this is to get reference to jPanel1 which is holding this table.
panel.removeAll() //this is to remove old table from panel
table=new JTable(); //this is to create new table according to what user choose
JScrollPane jScrollPane=new JScrollPane(table);
panel.add(jScrollPane); // this is to add new table to panel
panel.revalidate();
panel.repaint();
This part of code works good for first time and new Table is shown. Now user select other option for example he wants to color table. So in main window I do this:
customPanel1=new ColorPanel(table);
customPanel1 should receive reference to newly created table object in this constructor ,but it does not. Main Window is still sending reference to old table object.
I would like to put here short part of codes , but I hope it will be easy to understand from my resume as my codes are little big bigger.