Undefined variable: this

When running a swing application I get an undefined variable: this error when attempting to call the method addActionListener(), I can not see why I am getting this error as this is not a variable but rather a reserved word

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass GUIPracticeimplements ActionListener{

privatestaticvoid createAndShowGUI(){

JFrame frame =new JFrame("Address Book");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(new GridLayout(6,1));

JLabel label =new JLabel("<html><font color=black size=6>MMMMMMMMM</font><font color=lime size=7>Address Book</font></html>");

frame.getContentPane().add(label);

JButton newBook =new JButton("<html><font color=navy size=6>Create New Book</font></html>");

newBook.addActionListener(this);

JButton addEntry =new JButton("<html><font color=navy size=6>Add Contact</font></html>");

addEntry.addActionListener(this);

JButton change =new JButton("<html><font color=navy size=6>Change Contact</font></html>");

change.addActionListener(this);

JButton display =new JButton("<html><font color=navy size=6>Display Book</font></html>");

display.addActionListener(this);

JButton quit =new JButton("<html><font color=navy size=6>Quit</font></html>");

quit.addActionListener(this);

frame.setBackground(new Color(0x000000));

frame.getContentPane().add(newBook);

frame.getContentPane().add(addEntry);

frame.getContentPane().add(change);

frame.getContentPane().add(display);

frame.getContentPane().add(quit);

frame.pack();

frame.setVisible(true);

frame.setSize(600,600);

}

publicvoid actionPerformed(ActionEvent e)

{

JFrame frame =new JFrame("Address Book");

String s=e.getActionCommand();

if(s.equals("Create New Book"))

JOptionPane.showMessageDialog(frame,"You are creating a new address book");

elseif(s.equals("Add Contact"))

JOptionPane.showMessageDialog(frame,"You are adding another contact to your address book");

elseif(s.equals("Change Contact"))

JOptionPane.showMessageDialog(frame,"You are changing a contact in your address book");

elseif(s.equals("Display Book"))

JOptionPane.showMessageDialog(frame,"You are displaying a address book");

elseif(s.equals("Quit"))

JOptionPane.showMessageDialog(frame,"Goodbye");

}

publicstaticvoid main(String[] args){

GUIPractice g =new GUIPractice();

javax.swing.SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

createAndShowGUI();

}

});

}

}

any help would be greatly appreciated

[5151 byte] By [pocniba] at [2007-10-2 1:57:05]
# 1
"this" is only valid in an instance method or constructor. You're using it in a static method.
ChuckBinga at 2007-7-15 19:38:16 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2
Thank you, is there another way to call addActionListener() in a static method, or how would I then implement the event handling
pocniba at 2007-7-15 19:38:16 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3

I'm not sure why you are using invokeLater(). It appears that you could delete the whole Runnable stuff and change createAndShowGUI() into a constructor for the GUIPractice class.//Change the createAndShowGUI() declaration line to the following.

public GUIPractice {

//The rest of the code stays the same

//The main method becomes the following

public static void main(String[] args) {

new GUIPractice();

}

atmguya at 2007-7-15 19:38:16 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...