[SWING] How to get selected objects from a JList

L

loris_p

Hi. I have a JList containing several Account type items. I need to
get the selected values and pass them in a Account[] to another
function, but getSelectedValues() method returns an Object array. How
can I obtain an Account array from an Object array?
Thanks
 
M

Mark Space

loris_p said:
Hi. I have a JList containing several Account type items. I need to
get the selected values and pass them in a Account[] to another
function, but getSelectedValues() method returns an Object array. How
can I obtain an Account array from an Object array?
Thanks

I think getSelectedValues is what you are looking for.
 
R

Roedy Green

Hi. I have a JList containing several Account type items. I need to
get the selected values and pass them in a Account[] to another
function, but getSelectedValues() method returns an Object array.

You'd think JList would be generified. You will either have to use
an Object array and cast, or copy to a Account[] array with casts.
 
Joined
Nov 28, 2008
Messages
1
Reaction score
0
Some code to round up the question...

So based on what your type of data you are displaying (Vector, String, Array, etc.), you will have to re-cast and "drill" down to get your selected items. Here is some example code:

private BookEntry books[] = {
new BookEntry("The Fountainhead"),
new BookEntry("Revolution on Duck Island") }

class buttonClickListner implements ActionListener {
public void actionPerformed(ActionEvent e) {
int selected[] = booklist.getSelectedIndices( );
System.out.println("Selected elements: ");

for (int i=0; i < selected.length; i++) {
BookEntry element =
(BookEntry)booklist.getModel( ).getElementAt(selected);
System.out.println(" " + element.getTitle( ));
}
}
}

This comes late - but may benefit the future people... :flute:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top