Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-16-2008, 07:31 PM
Member
 
Join Date: Jun 2008
Posts: 1
jDuke is on a distinguished road
Error : Help Please
Hi there, i have this code:

Code:
openMenuItem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent arg0) { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int state = chooser.showOpenDialog(frame); if (state == JFileChooser.APPROVE_OPTION) { File directory = chooser.getSelectedFile(); imageFiles = directory.listFiles(new ImageListFileFilter()); for (int i = 0; i < imageFiles.length; i++) { //imageContainers.add(new Thumbnail(fullSize, i)); imageContainers.add(new JButton()); imageContainers.get(i).setIcon(new ImageIcon(new Picture(imageFiles[i], true).getPicture())); imageContainers.get(i).addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent evt) { File x = imageFiles[i]; fullSize.setIcon(new ImageIcon(new Picture(imageFiles[0], false).getPicture())); } }); panel.add(imageContainers.get(i)); } } } });
And in the variable 'i' on the line
Code:
File x = imageFiles[i];
It gives me this error:
Cannot refer to a non-final variable i inside an inner class defined in a different method

I couldn't find anywhere to solve it on the internet.
Can anyone help me please it's really important.

Thanks in advance.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-17-2008, 05:45 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
Code:
class EnclosingClass { inSomeInitializingMethod() { File[] imageFiles; // local variable openMenuItem.addActionListener(new ActionListener() { ... // Local variable accessed inside inner class must be // declared final. One option is to remove the local // variable and make it a member variable - see below. imageFiles = directory.listFiles(new ImageListFileFilter());
Code:
class EnclosingClass { File[] imageFiles; // member variable inSomeInitializingMethod() { openMenuItem.addActionListener(new ActionListener() { ... // Should be okay now. imageFiles = directory.listFiles(new ImageListFileFilter());
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-17-2008, 05:46 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,042
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Simply it says, you must use final values only within the inner class. Try to keep your variable constant.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
error 530 error authentication required rgale JavaServer Pages (JSP) and JSTL 0 05-12-2008 05:28 PM


All times are GMT +3. The time now is 09:29 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org