4 Replies - 157 Views - Last Post: 07 August 2012 - 08:53 PM Rate Topic: -----

#1 vbabey  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 30-July 12

Get names of items in JList

Posted 07 August 2012 - 07:52 PM

I created a JList which is updating automatically after each active session...
I want to get the name of the selected item in that JList to a JOptionPane.showMessageDialog();

I tried like this ...
int i = userList.getSelectedIndex();
JOPitonPane.showMessageDialog(null,"Username: "+ (userList.getModel().getElementAt(i)));




but this is not working.
Can anyone help me....

Is This A Good Question/Topic? 0
  • +

Replies To: Get names of items in JList

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,310
  • Joined: 06-March 08

Re: Get names of items in JList

Posted 07 August 2012 - 08:02 PM

View Postvbabey, on 07 August 2012 - 10:52 PM, said:

but this is not working.

which means ? Kind of vague as diagnostic... we are supposed to guess what does not work ?
Was This Post Helpful? 0
  • +
  • -

#3 vbabey  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 30-July 12

Re: Get names of items in JList

Posted 07 August 2012 - 08:11 PM

Ok then I would simply say...
I have created a JList and there are 5 items in it....
once I click one of the item and press a jButton I want to get a messageBox showing name of the selected item.
Please Help me!!!!
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • 1.21 Jiggawatts!
  • member icon

Reputation: 572
  • View blog
  • Posts: 1,752
  • Joined: 29-July 11

Re: Get names of items in JList

Posted 07 August 2012 - 08:46 PM

add a ListSelectionListener to the JList. Call the list's getSelectedIndex(), when clicked, then print the toString() of the selected index of the array which backs your jlist.
Was This Post Helpful? 2
  • +
  • -

#5 vbabey  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 30-July 12

Re: Get names of items in JList

Posted 07 August 2012 - 08:53 PM

Thanks farrell2k...It works...

This post has been edited by vbabey: 07 August 2012 - 09:00 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1