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 6 of 6

Thread: Scope problem

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

    Default Scope problem

    import java.awt.*;
    import javax.swing.*; 
    import java.awt.event.*;
     
    class ImageViewer{
     
    	public static void main(String[]args){
     
    		createWindow("Image Viewer");
    		createIcon("Desert.jpg");
    		addIconToGraphicalComponent();
     
    	}
     
    	public static void createWindow(String windowName){
     
    		JFrame window = new JFrame(windowName);
    		window.setSize(600,400);
    		window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
    		window.setResizable(false);
    		window.setVisible(true);
     
    	}
     
    	public static void createIcon(String fileName){
     
    		ImageIcon icon = new ImageIcon(fileName);
     
    	}
     
    	public static void addIconToGraphicalComponent(){
     
    		JLabel label = new JLabel(icon);
     
    	}
     
    	public static void addGraphicalComponentToWindow(){
     
    		Container windowContentPane = window.getContentPane();
    		windowContentPane.add(label);
     
    	}
     
     
    }

    I believe I am getting scope error messages. They are all similar (3 of them) so ill just mention one of them.

    For the line "JLabel label = new JLabel(icon);" it says "cannot find symbol" and points towards the icon object. As far as i can tell it cannot find the icon object as it is created in a different method, how can I overcome this without having to put it all into one method?

    P.S, I'm not trying to do anything in particular, just "messing".


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Scope problem

    Make your create icon method return the icon objevt it creates

    Chris

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Scope problem

    And pass it as an argument to the method that needs it.

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

    Default Re: Scope problem

    I now have got rid of the scope problem, but I have another, heres the code:

    import java.awt.*;
    import javax.swing.*; 
    import java.awt.event.*;
     
    class ImageViewer{
     
    	public static void main(String[]args){
     
    		JFrame window = createWindow();
    		ImageIcon icon = createIcon("Desert.jpg");
    		JLabel label = addIconToGraphicalComponent(icon);
    		addGraphicalComponentToWindow(window, label);
    		addMenuToWindow(window);
     
     
    	}
     
    	public static JFrame createWindow(){
     
    		JFrame window = new JFrame("Image Viewer");
    		window.setSize(600,400);
    		window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
    		window.setResizable(false);
    		window.setVisible(true);
    		return window;
     
    	}
     
    	public static ImageIcon createIcon(String fileName){
     
    		ImageIcon icon = new ImageIcon(fileName);
    		return icon;
     
    	}
     
    	public static JLabel addIconToGraphicalComponent(ImageIcon icon){
     
    		JLabel label = new JLabel(icon);
    		return label;
     
    	}
     
    	public static void addGraphicalComponentToWindow(JFrame window, JLabel label){
     
    		Container windowContentPane = window.getContentPane();
    		windowContentPane.add(label);
     
    	}
     
    	public static void addMenuToWindow(JFrame window){
     
    		JMenuBar windowMenuBar = new JMenuBar();
     
    		JMenu fileMenu = new JMenu("File");
     
    		JMenuItem openItem = new JMenuItem("Open Image");
    		JMenuItem exitItem = new JMenuItem("Exit Application");
     
    		fileMenu.add(openItem);
    		fileMenu.add(exitItem);
     
    		windowMenuBar.add(fileMenu);
     
    		window.setJMenuBar(windowMenuBar);
     
     
    	}
     
     
    }

    I dont get any error, the problem is when the app is running. When i run the application the window does NOT have an image nor does it have a menu bar. But if i minimise the window then maximise it the image and menu bar then appears?

    Oh and thanks for your posts before.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Scope problem

    Do you call setVisible(true) before you add anything to the frame?
    Wait until after you have added components to it.

  6. #6
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scope problem

    weyy thanks

Similar Threads

  1. Variable Scope
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 7
    Last Post: October 5th, 2011, 04:08 PM
  2. Java Scope in future
    By Shemil in forum Member Introductions
    Replies: 2
    Last Post: July 20th, 2011, 04:45 PM
  3. Scope of Variables
    By PineAppleKing in forum Java Theory & Questions
    Replies: 5
    Last Post: June 11th, 2011, 10:22 AM
  4. scope - quick question
    By bbr201 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 28th, 2010, 08:30 AM
  5. variabe not within scope
    By brainwave in forum Java Servlet
    Replies: 0
    Last Post: April 17th, 2010, 05:51 AM