We help IT Professionals succeed at work.

Local Variable Accessed From Inner Class

LarryAndro
LarryAndro asked
on
When I compile my project I get the following error message...

--------------------Configuration: ListFiles - JDK version 1.6.0_01 <Default> - <Default>--------------------
C:\Documents and Settings\vhaisdandrel\My Documents\JCreator LE\MyProjects\ListFiles\src\mugs\PrintPage.java:336: local variable butDisplayPage is accessed from within inner class; needs to be declared final
               butDisplayPage.setEnabled(true);
               ^
1 error
Process completed.

Can someone explain to me why this is happening, and what I should do to fix the problem?  
package mugs;
 
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.io.*;
import java.util.*;
import javax.swing.*; 
import javax.swing.filechooser.*;
import javax.swing.border.LineBorder;
import nl.marcoslot.lib.gui.layout.alm.ArithmeticLayoutManager;
 
public class PrintPage extends JFrame implements Printable {
	
	String[][][] arrPg = new String[100][6][6];
	
  	public static ComponentPrinterFrame cpf = null;
  	public static PrintPage pp=null;
  	
  	File dirName = null;
	public JRadioButton falseBorders    = null;
	public JRadioButton trueBorders   = null;
	public JTextField txtDirectory = null;
	public JTextField txtDisplayPage = null;
	public JTextField txtFirstPixRight = null;
	public JTextField txtFirstPixTop = null;
	public JTextField txtFooterDate = null;
	public JTextField txtFooterPgNoTop = null;
	public JTextField txtFooterSide = null;
	public JTextField txtFooterSidePgNo = null;
	public JTextField txtFooterTop = null;
	public JTextField txtHeader = null;
	public JTextField txtHeaderHeight = null;
	public JTextField txtHeaderWidth = null;
	public JTextField txtHeaderTop = null;
	public JTextField txtHeight = null;
	public JTextField txtNumPages = null;
	public JTextField txtNumPix = null;
	public JTextField txtNumToPrint = null;
	public JTextField txtPageToPrint = null;
	public JTextField txtPixSideToSide = null;	
	public JTextField txtPixTopToTop = null;
	public JTextField txtPrefHeight = null;
	public JTextField txtPrefWidth = null;  
	public JTextField txtProgress = null;
	public JTextField txtWidth = null;
				
  public static void main(String[] args) {
    pp = new PrintPage();
    pp.setVisible(true);
  }
  
  public PrintPage() {
    super("Mug Shot Directory Control Center");
    createUI();
  }
  
