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

Thread: Out of scope problem

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Out of scope problem

    hello there i have a problem where my Center is out of scope of do i make it in scope?
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
     
     
    public class textAreaTest {
    	public static void main(String [] args)	{
    		JFrame frame2 = new JFrame();
    		frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame2.setSize(new Dimension(600, 300));
    		frame2.setLocation(new Point(300, 300));
    		frame2.setLayout(new BorderLayout());
    		frame2.setTitle("Home");;
     
    		//buttons
    		JButton konti1 = new JButton();
    		konti1.setText("konti1");
    		JButton konti2 = new JButton();
    		konti2.setText("konti2");
     
     
    		//Center
    		JTextArea Center = new JTextArea(400, 500);            //this is out of scope
     
    		//East
    		JPanel East = new JPanel(new GridLayout(1,2));
    		East.add(konti1);
    		East.add(konti2);
     
     
    		//layout
    		frame2.add(Center, BorderLayout.CENTER);
    		frame2.add(East, BorderLayout.EAST);
     
    		// action center
    		konti1.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				try{Center.read(new FileReader("konti1.txt"),null);}catch(IOException ioe){}      //i call it here
    			}
    		});
     
    		konti2.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				try{Center.read(new FileReader("konti2.txt"),null);}catch(IOException ioe){}      // and here
    			}
    		});
     
    		frame2.setVisible(true);
    	}
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Out of scope problem

    It is not a scope issue as such. When you attempt to access a variable in an outer class from within an inner class it needs to be declared as final. I bet the error message said something to that effect.
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    lf2killer (November 21st, 2012)

  4. #3
    Member
    Join Date
    Sep 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Out of scope problem

    ok thanks but what should i do it to run? make a new public class where i call this method?

    public class textAreaTest {
    public final JTextArea Center = new JTextArea(400, 500);
    	public void main(String [] args)	{
    		JFrame frame2 = new JFrame();
    		frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame2.setSize(new Dimension(600, 300));
    		frame2.setLocation(new Point(300, 300));
    		frame2.setLayout(new BorderLayout());
    		frame2.setTitle("Home");;
     
    		//buttons
    		JButton konti1 = new JButton();
    		konti1.setText("konti1");
    		JButton konti2 = new JButton();
    		konti2.setText("konti2");
     
     
     
     
    		//East
    		JPanel East = new JPanel(new GridLayout(1,2));
    		East.add(konti1);
    		East.add(konti2);
     
     
    		//layout
    		frame2.add(Center, BorderLayout.CENTER);
    		frame2.add(East, BorderLayout.EAST);
     
    		// action center
    		konti1.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				try{Center.read(new FileReader("konti1.txt"),null);}catch(IOException ioe){}
    			}
    		});
     
    		konti2.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				try{Center.read(new FileReader("konti2.txt"),null);}catch(IOException ioe){}
    			}
    		});
     
    		frame2.setVisible(true);
    	}
    }


    --- Update ---

    I got it to work
    thanks for the help

Similar Threads

  1. scope problem: append to text field declared out of class
    By chopficaro in forum Java Theory & Questions
    Replies: 2
    Last Post: July 14th, 2012, 03:21 PM
  2. Problem with scope, please help!
    By austin.rose94 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 14th, 2011, 11:11 AM
  3. Scope problem
    By TP-Oreilly in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 9th, 2011, 07:41 AM
  4. Variable Scope
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 7
    Last Post: October 5th, 2011, 04:08 PM
  5. Scope of Variables
    By PineAppleKing in forum Java Theory & Questions
    Replies: 5
    Last Post: June 11th, 2011, 10:22 AM