Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Swing listSelectionListener communication  RSS feed

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Swing applet which has a JList in a JScrollPane. I did a jlist.addListSelectionListener and passed it the constructor for an anonymous inner class. In this inner class listener, I want to modify the content of a JLabel which has already been added to the content pane. The JLabel was constructed with an ImageIcon
JLabelphotoLabel= new JLabel();
try
{
URL photoURL = new URL ( "http://AccessibleSailing.info
/Asenseibeepysly.jpg");
photoLabel =
new JLabel(new ImageIcon(photoURL,"ragamuffin") );
}
catch(java.net.MalformedURLException me)
{

}
The listener inner class gets a selection which is in effect a user's selection of a different photo for the JLabel. How can I accomplish this? Naturally, the inner class is not allowed access to a local variable outside of its scope.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Make the label to be a final variable...

final JLabel label = new JLabel(new ImageIcon(URL,"Icon"));
In the inner class use
label.setIcon(new ImageIcon(URL,"Icon1"));

hope this mighr solve ur problem..
Regards,
Jagan Mohan Reddy
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!