0

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Try calling `panel.validate()` after the change is made. – Jared Apr 03 '14 at 21:52
  • Hey, I forgot to add that but I am calling panel.revalidate() and panel with table is shown. Problem is that main window still somehow holds reference to old table instead new. Also tried calling revalidate() on main window. – user3495683 Apr 03 '14 at 22:00
  • Have you tried `validate()`...`revalidate()` has never worked for me. And when you say it's holding the old reference, do you mean it's displaying the old table (not the new one) or do you mean the actual object contains the old table and not the new one? – Jared Apr 03 '14 at 22:01
  • Its displaying new ,but contains reference to old. – user3495683 Apr 03 '14 at 22:09
  • 2
    You should hold a reference to 'jpanel1' instead of using getParent().getParent().getParent(). Additionally, it looks like you set the reference to panel using the panel's great-grand-parent but then insert it as a grand-child. Compare table > ? > ? > ? to panel > jscrollpane > table. You are depending on the implementation of JScrollPane to wrap the component-to-wrap within an intermediate wrapper. I don't think this behavior is in the specs. – Charlie Apr 03 '14 at 22:18
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Apr 04 '14 at 04:40

1 Answers1

3

So here's the deal. You don't want to keep creating new tables, removing the old one and adding the one. Completely unnecessary. What you want to do to change the table data, is swap TableModels or alter the existing one. You can see more at Creating a TableModel and How to Use Tables.

Try this as a simple test (instead of removing the old table and creating a new one), just change the data/model.

Object[] colNames = { "Col 1", "Col 2", "Col 3" };
Object[][] data = { { "Data", "Data", "Data" },
                    { "Data", "Data", "Data" },
                    { "Data", "Data", "Data" } };
DefaultTableModel model = new DefaultTableModel(data, cols);
table.setModel(model);

You should see the data change automatically.


As an aside, if you really do need to swap views, instead of trying to add an remove containers, you should look into using a CardLayout that let's you swap views without having to add and remove components (which can be troublesome). You can see more at How to Use CardLayout an see a simple example here. Also you may also want to look at How to Use CardLayout with Netbeans GUI Builder


UPDATE

"so I will have to clear every change user made to table in order to match changes what would do for example empty constructor ( new JTable() )"

You can do this with the model. Look at all the methods from DefaultTableModel you can use. If you initialize your table with a DefaultTableModel

DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);

you can then use methods like

public void setColumnIdentifiers(Object[] newIdentifiers) - To change the headers

public void setRowCount(int rowCount) - set the number of rows. Use 0 to clear model.

public void addRow(Object[] rowData) - to add rows dynamically.

And also many more methods. Look at the API

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • If anything other helps I will go for this , but only problem is that this program should show users how tables works ,so I will have to clear every change user made to table in order to match changes what would do for example empty constructor ( new JTable() ) to this table. So somehow simulate creating new Table. – user3495683 Apr 03 '14 at 22:11
  • I have no idea what you mean. I can tell you is that tables are _supposed_ to work with models. If you _really_ want to show how a table works, then you _should_ be showing something about the table model. – Paul Samsotha Apr 03 '14 at 22:16
  • See my **UPDATE**. Maybe that will help. – Paul Samsotha Apr 03 '14 at 22:25