How to populate data from a database table into JList?
I can retrieve the data from the database table ok. No Problem but now want to populate those data into the JList. What are the code for this?
I need to know the class and methods for this.

Thanks!!!!

Member Avatar for iamthwee

How to populate data from a database table into JList?
I can retrieve the data from the database table ok. No Problem but now want to populate those data into the JList. What are the code for this?
I need to know the class and methods for this.

Thanks!!!!

Googling for the JList api would be a good start?

How to populate data from a database table into JList?
I can retrieve the data from the database table ok. No Problem but now want to populate those data into the JList. What are the code for this?
I need to know the class and methods for this.

Thanks!!!!

Well, as you can use a Vector to fill the JList, this would be one option.

I chose a slightly bigger solution. I wrote my own DatabaseTransaction class, which basically wraps query(), update() and get[Type]() functions of the JDBC interface.

Then I wrote my own TableModel and ListModel classes, providing a function which takes the transaction class and the column name of the value to be displayed as a parameter (resp. an array of column names and one of column captions for tables).

This works pretty smooth, as long as all of your values can be retreived as a string, which is making trouble with date/time fields.

Wow, that sounds very nice to me. Would be nice if you could share the code. I love reuse!!! :p

THANKS!

Well, as you can use a Vector to fill the JList, this would be one option.

I chose a slightly bigger solution. I wrote my own DatabaseTransaction class, which basically wraps query(), update() and get[Type]() functions of the JDBC interface.

Then I wrote my own TableModel and ListModel classes, providing a function which takes the transaction class and the column name of the value to be displayed as a parameter (resp. an array of column names and one of column captions for tables).

This works pretty smooth, as long as all of your values can be retreived as a string, which is making trouble with date/time fields.

Wow, that sounds very nice to me. Would be nice if you could share the code. I love reuse!!! :p

THANKS!

:) I can imagine, that you love reuse.

The library is not ready, yet. I still need to fix problems with date/time fields (autodetecting field types from tables) and kill dependencies to other librarys used in the project.

Maybe you should simply use the Vector method for now, if you don't want to write a rather big solution like mine.

I'll check if I can post some code snippets for you, to give an idea of what the classes do. But that's not only my decission!!

A JList with a default model could be used as a mutable list.

MutableList myList = new MutableList();
myList.getContents().removeAllElements();

myList.getContents().addElement(something);

import javax.swing.JList;
import javax.swing.DefaultListModel;
public class MutableList extends JList
{
    public MutableList()
    {
        super(new DefaultListModel());
    }
    
    public DefaultListModel getContents()
    {
        return (DefaultListModel)getModel();
    }
    
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.