small bit of code . actionlistener not working

hello, i am trying to get this actionlistener for the button bNew to work but it gives me the error cannot find symbol variable bNew. im guessing inside actionperformed it cannot see it, but it should right ? .. any help appreciated.

thanks :)

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

/**

* Sample application using Frame.

*

* @author

* @version 1.00 07/02/05

*/

publicclass InvoiceFrameextends JFrameimplements ActionListener

{

/**

* The constructor.

*/

public InvoiceFrame()

{

JButton bNew =new JButton("New Invoice");

JButton bLoad =new JButton("Load Invoice");

bNew.addActionListener(this);

bLoad.addActionListener(this);

JTextArea taDisplay =new JTextArea(10,10);

JPanel center =new JPanel();

JPanel south =new JPanel();

Container c = getContentPane();

c.setLayout(new BorderLayout());

c.add(center, BorderLayout.CENTER);

c.add(south, BorderLayout.SOUTH);

center.add(taDisplay);

south.add(bLoad);

south.add(bNew);

this.setSize(400,400);

this.setVisible(true);

this.setTitle("Invoice Mate");

}

publicvoid actionPerformed(ActionEvent ae)

{

if ( ae.getSource() == bNew )

{

taDisplay.append("blah");

}

}

}

[2469 byte] By [rks01a] at [2007-11-26 17:32:26]
# 1

Hi, it is to do with scope. You declare and initialise your button variables within one method and they can not be seen by the other.

Try this.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

/**

* Sample application using Frame.

*

* @author

* @version 1.00 07/02/05

*/

public class InvoiceFrame extends JFrame implements ActionListener

{

private JButton bNew;

private JButton bLoad;

private JTextArea taDisplay;

/**

* The constructor.

*/

public InvoiceFrame()

{

bNew = new JButton("New Invoice");

bLoad = new JButton("Load Invoice");

bNew.addActionListener(this);

bLoad.addActionListener(this);

JTextArea taDisplay = new JTextArea(10,10);

JPanel center = new JPanel();

JPanel south = new JPanel();

Container c = getContentPane();

c.setLayout(new BorderLayout());

c.add(center, BorderLayout.CENTER);

c.add(south, BorderLayout.SOUTH);

center.add(taDisplay);

south.add(bLoad);

south.add(bNew);

this.setSize(400,400);

this.setVisible(true);

this.setTitle("Invoice Mate");

}

public void actionPerformed(ActionEvent ae)

{

if ( ae.getSource() == bNew )

{

taDisplay.append("blah");

}

}

}

Notice where I Declared the variables?

Cheers

kikemellya at 2007-7-9 0:00:27 > top of Java-index,Java Essentials,New To Java...
# 2
You've declared nNew as a local variable, and local variables aren't visible in other methods. What can you do to make this variable visible to the actionPerformed method?
DrLaszloJamfa at 2007-7-9 0:00:27 > top of Java-index,Java Essentials,New To Java...
# 3
aha , thank you all, i understand now.
rks01a at 2007-7-9 0:00:27 > top of Java-index,Java Essentials,New To Java...