Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Add Arraylist to Jtable in another class  RSS feed

 
Luke White
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I'm trying to fill my jtable with an arraylist. The problem is the jtable is in an extended class and the arraylist in the mainGUI. Now how can I fill the jtable with the arraylist?
P.S. I'm new here.

That's the arraylist in my MainGUI


This is my jtable



Thanks in Advance!
 
Campbell Ritchie
Sheriff
Posts: 53764
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why do you have a List in the GUI at all? The List ought to be in the application; then it can be made available to the GUI classes.
 
Luke White
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to open a new GUI in the mainGUI. That new GUI is the jtable. It's a request from the task that it isn't in the class.
 
Campbell Ritchie
Sheriff
Posts: 53764
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not open a new GUI if you mean a frame. You should consider some sort of Dialog object for a second window.
You are right; the List should not be in the same class.
 
Luke White
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how can I fill the jtable with my arraylist which isn't in the same class? From MainGui to the Jtable?
 
Campbell Ritchie
Sheriff
Posts: 53764
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your app should have an API which includes access to that List, maybe like this:-Do you require the table be available at all time? In which case you can fill the table from that List in the main GUI constructor, yes. If the table is only available when you click a button or similarly, then use the action listener to populate the table and add it to a Dialog object.

The unmodifiableList method returns the List in a read‑only form, not immutable. Changes in the original List are reflected in the unmodifiable version returned. I cannot remember enough about tables to know whether the table will update itself or not. You can probably find that in the Java┬« Tutorials.
 
Luke White
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will open with a button.



..
 
Campbell Ritchie
Sheriff
Posts: 53764
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't call your class List; there are already two other entities called List: 1 2.
You should be able to get the List(java.util) from your application and pass it to the constructor of whatever you are calling the table, so that shouldn't be a problem.
 
Campbell Ritchie
Sheriff
Posts: 53764
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really want to create a new instance every time you click that button? Maybe you should consider creating the table once for all and setting it visible or invisible as required? Why does it have to appear and disappear at all?
 
Campbell Ritchie
Sheriff
Posts: 53764
127
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And don't use a mouse listener on buttons: use an Action Listener. In Java8 you can use a λ instead.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!