Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JList get Selected Element problem

 
siva chaitanya
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic