How to make menuitems do something when they are chosen?
Hi, first post from a novice so please be patient, and i apologise if makes no sense.
i have an assignment to do for one of my java modules (studying at 32, I must be crazy!).
the part I can't get to grips with is that I have a menu bar with 4 menu headings, each have multiple items. Im unsure how to register an event listener
(ActionListener?) Them Im unsure how to write the event listener code?
I'll show what I've done so far and comment what I was thinking.
Any help,tips or advice will be greatly appreciated.
(My comments are in capitals, the other comments are part of the provide skeleton code. Also I'm only interested in the 'exit' menuitem for now)
Thanks
Code:
/*
* M257DrawingApplication.java
*
*/
package tma02q3;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author M257 CT
*/
public class M257DrawingApplication extends JFrame
{
// instance variables provided
private final int FRAME_WIDTH = 1000;
private final int FRAME_HEIGHT = 500;
private DrawingPanel drawingPanel;
private MenuWatcher mw; //INSTANCE OF EVENT LISTENER -MENUWATCHER
// TODO add further instance variables as required
/**
* Creates a new instance of M257DrawingApplication
*/
public M257DrawingApplication(String title)
{
super(title);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
this.setResizable(false);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar mb = new JMenuBar(); //IS THIS THE RIGHT PLACE TO PUT MY MENUBAR?
JMenu m1 = new JMenu("Style");
JMenu m2 = new JMenu("Size");
JMenu m3 = new JMenu("Colour");
JMenu m4 = new JMenu("Close");
JMenuItem exit = new JMenuItem("exit");
exit.addActionListener(mw);//IS THIS RIGHT TO ADD THE EVENT LISTNER MW TO THE EXIT ITEM?
m1.add(new JMenuItem("Courier New"));//IGNORE THESE STRINGS, I NEED TO CHANGE THEM
m1.add(new JMenuItem("Courier New"));
m1.add(new JMenuItem("Courier New"));
m2.add(new JMenuItem("16"));
m2.add(new JMenuItem("18"));
m2.add(new JMenuItem("20"));
m3.add(new JMenuItem("Blue"));
m3.add(new JMenuItem("Red"));
m3.add(new JMenuItem("Green"));
m4.add(exit);
mb.add(m1);
mb.add(m2);
mb.add(m3);
mb.add(m4);
setJMenuBar(mb);
//set visible here, now size is available
setVisible(true);
drawingPanel = new DrawingPanel(getAvailableWidth(), getAvailableHeight());
// given exiting on close
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//TODO add registering of any event handlers here
mw = new MenuWatcher();
}
//These methods should only be called after the frame is visible.
//They tell you about the available width and height in the frame
private int getAvailableWidth()
{
return getWidth() - getInsets().left - getInsets().right;
}
private int getAvailableHeight()
{
return getHeight() - getInsets().top - getInsets().bottom;
}
//TODO complete update method
//this method drives the application
public void update()
{
//the idea here is to loop
//and change the picture
//this involves updating the content of the drawingPanel
// by calling the panel's updatePictureState method
drawingPanel.updatePictureState();
repaint();
try
{
Thread.sleep(50);
}
catch (InterruptedException e)
{
System.exit(0);
}
}
// i0nner class on which to draw everything - you can add
// to this class as much as you like
private class DrawingPanel extends JPanel // provided
{
private Picture myPicture;
// add further instance variables if required
public DrawingPanel(int width, int height) // given
{
myPicture = new Picture(width, height);
setSize(width, height);
add(new JButton());
}
//this method is invoked automatically when repaint occurs in
//the outer container
public void paintComponent(Graphics g) // given
{
super.paintComponent(g);
myPicture.paint(g);//This does the redrawing based on current state
}
// TODO complete updatePictureState method
//this is about updating the state of elements in the picture
//not about redrawing
public void updatePictureState()
{
//TODO update state of myPicture
}
//TODO add further methods as required
}
//TODO add further (inner) classes as required
private class MenuWatcher implements ActionListener
{
public void actionPerformed (ActionEvent a)
{
if (a.equals(exit))// TELLS ME IT CAN'T FIND VARIABLE EXIT?
{
}
}
}
}
Re: How to make menuitems do something when they are chosen?
Read the API for JMenu and follow the link to the Swing tutorial on How to Use Menus.
db