This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTables in JTabbedPane

 
Allen Smith
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to put JTables on tabs on a JTabbedPane. Since the tables are all going to display the same type of data I want to use the same tableModel. Unfortunately, when I use the same model I get data on multiple tables.

Here is how I am setting up the tables and populating the tabs:


Populating individual tables...


Need some help understanding how to use the same model without populating multiple tables.
 
Paul Clapham
Sheriff
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the tables are all going to display the same type of data I want to use the same tableModel.
That isn't quite right. Since the tables are all going to display the same type of data you want to use the same type of tableModel. You do appear to be putting the same data into each of your table model instances, like this:The RHS of that expression doesn't depend on "i" and "columns" doesn't appear to change anywhere inside the loop.
 
Allen Smith
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are exactly right I want to use the same type of tableModel.

How do I get the same type of model??? I decided to use the array of models as a way of getting individual models, but obviously that isn't working.
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

apparently you are creating different instances. Maybe you are using only one of them, though. Or maybe you have static variables in the Transaction_Table class.

Kai
 
Allen Smith
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am pretty sure that I am using the correct instance of the table, but when I add data to a second table, the information appears in both tables.

I found this quote on "The Java Developers Almanac: 1.4"

"When you share a table model between two table components, any changes made to values in the model will appear in both table components."

I want to have multiple tables that display the same type of data. I am obviously missing an easy concept here. Has anyone done this type of thing?
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are missing a simple concept - but that's all that I can say for sure from what you have provided. A static variable might be the cause, or not using close encapsulation. For example this "columns" reference you are using could be an array or Collection which could be used directly as the data backbone by the model without making a copy. In that case all models would contain the same data.

Speculation, but that's what you asked for if I understood your request. The relevant code that causes the problem might be the constructor of the table model and its instance variables - show it to us.

The quote from the The Java Developers Almanac you provided has nothing to do with the problem, because you are just using the same model class for multiple tables, not the same instance.

Kai
[ February 18, 2006: Message edited by: Kai Witte ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic