Reputation: 11
My Code is:
JButton btnNewButton = new JButton("ok"); //JButton btnNewButton = new JButton("Ok");
btnNewButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource().equals(btnNewButton))
{
}
}
when i wrote this still getting error. if(arg0.getSource().equals(btnNewButton)) getting error please any one fix it
Upvotes: 0
Views: 80
Reputation: 1436
try this code
JButton btnNewButton = new JButton("ok"); //JButton btnNewButton = new JButton("Ok");
btnNewButton.addActionListener(this);
public void actionPerformed(ActionEvent ae) {
if(ae.getSource().equals(btnNewButton))
{
////do your code here
}
Upvotes: 1
Reputation: 6618
Anonymous internal classes can't access local variables, unless they have been declared final. Changing the declaration of btnNewButton
to final JButton btnNewButton = ..."
would make it work.
However, since you are using an anonymous listener that is attached to nothing else but btnNewButton
, you already know the event source must be btnNewButton
, and the whole check is redundant.
Upvotes: 6