• Post Reply Bookmark Topic Watch Topic
  • New Topic

retrieve JList data from renderer

 
Robert Ingmarsson
Greenhorn
Posts: 27
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best forumists!

The question.

I have a problem. In a project i have a JList that contains objects rendered as JPanel items.
In each of my JPanels i have two labels with text values, the name of a person and the section
this person belongs to. My question is, how do i retrieve the label values from each panel objects in my JList?.

The code.

This is how the code for my JList renderer looks like.



Below is how the code for my JPanel class looks like. I have two label objects in it that contains two values
that are assigned through an action event in a jdialog.



Final thoughts.

How do i retrieve the name and the section values from my JList in an action event? I hope i have been somewhat clear
concerning this issue.

 
Robert Ingmarsson
Greenhorn
Posts: 27
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This problem have been solved. Thanks for your time in reading this post.

 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your design is wrong. You should NOT be storing a JPanel in the JList.

Instead you should be creating a custom object with your data properties.

Then you also create a custom renderer which uses a JPanel with labels. The rendering code will then just take the properties from the custom object and set the text on the labels.

The whole point of using renderers is to minimize the number of Swing components created. In this case you only ever have 3 components (the panel and 2 labels) whether you have 1 or 100 objects in the ListModel.

Or probably a better design might be to just use a JTable which will support separate columns of data without any custom renderers.
 
Robert Ingmarsson
Greenhorn
Posts: 27
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Your design is wrong. You should NOT be storing a JPanel in the JList.

Instead you should be creating a custom object with your data properties.

Then you also create a custom renderer which uses a JPanel with labels. The rendering code will then just take the properties from the custom object and set the text on the labels.

The whole point of using renderers is to minimize the number of Swing components created. In this case you only ever have 3 components (the panel and 2 labels) whether you have 1 or 100 objects in the ListModel.

Or probably a better design might be to just use a JTable which will support separate columns of data without any custom renderers.


Best, Mr Camick!

I have "converted" my JList into a JTable and attached an instance of a DefaultTableModel to it. I have to columns in my JTable,
one column named "Name" and the other named "Section". I am a beginner and is working with a demo applocation that is connected
to a database. How do you iterate through the rows of a DefaultTableModel and retreive the data stored in it? It is my goal to store
this data in the database through an action event. I have two of theese person tables in my application which is a simple movie database.



This is how i populate each row in my JTable.



I know i have to create arrays of strings containing the data in each row and column. But this is where i am lost. Any help on this matter would
be incredibly much appreciated.

//Robert.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you iterate through the rows of a DefaultTableModel?

Well, first go back to the post where you asked that. You'll notice that the word "DefaultTableModel" is formatted as a link. Follow that link to the API documentation where you'll find the docs for the class. That tells you what methods objects of the DefaultTableModel class has. You're probably interested in getRowCount() and getValueAt(row, column) but you don't have to use those to get the model's contents. Remember that your actual requirement was to retrieve the data stored in the table; you decided that iterating through its rows was the way to do that. But if you peruse the API documentation a bit more you'll find there's a method which returns the entire contents of the table all at once, so you might want to just use that method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!