  protected void createUI() {
   final int widthContainer = 400;
   final int heightContainer = 730;
   final int insetTop = 5;
   final int insetLeft = 5;
   final int insetBottom = 5;
   final int insetRight=5;
   final int lineHeight=25;
   int adv=0;
   int lineCt=-1;
   int offset;
 
   JPanel panel = new JPanel() {
  		public Insets getInsets() {
    		return new Insets(insetTop,insetLeft,insetBottom,insetRight);
  		}
	};
 
	//Main Pane
	panel.setLayout(new ArithmeticLayoutManager());
	LineBorder line = new LineBorder(Color.black,1,true);
	panel.setBorder(line);
	setContentPane(panel);
	setSize(widthContainer,heightContainer);
	setResizable(false);
	
	//Display Borders [falseBorders,trueBorders]  
	lineCt++; offset=70;
	JLabel lblDBorders = new JLabel("Display borders:");
	panel.add(lblDBorders,"name=lblDBorders;left=parent.left+"+offset+";top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	falseBorders    = new JRadioButton("No"   , false);
	trueBorders   = new JRadioButton("Yes"  , true);
	ButtonGroup bgroup = new ButtonGroup();
	falseBorders.setSelected(true);
	bgroup.add(falseBorders);
	bgroup.add(trueBorders);
	panel.add(trueBorders,"name=trueBorders;left=lblDBorders.rRight+8;top=parent.top");
	panel.add(falseBorders,"name=falseBorders;left=trueBorders.rRight;top=parent.top");
 
	drawLine(panel,360,1,Color.black,10,25);
 
	//Page Width [txtWidth]
	lineCt++; offset=offset+12; adv=adv+5;
	JLabel lblWidth = new JLabel("Page width:");
	panel.add(lblWidth,"name=lblWidth;left=parent.left+"+offset+";top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	txtWidth = new JTextField("580");
	txtWidth.setColumns(3);
	panel.add(txtWidth,"name=txtWidth;left=lblWidth.rRight+8;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	
	//Page Height [txtHeight]
	lineCt++; offset=offset-3; adv=adv;
	JLabel lblHeight = new JLabel("Page height:");
	panel.add(lblHeight,"name=lblHeight;left=parent.left+"+offset+";top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	txtHeight = new JTextField("810");
	txtHeight.setColumns(3);
	panel.add(txtHeight,"name=txtHeight;left=lblHeight.rRight+8;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
 
	//Picture Width [txtPrefWidth]
	lineCt++; offset=offset-10; adv=adv+5;
	JLabel lblPrefWidth = new JLabel("Picture width:");
	panel.add(lblPrefWidth,"name=lblPrefWidth;left=parent.left+"+offset+";top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	txtPrefWidth = new JTextField("103");
	txtPrefWidth.setColumns(3);
	panel.add(txtPrefWidth,"name=txtPrefWidth;left=lblPrefWidth.rRight+8;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	
	//Picture Height [txtPrefHeight]
	lineCt++; offset=offset-4; adv=adv;
	JLabel lblPrefHeight = new JLabel("Picture height:");
	panel.add(lblPrefHeight,"name=lblPrefHeight;left=parent.left+"+offset+";top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	txtPrefHeight = new JTextField("125");
	txtPrefHeight.setColumns(3);
	panel.add(txtPrefHeight,"name=txtPrefHeight;left=lblPrefHeight.rRight+8;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
 
	drawLine(panel,360,1,Color.black,10,82);
 
	//Offset, First Pix, Top [txtFirstPixTop]
	lineCt++; offset=offset-28; adv=adv+7;
	JLabel lblFirstPixTop = new JLabel("Offset, first pix, top:");
	panel.add(lblFirstPixTop,"name=lblFirstPixTop;left=parent.left+"+offset+";top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	txtFirstPixTop = new JTextField("22");
	txtFirstPixTop.setColumns(3);
	panel.add(txtFirstPixTop,"name=txtFirstPixTop;left=lblPrefHeight.rRight+8;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
 
	//Offset, First Pix, Right [txtFirstPixRight]
	lineCt++; offset=offset-8; adv=adv;
	JLabel lblFirstPixRight = new JLabel("Offset, first pix, right:");
	panel.add(lblFirstPixRight,"name=lblFirstPixRight;left=parent.left+"+offset+";top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	txtFirstPixRight = new JTextField("0");
	txtFirstPixRight.setColumns(3);
	panel.add(txtFirstPixRight,"name=txtFirstPixRight;left=lblPrefHeight.rRight+8;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
 
	//Offset, Pictures, Top to Top [txtPixTopToTop]
	lineCt++; offset=offset+2; adv=adv;
	JLabel lblPixTopToTop = new JLabel("Offset, pix, top to top:");
	panel.add(lblPixTopToTop,"name=lblPixTopToTop;left=parent.left+"+offset+";top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	txtPixTopToTop = new JTextField("145");
	txtPixTopToTop.setColumns(3);
	panel.add(txtPixTopToTop,"name=txtPixTopToTop;left=lblPrefHeight.rRight+8;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
 
	//Offset, Pictures, Side to Side [txtPixSideToSide]
	lineCt++; offset=offset-11; adv=adv;
	JLabel lblPixSideToSide = new JLabel("Offset, pix, side to side:");
	panel.add(lblPixSideToSide,"name=lblPixSideToSide;left=parent.left+"+offset+";top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	txtPixSideToSide = new JTextField("115");
	txtPixSideToSide.setColumns(3);
	panel.add(txtPixSideToSide,"name=txtPixSideToSide;left=lblPrefHeight.rRight+8;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
 
	drawLine(panel,360,1,Color.black,10,191);
	
	//Header [txtHeader]
	lineCt++; offset=offset; adv=adv;
	txtHeader = new JTextField("Arlington SDA Church Mug Shot Directory");
	txtHeader.setColumns(33);
	panel.add(txtHeader,"name=txtHeader;left=10;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
 
	//Offset, Header, Top [txtHeaderTop]
	lineCt++; offset=offset+22; adv=adv;
	JLabel lblHeaderTop = new JLabel("Offset, Header, top:");
	panel.add(lblHeaderTop,"name=lblHeaderTop;left=parent.left+"+offset+";top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	txtHeaderTop = new JTextField("0");
	txtHeaderTop.setColumns(3);
	panel.add(txtHeaderTop,"name=txtHeaderTop;left=lblPrefHeight.rRight+8;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
 
	//Header, Width [txtHeaderWidth]
	lineCt++; offset=offset+26; adv=adv;
	JLabel lblHeaderWidth = new JLabel("Header, width:");
	panel.add(lblHeaderWidth,"name=lblHeaderWidth;left=parent.left+"+offset+";top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	txtHeaderWidth = new JTextField("450");
	txtHeaderWidth.setColumns(3);
	panel.add(txtHeaderWidth,"name=txtHeaderWidth;left=lblPrefHeight.rRight+8;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
 
	//Header, Height [txtHeaderHeight]
	lineCt++; offset=offset-4; adv=adv;
	JLabel lblHeaderHeight = new JLabel("Header, height:");
	panel.add(lblHeaderHeight,"name=lblHeaderHeight;left=parent.left+"+offset+";top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	txtHeaderHeight = new JTextField("24");
	txtHeaderHeight.setColumns(3);
	panel.add(txtHeaderHeight,"name=txtHeaderHeight;left=lblPrefHeight.rRight+8;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
 
	//Date [txtFooterDate]
	lineCt++; offset=offset; adv=adv;
	DateFormat df = new SimpleDateFormat("MMMMM dd, yyyy");
    Date now = new Date();
    txtFooterDate = new JTextField(df.format(now));
	txtFooterDate.setColumns(33);
	panel.add(txtFooterDate,"name=txtFooterDate;left=10;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
 
	//Offset, Footer, Top [txtFooterTop]
	lineCt++; offset=offset-17; adv=adv;
	JLabel lblFooterTop = new JLabel("Offset, Footer, top:");
	panel.add(lblFooterTop,"name=lblFooterTop;left=parent.left+"+offset+";top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	txtFooterTop = new JTextField("0");
	txtFooterTop.setColumns(3);
	panel.add(txtFooterTop,"name=txtFooterTop;left=lblPrefHeight.rRight+8;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
 
	//Offset, Footer, Side [txtFooterSide]
	lineCt++; offset=offset-7; adv=adv;
	JLabel lblFooterSide = new JLabel("Offset, Footer, side:");
	panel.add(lblFooterSide,"name=lblFooterSide;left=parent.left+"+offset+";top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	txtFooterSide = new JTextField("60");
	txtFooterSide.setColumns(3);
	panel.add(txtFooterSide,"name=txtFooterSide;left=lblPrefHeight.rRight+8;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
 
	//Offset, Footer, Top, Page Number [txtFooterPgNoTop]
	lineCt++; offset=offset-21; adv=adv;
	JLabel lblFooterTopPgNo = new JLabel("Offset, Footer, Pg#, top:");
	panel.add(lblFooterTopPgNo,"name=lblFooterTopPgNo;left=parent.left+"+offset+";top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	txtFooterPgNoTop = new JTextField("0");
	txtFooterPgNoTop.setColumns(3);
	panel.add(txtFooterPgNoTop,"name=txtFooterPgNoTop;left=lblPrefHeight.rRight+8;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
 
	//Offset, Footer, Side, Page Number [txtFooterSidePgNo]
	lineCt++; offset=offset-5; adv=adv;
	JLabel lblFooterSidePgNo = new JLabel("Offset, Footer, Pg#, side:");
	panel.add(lblFooterSidePgNo,"name=lblFooterSidePgNo;left=parent.left+"+offset+";top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	txtFooterSidePgNo = new JTextField("60");
	txtFooterSidePgNo.setColumns(3);
	panel.add(txtFooterSidePgNo,"name=txtFooterSidePgNo;left=lblPrefHeight.rRight+8;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
	
	drawLine(panel,360,1,Color.black,10,191);
 
	//Directory [txtDirectory]
	lineCt++; offset=offset; adv=adv;
	txtDirectory = new JTextField();
	txtDirectory.setColumns(21);
	txtDirectory.setEditable(false);
	txtDirectory.setBackground(new Color(253,253,253));
	panel.add(txtDirectory,"name=txtDirectory;left=10;top=parent.top+"+(adv+3+(lineCt*lineHeight)));
 
	//Open directory button
	JButton butOpen = new JButton("Open Directory");
	int yPos=panel.getHeight()-butOpen.getHeight()-5;
	int xPos=(panel.getWidth()/2)-(butOpen.getWidth()/2);
	panel.add(butOpen,"x=255;y=467");
 
	// Display page statistics...
	JLabel lblNumPix = new JLabel("#Pix:");
	panel.add(lblNumPix,"name=lblNumPix;x=5;y=493");
	txtNumPix = new JTextField("");
	txtNumPix.setColumns(4);
	txtNumPix.setEditable(false);
	txtNumPix.setBackground(new Color(253,253,253));
	panel.add(txtNumPix,"name=txtNumPix;x=35;y=493");
 
	JLabel lblNumPages = new JLabel("#Pages:");
	panel.add(lblNumPages,"name=lblNumPages;x=120;y=493");
	txtNumPages = new JTextField("");
	txtNumPages.setColumns(3);
	txtNumPages.setEditable(false);
	txtNumPages.setBackground(new Color(253,253,253));
	panel.add(txtNumPages,"name=txtNumPages;x=168;y=493");
	
	// Select/Display page number...
	offset=30;
	JLabel lblDisplayPage = new JLabel("Page#:");
	panel.add(lblDisplayPage,"name=lblDisplayPage;x=110;y="+(503+offset));
	txtDisplayPage = new JTextField("1");
	txtDisplayPage.setColumns(3);
	panel.add(txtDisplayPage,"name=DisplayPage;left=lblDisplayPage.rRight+8;y="+(502+offset));
	JButton butDisplayPage = new JButton("Display Page");
	butDisplayPage.setEnabled(false);
	panel.add(butDisplayPage,"x=200;y="+(498+offset));
 
	// Pages & #Pages to print...
	JLabel lblPgControl = new JLabel("# pages to print:");
	panel.add(lblPgControl,"name=lblNumPix;x=5;y=570");
	txtNumToPrint = new JTextField("1");
	txtNumToPrint.setColumns(2);
	panel.add(txtNumToPrint,"name=txtNumToPrint;x=100;y=570");
	JLabel lblProviso = new JLabel("[the optional ':#' overrides this qty.]");
	panel.add(lblProviso,"x=145;y=570");
	txtPageToPrint = new JTextField("");
	txtPageToPrint.setColumns(33);
	panel.add(txtPageToPrint,"name=txtPageToPrint;x=10;y=590");
	JLabel lblPInfo = new JLabel("[All, Odd, Even, or page#, optionally followed by ':#' for copies]");
	panel.add(lblPInfo,"x=20;y=610");
 
	//Progress Window
	txtProgress = new JTextField("");
	txtProgress.setColumns(33);
	txtProgress.setEditable(false);
	txtProgress.setBackground(new Color(253,253,253));
	panel.add(txtProgress,"name=txtProgress;x=10;y=640");
	
	//Print Page(s) BUTTON
	JButton printButton = new JButton("Print Page(s)");
	yPos=panel.getHeight()-printButton.getHeight()-5;
	xPos=(panel.getWidth()/2)-(printButton.getWidth()/2);
	panel.add(printButton,"x=0.5*parent.width-0.5*this.width;y=parent.height-this.height-10");
	
	// Center.
	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	Dimension frameSize = getSize();
	int x = (screenSize.width - frameSize.width) / 2;
	int y = (screenSize.height - frameSize.height) / 2;
	setLocation(600,0);
	
	addWindowListener(new WindowAdapter() {
	  public void windowClosing(WindowEvent e) {
	    dispose();
	    System.exit(0);
	  }
	});
 
	butOpen.addActionListener(new ActionListener() {
	  
	  public void actionPerformed(ActionEvent ae) {
	  	dirName=null;
	  	JFileChooser fc = new JFileChooser();
	  	fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int retVal = fc.showOpenDialog(PrintPage.this);
		if (retVal == JFileChooser.APPROVE_OPTION) {
               dirName = fc.getSelectedFile();
               txtDirectory.setText(dirName.toString());
               buildPages(dirName);
               butDisplayPage.setEnabled(true);
               txtProgress.setText("Enabled txtDisplayPage");
        }
	  }
	});
 
	printButton.addActionListener(new ActionListener() {
	  public void actionPerformed(ActionEvent ae) {
	    PrinterJob pj = PrinterJob.getPrinterJob();
	    pj.setPrintable(cpf);
	    int numCopy = ComponentPrinterFrame.tfToInt(pp.txtNumToPrint);
	    pj.setCopies(1); 
	    if (pj.printDialog()) {
	      try { 
    		disableDoubleBuffering(getContentPane());
    		for(int nCopy=0;nCopy<numCopy;nCopy++){
    			pj.print();
    		}
    		enableDoubleBuffering(getContentPane());
	      }
	      catch (PrinterException pe) {
	        pp.txtProgress.setText("Error: "+pe);
	      }
	    }
	  }
	});
 
	butDisplayPage.addActionListener(new ActionListener() {
	  public void actionPerformed(ActionEvent ae) {
		if (dirName!=null) {
			   cpf = new ComponentPrinterFrame(pp);
			   cpf.setVisible(true);
        }
	  }
	});
	
  }
  
  public int print(Graphics g, PageFormat pf, int pageIndex) {
    if (pageIndex != 0) return NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D)g;
    g2.translate(pf.getImageableX(), pf.getImageableY());
    getContentPane().paint(g2);
	//txtProgress.setText("Printing complete...");
	return PAGE_EXISTS;
  }
  
	public void drawLine(JPanel pnl,int width, int height, Color col, int xStart, int yStart){
		DrawingArea da = new DrawingArea(width,height);
		da.setBackground(col);
		pnl.add(da,"x="+xStart+";y="+yStart);
	}
 
	public void buildPages(File dirName){
		FilenameFilter filter = new FilenameFilter() {
        	public boolean accept(File dir, String name) {
            	return name.toLowerCase().endsWith(".jpg");
        	}
    	};
		String[] files = dirName.list(filter);
		txtNumPix.setText(Integer.toString(files.length+1));
		int pgNo=1, rowNo = 1, colNo=0, ctr=0;
		for (int i=0; i<files.length; i++) {
            String filename = files[i];
            colNo++;
            //txtProgress.setText("Pages: "+Integer.toString(pgNo)+" row#:"+Integer.toString(rowNo)+" col#:"+Integer.toString(colNo));
            arrPg[pgNo][rowNo][colNo]=filename.toString();
            //If last pix on column on last row on page...
            if((rowNo==5)&&(colNo==5)){
            	pgNo++; rowNo=1 ; colNo=0;
            	continue;
            }
            //If last pix on column...
            if(colNo==5){
            	rowNo++; colNo=0;
            	continue;
            }
            	
        }
        txtNumPages.setText(Integer.toString(pgNo));
	}
	
	  public static void disableDoubleBuffering(Component c) {
	    RepaintManager currentManager = RepaintManager.currentManager(c);
	    currentManager.setDoubleBufferingEnabled(false);
	  }
	
	  public static void enableDoubleBuffering(Component c) {
	    RepaintManager currentManager = RepaintManager.currentManager(c);
	    currentManager.setDoubleBufferingEnabled(true);
	  }
 
	public String getPageToPrint(){
		return "23";
	}
 
}

Open in new window

Comment
Watch Question

BRONZE EXPERT
Commented:
BRONZE EXPERT
Commented:
V good example here: http://gcc.gnu.org/ml/java-prs/2001-q1/msg00036.html

which is short, so here it is:
public class Local2
{
  void a()
  {
    int x;
    class I
    {
      int b() {return x;};    
    }
  }
}

// recompile with x as final & initialised, and it will compile.
Commented:
>Can someone explain to me why this is happening, and what I should do to fix the problem?
It's interlligent feature of java compiler to ensure accessing to an existing/instantiated variable which's placed outside the anonymous local inner class.
There are many ways to fix the problem, such as declare the outside variable with final (which means you need to instantiate it at the line you declare it) or provide a method to access to the outside variable instead.
Regarding your case, you can declare the butDisplayPage var as final like this

final JButton butDisplayPage = new JButton("Display Page");

Author

Commented:
Guys, a quick look, and I think I have the information I need.  I'll digest in the morning, and split points then.  Thanks!

Author

Commented:
Thanks, everyone!
BRONZE EXPERT

Commented:
;) Thanks, k.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.