• Post Reply Bookmark Topic Watch Topic
  • New Topic

Populate JList data from database

 
Rich Ramirez
Greenhorn
Posts: 19
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I want to assign every data displayed in jList with a data also coming from database (like IDs which is integer).

Ex table:
ID COLORNAME
=============
1 BLUE
2 RED
3 YELLOW
4 GREEN

Colors are displayed in jList, when I click 1 of them, it's corresponding ID will also be with it every time i click that color so that i can also use that ID in other class.

Did you get it? Hope you get it.

Thanks. It would be great if anyone will answer me.
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Combo Box With Hidden Data for some solutions.

I know the title says combo box, but the solution is applicable to a JList as well.
 
Rich Ramirez
Greenhorn
Posts: 19
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:See Combo Box With Hidden Data for some solutions.

I know the title says combo box, but the solution is applicable to a JList as well.


Thank you Rob! But while I'm reading what you have suggested I want you to see this image to clearly elaborate my question if you have time.
In this link: http://s11.postimg.org/bkuvtwhkz/Question_in_JAVA.png

I'm so desperate, I did ask 3 blogs already. LOLs.
Thank you!
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make a class with 2 attributes id (int) and name (String). Override the toString method to return name. Overload the constructor with 2 arguments to initialize the 2 attributes.
You can make objects of this class and add it into the JList.
Therefore the selected item of the JList will be an instance of this class. Therefore you can get the corresponding id using the object.

The link given by Rob Camick also contain these details. But no need to use exactly that code for this purpose.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not call setRenderer on your JComboBox and provide an instance of a DefaultListCellRenderer sub class where you've overridden the getListCellRendererComponent method?
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a custom renderer breaks the default functionality of a JList. You will no longer be able to select items from the list by enter the leading characters of the items in the list. The item selection when using the keyboard depends on a valid toString() implementation of the Object added to the model.
 
Rich Ramirez
Greenhorn
Posts: 19
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys!
This is very complicated for me to understand as a beginner in this language.

Here's my code for displaying data coming from the database and putting it in the jList:

For my DATABASE class:


and for my MODEL class i initialized an array object for storage that will be called in controller:


and in my CONTROLLER class I get the data in MODEL class and pass it in my VIEW class:


and here's my VIEW class:


How will I be able to add what you suggested in my basic MVC Pattern java learning style.
I did try your suggestions but I didn't get some of it.

Thanks so much Sirs.
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the JList API and follow the link to the Swing tutorial on "How to Use Lists". Your code is way too complex.

Swing components already follow a pseudo MVC design so you don't need to do anything special. That is you just create a ListModel (the model) and create the JList (the view) using the model. So when you read data from the database you load it directly to the model.

You only need to do your own MVC design when you are creating custom components.
 
Rich Ramirez
Greenhorn
Posts: 19
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
You only need to do your own MVC design when you are creating custom components.


I used MVC pattern just to make my program understandable. The code can easily be found when I want something to modify.

Actually, my solution with my question is to create an array of two rows and a number of columns, and store the fetch data in there.
(array because I don't want to close and open a connection most of the time, the program may hang)
So from the array, I will take the row_1 data and display it to jList. So when jList has been clicked, it will go look to array again and locate what is the partner of than data in row_2.
It will be like the "Row_1:Column1" is the partner of "Row_2:Column_1". I just don't know how to program it. Haha. Anyway, will study it. If it works, I'll post it here.

But is my solution ok for you?

Thanks!
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the ID coming from..?? I can't see you get it from database. If it is your own ID, how you generate it..?
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I don't think it is a very good solution. You have data stored I two places. Some of the data is in the ListModel and some of the data in in your Array.

I gave you a simple solution that keep all the data store in the ListModel which is the proper solution.
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you getting the ID from inserting order, you can get it by getSelectedIndex() + 1;.
Or else, you can make a array of your own objects, which has the name and the ID.
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or else, you can make a array of your own objects, which has the name and the ID.


No you should NOT make an array of your own objects.

The objects should be stored in the ListModel. That is the purpose of using models in Swing. The model stores the data.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!