Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: InnerClass troubles

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default InnerClass troubles

    Hey,
    Im trying to use a variable from an inner/nested class in an outer class. My code is:

    import java.awt.*;
    import javax.swing.*;
    import java.io.File;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.filechooser.FileFilter;
    public class FileChooser {
     
        public static void main(String[] args) {
            GridLayout myLayout = new GridLayout(2, 1);
            final JFrame mainFrame = new JFrame( "Yes No Dino");
            mainFrame.setSize(400,200);        
            JPanel myPanel = new JPanel();
            myPanel.setLayout(myLayout);
     
            JButton selectFile = new JButton ("Click to choose your file");
            JButton outputFile = new JButton ("Click to open your file");
     
            final JFileChooser fc = new JFileChooser();
     
            ActionListener chooseFile = new ActionListener(){
       public void actionPerformed(ActionEvent e){
     
     
           // Show open dialog; this method does not return until the dialog is closed
           fc.showOpenDialog(mainFrame);
           File selFile = fc.getSelectedFile();
     
        }
    };
     
            System.out.println(selFile);
     
            myPanel.add(selectFile);
            selectFile.addActionListener(chooseFile);
            mainFrame.getContentPane().add(myPanel);
            mainFrame.setVisible(true);
     
     
        }
    }

    But for some reason it says "cannot find symbol- variable selFile" when i try to print it out. How do i use variables from inner classes in outer classes


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: InnerClass troubles

    Inner Classes have access to Outer Class instance varaiables. Reverse is not true, i.e., Outer class doesn't have access to inner class variables.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: InnerClass troubles

    Quote Originally Posted by geminionweb View Post
    Inner Classes have access to Outer Class instance varaiables. Reverse is not true, i.e., Outer class doesn't have access to inner class variables.
    Outer classes do have access to inner class variables, providing you have a reference to an instance of the inner class, the variable you are accessing is an instance variable with the appropriate visibility. In the above posted code, this is not the case - the selFile variable does not have scope outside the actionPerformed method.

  4. #4
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: InnerClass troubles

    So, how would i access a variable from the innerclass, or make a reference to the instance (im not quite sure what that is). Is there a method for a string that allows me to set it as something else?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: InnerClass troubles

    Quote Originally Posted by pottsiex5 View Post
    So, how would i access a variable from the innerclass, or make a reference to the instance (im not quite sure what that is). Is there a method for a string that allows me to set it as something else?
    There are quite a few ways to accomplish this, most (and the ones I would encourage) involve an Object Oriented approach. One way: have your FileChooser class extend JFrame, and use this class to create your JFrame. This allows you to have instance variables within the FileChooser class that can be set by inner classes. Your ActionListener would than be an inner class of FileChooser, in which case it has access to the FileChooser instance variables so it can set these as appropriate.

Similar Threads

  1. .setVisible() troubles
    By pottsiex5 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 5th, 2011, 03:28 AM
  2. ArrayList troubles
    By javapenguin in forum What's Wrong With My Code?
    Replies: 20
    Last Post: November 18th, 2010, 06:03 PM
  3. [SOLVED] ArrayList Troubles.
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 2nd, 2010, 10:19 AM
  4. JSP Troubles
    By sdkeslar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 12th, 2010, 02:26 PM
  5. Array Troubles
    By Leeds_Champion in forum Collections and Generics
    Replies: 6
    Last Post: October 22nd, 2009, 11:05 AM