Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Handling JMenu actions

I am writing a simple text editor, I have to be able to open, save, print, and exit. I have a class called TextMenu which is my menu. My problem is that i set the action events in that class, and I don't know how to control components in my main class which has the components that i have to control with it.

Its confusing :confused:

here is my code

TextMenu Class

import javax.swing.*;
import java.awt.event.*;

public class TextMenu extends JMenuBar implements ActionListener
{
String[] fileItems = new String[] {"New","Open","Save","Print","Exit"};
char[] fileShortcuts = {'N','O','S','P','X'};

public TextMenu()
{
JMenu fileMenu = new JMenu("File");

//Assemble the File menus
for(int i = 0; i< fileItems.length; i++)
{
JMenuItem item = new JMenuItem(fileItems[i]);
item.setAccelerator(KeyStroke.getKeyStroke(fileShortcuts[i],
java.awt.Event.CTRL_MASK,false));
item.addActionListener(this);
fileMenu.add(item);
}
add(fileMenu);
}

public void actionPerformed(ActionEvent event)
{
System.out.println("Menu item ["+ event.getActionCommand() +
"] was pressed.");

if(event.getActionCommand() =="Exit")
{
System.exit(0);
}
}
}

TextPadWindow class

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TextPadWindow extends JFrame
{
TextMenu myMenu = new TextMenu();
JTextArea workArea = new JTextArea();
JScrollPane workPanel = new JScrollPane();
JLabel stats = new JLabel("Total lines: ");
Container container;

public TextPadWindow()
{
super("Text Pad");
setJMenuBar(myMenu);
container = this.getContentPane();

workArea.setLineWrap(true);
workArea.setWrapStyleWord(false);
workArea.setMargin(new Insets(5,5,5,5));

workArea.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e)
{
stats.setText("Total lines: " + workArea.getLineCount());
}
});

workPanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
workPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
workPanel.setViewportView(workArea);

container.add(workPanel,BorderLayout.CENTER);
container.add(stats,BorderLayout.SOUTH);
setSize(300,250);
setVisible(true);
}
}

Any help will be very apreciated.
[2769 byte] By [Wizard1988] at [2007-11-11 7:25:38]
# 1 Re: Handling JMenu actions
I' ve never found the need to extend JMenuBar. It's easy enough to just leave it as is in the JFrame. If you want to seperate it's creation but still be able to control all the main components then just put it all in a method called say initMenuBar(). Here's an example of how I usually do it in the JFrame...

private void initMenuBar() {
JMenuBar menubar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem exitItem = new JMenuItem("exit");
fileMenu.add(exitItem);
JMenu helpMenu = new JMenu("Help");
JMenuItem usageItem = new JMenuItem("usage");
JMenuItem aboutItem = new JMenuItem("about");
helpMenu.add(usageItem); helpMenu.addSeparator(); helpMenu.add(aboutItem);
menubar.add(fileMenu); menubar.add(helpMenu);
//add action listeners
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
doExit();
}
});
usageItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
doUsage();
}
});
aboutItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
doAbout();
}
});
//set the bar
setJMenuBar(menubar);

}
Joe Beam at 2007-11-11 22:38:19 >