Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

JTable switching

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
*****/
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have it now.
Thanks all.
-Jason
 
You may have just won ten million dollars! Or, maybe a tiny ad.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic