Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Access JLabel text in JPanel in JList  RSS feed

 
peter hammond
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hi guys

i have a JList that i have rendered with a JLabel and Icon set it on a panel and put that into an array into a JList...

all works great but is there a way to get the text in the JLabel i have created???




the renderer


i want to get the text from the new list selected item...

many thanks
 
peter hammond
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ended up doing it a dirty way by created another array to log what was selected in the first list..

but would still like to know if its possible to access a label from jpanels in a list........
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't even easy to get a JLabel from a JPanel. But why should it be easy? JPanel wasn't designed as a container for a JLabel, so there is little reason for JPanel to provide an obvious method to get a JLabel out of it. There are unobvious methods, though, but I recommend doing what you did.

Although really I recommend reviewing why you decided you needed the JPanel. I don't see a need for it, but perhaps you found that something didn't come out right if you just just used a JLabel by itself?

Actually you don't even need to use a JLabel, since your cell renderer already is (or can be) a JLabel. All your renderer code needs to do is to set the colours of the renderer appropriately.
 
Alan Mehio
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If you can post a working code; then we can help.

 
peter hammond
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i did think about ditching the panels and just labeling up the listed items but i found it was cleaner to package everything up in its individual panel then add all the panels to the list then access those panles from a master array and sub array...

it all works fine it was a question really to find out if it was possible to access these labels from within panels which i know now you cant so in future projects i will know to think of another way to display the items...

thanks for you guys help i have learnt so much here already as there are people that do reply to post

hopefully in time maybe i can help people out once i get enough practise in....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!