File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JList get Selected Element problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JList get Selected Element problem" Watch "JList get Selected Element problem" New topic
Author

JList get Selected Element problem

siva chaitanya
Ranch Hand

Joined: Jul 05, 2011
Posts: 59
How to get the selected elements from JList . I tried everything using getSelectedValue(), getSelectedIndices() but i can't able to make it.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> but i can't able to make it.

obviously you're doing something wrong, and, just as obvious,
no-one here has a crystal ball.

we don't like playing '20 guesses', so, post a sample program of what you're doing.
i.e. just a JList (with elements) in a JScrollPane in a JFrame. Also in the frame have
a JButton with an actionListener to print out the selected element.
Ensure we can copy/paste/compile/run/observe and see the problem.
siva chaitanya
Ranch Hand

Joined: Jul 05, 2011
Posts: 59
I am taking two JLists countriesList and displayCountriesList i populated data in the countriesList and when i click one button, the selected element in the countriesList is added into the displayCountriesList
This is the actionPerformed method when the button is clicked

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632

do you already have displayCountriesList in a scrollpane showing/visible when you click the button?

if so, the above 2 lines should be just
siva chaitanya
Ranch Hand

Joined: Jul 05, 2011
Posts: 59
But i can't able to get the values from countriesList even if i try to print the value on console after clicking the button i am getting this value '[I@30186b31'
What is this value ?
When i try to print it in option pane it is saying index -1 means no item selected right
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> When i try to print it in option pane it is saying index -1 means no item selected right

more likely it means you have a fully initialised class field
JList countriesList = new JList();

then, in the constuctor, again a fully initialised *local* variable
JList countriesList = new JList();

where the local variable is the one on the screen, the class field (no elements)
is the one accessed from the button's listener

could even be the models, as above.

check your constructor, if you find something like the above, change
//JList countriesList = new JList();
to
countriesList = new JList();

> '[I@30186b31'

is generally a memory address
siva chaitanya
Ranch Hand

Joined: Jul 05, 2011
Posts: 59
Thank you so much Michael Dunn i got it, the problem is as you mentioned i have initialized JList both in constructor and in initComponents() method. Actually i am using netbeans platform application for swings so initComponents() method is default generated code in that JList is initialized so i removed the JList countriesList = new JList(); line in constructor it works.

But can you please solve another problem.

When i try to drag the window the JPanel is also moving but i want to fix the position of the JPanel is it possible ?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> When i try to drag the window the JPanel is also moving but i want to fix the position of the JPanel is it possible ?

I already answered in your other topic.

as you now mention using netbeans, you may be using the builder-generated GroupLayout.
if so, you'll probably have to change that to a more suitable layoutManager.
siva chaitanya
Ranch Hand

Joined: Jul 05, 2011
Posts: 59
But GroupLayout is the default one in Netbeans Platform Application i cant change it right now but i changed it in components creation tab i.e navigation tab . I set the layout as Border Layout
 
 
subject: JList get Selected Element problem
 
Similar Threads
JList selection to int not working
problem selecting JList item
JList
JList + Vector
how to open a file using saved on database using java code