Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable switching

 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Greg Brouelette
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, here is the code that I am trying to get to work correctly. Please advise.
Thanks!
/*******
TableTest.java
*******/
/*****
JTableModel
*****/
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try switching your data1 to data2 for table2.
 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have it now.
Thanks all.
-Jason
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic