Results 1 to 3 of 3
  1. #1
    jDuke is offline Member
    Join Date
    Jun 2008
    Posts
    1
    Rep Power
    0

    Default Error : Help Please

    Hi there, i have this code:

    Java 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
    Java 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.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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());
    Java Code:
    class EnclosingClass {
        File[] imageFiles;  // member variable
    
        inSomeInitializingMethod() {
            openMenuItem.addActionListener(new ActionListener() {
                ...
                // Should be okay now.
                imageFiles = directory.listFiles(new ImageListFileFilter());

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. error 530 error authentication required
    By rgale in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-12-2008, 05:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •