wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JTable switching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable switching" Watch "JTable switching" New topic
Author

JTable switching

Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
Hello all,
I have a JFrame that has 2 JButtons(b1 and b2) and 2 JTables. When the JFrame first comes up, JTable1 is displayed. When b2 is clicked I want Jtable2 to be displayed. Also, if b1 is clicked later, I want JTable1 to be displayed. You should be able to switch between the two tables in this manner.
The JTables are globals so they can be accessed anywhere in the class. I have 2 action listeners, one on each button. It is here where I am having the problem. I cannot seem to get the Jtables to switch. I am currently using a CardLayout on a JPanel with the 2 JTables added to the JPanel(also a global). In the listeners, I tell the CardLayout to switch to the next JTable using the next(JPanel) method.
This is just my latest attempt. I have also tried using the same Jtable and just switching the data contents with a new JTableModel using the setModel() method. I have also tried using the repaint() method of the JScrollPanel the JTable is in, and the JTable itself. I have also tried the fireTableDataChanged() method.
Nothing seems to be working. Any ideas?
Thanks!


Jason R. Kretzer<br />Software Engineer<br />System Administrator<br /><a href="http://alia.iwarp.com" target="_blank" rel="nofollow">http://alia.iwarp.com</a>
Greg Brouelette
Ranch Hand

Joined: Jan 23, 2002
Posts: 144
You may want to look into a CardLayout layout manager. You can put each JTable into a different "Card" and have your buttons pull up the appropriate card.
Think of CardLayout as a JTabbedPane without the tabs. In fact, maybe putting them into a JTabbedPane would be a good idea. Then you could just use the tabs to switch between the two tables.
Hope it helps.


For a good Prime, call:<br />29819592777931214269172453467810429868925511217482600306406141434158089
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
OK, here is the code that I am trying to get to work correctly. Please advise.
Thanks!
/*******
TableTest.java
*******/
/*****
JTableModel
*****/
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
You don't create your JTables. They are null and added to your scrollpanes.
add
table = new JTable(data1, colnames);
table2 = new Jtable(data2, colnames);
Or whatever you want to do and try it.
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
OK, here is the code with table creation BEFORE adding to the JScrollPanes.
So, any ideas why it is not switching?

[ June 03, 2002: Message edited by: Jason Kretzer ]
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Try switching your data1 to data2 for table2.
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
I have it now.
Thanks all.
-Jason
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: JTable switching
 
Similar Threads
refreshing a JTable
JTable Updating
Problem Updating Content Of JTable
Using a the selected JTable name
database application problem with jtable binding