Skip to main content

Need Help urgently with JMenuBar!!

1 reply [Last post]
PaulOckleford
Offline
Joined: 2006-02-17
Points: 0

Hi I hope someone can help me please! I am trying to add ActionListeners to JMenuBar Item But if I use:

playerItem.addActionListener(new ActionListener());

I get interface.java.awt.event.ActionListener is abstract;cannot be instantiated error.

If I use:

playerItem.addActionListener(this);

I get: this cannot be referenced from a static context:

Please can someone help I have included the full code below. Thanks.

import java.util.*; //Required for vector class
import java.io.*; //Required for IOExceptions
import java.awt.*; //Required for Graphic Components
import javax.swing.*;
import java.awt.event.*;

public class ProgRun implements ActionListener
{
public static void main (String[] args)
{
//Add the football image to Frame
Image image = Toolkit.getDefaultToolkit().getImage("ball.gif");

JMenuBar menuBar;
menuBar = new JMenuBar();//create a menu bar

JMenu listMenu = new JMenu("List");//create a menu
JMenu aboutMenu = new JMenu("About");//create a menu
menuBar.add(listMenu);//add menu to menubar
menuBar.add(aboutMenu);//add menu to menubar

//create the items to add to the menu
JMenuItem playerItem = new JMenuItem("List Players");
JMenuItem teamItem = new JMenuItem("List Teams");
JMenuItem gamesItem = new JMenuItem("List Games");
JMenuItem venueItem = new JMenuItem("List Venues");
JMenuItem aboutItem = new JMenuItem("About...");

//add items to menu
listMenu.add(playerItem);
listMenu.add(teamItem);
listMenu.add(gamesItem);
listMenu.add(venueItem);
aboutMenu.add(aboutItem);

//add actionlisteners to the menu items
playerItem.addActionListener(new ActionListener());
teamItem.addActionListener(new ActionListener());
gamesItem.addActionListener(new ActionListener());
venueItem.addActionListener(new ActionListener());
aboutItem.addActionListener(new ActionListener());

JFrame runFrame = new JFrame();//create the frame
runFrame.setJMenuBar(menuBar);//add menu to the frame
//force user to use the quit Button and not the close Button
runFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
runFrame.setTitle("League Manager 2004");

MenuOption runMenuOption = new MenuOption();
runFrame.setIconImage(image);
runFrame.setSize(195, 220);
runFrame.setBackground(Color.lightGray);
runFrame.setResizable(false);
runFrame.setLocation(280, 180);
runFrame.getContentPane().add(runMenuOption);
runFrame.setVisible(true);

}
public void actionPerformed(ActionEvent e)
{
System.out.print("works");
}

}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Anonymous

There are some possible solutions for your problem. One would be this for example:

[code]
myMenu.addActionlistener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// do your action
}
});
[/code]

Another thing to note:
You have put all your code in the main method which is static and there you get your error from. If you would modify your program like this for example

[code]
public class MyProgram implements ActionListener {
public static void main(String[] args) {
MyProgram prog = new MyProgram();
prog.initGUI();
prog.do_something_else();
}

void initGUI() {
// your Menu & GUI code here
}

void do_something_else() {

}

public void actionPerformed(ActionEvent arg) {

}
}
[/code]

you wouldn't get the static error any more.

mybe you should have a look at [url=http://www.google.com/search?q=java+action+listener+tutorial&ie=UTF-8&oe=UTF-8]this google search[/url] I am sure you will find some helpful background material ;)

Hope that helps.

Patrick