I have this:
settingsMenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
settingsMenuActionPerformed(evt);
}
});
private void settingsMenuActionPerformed(java.awt.event.ActionEvent evt) {
HelpFrame help = new HelpFrame();
help.setVisible(true);
}
When I click on settings no frame is shown...I tried it in a JMenuItem and it works.
Thanks for the replies ;)
A JMenu is essentially a button. I think to get the behavior that you want, you would need to make your settingsMenu a JButton. Try it, it works.
You will have to change the background color, the font, the border, etc. to that of a JMenu...maybe there is an easier way, but you can try something like this:
JButton settingsButton = new JButton("Settings");
settingsButton.setFont(fileMenu.getFont()); //set the font to that of your file menu, or whatever you want
settingsButton.setBorder(null);
//set the background color...etc...
settingsButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
settingsMenuActionPerformed(evt);
}
});
private void settingsMenuActionPerformed(java.awt.event.ActionEvent evt) {
HelpFrame help = new HelpFrame();
help.setVisible(true);
}
You need to add a MenuListener to the higher level menu. See attached sample working code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MenuTest
{
public static void main(String args[])
{
new MenuTestFrame();
}
}
class MenuTestFrame extends JFrame implements ActionListener
{
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu view = new JMenu("View");
JMenu help = new JMenu("Help");
JMenuItem fileOpen = new JMenuItem("Open...");
JSeparator separator = new JSeparator();
JMenuItem fileSaveAs = new JMenuItem("Save As...");
JMenuItem editCut = new JMenuItem("Cut");
JMenuItem editCopy = new JMenuItem("Copy");
JMenuItem editPaste = new JMenuItem("Paste");
JMenuItem helpAbout = new JMenuItem("About...");
MenuTestFrame()
{
super();
/* Components should be added to the container's content pane */
Container cp = getContentPane();
/* Add menu items to menus */
file.add(fileOpen);
file.add(separator);
file.add(fileSaveAs);
edit.add(editCut);
edit.add(editCopy);
edit.add(editPaste);
help.add(helpAbout);
/* Add menus to menubar */
mb.add(file);
mb.add(edit);
mb.add(view);
mb.add(help);
/* Set menubar */
setJMenuBar(mb);
/* Add the action listeners */
fileOpen.addActionListener(this);
fileSaveAs.addActionListener(this);
editCut.addActionListener(this);
editCopy.addActionListener(this);
editPaste.addActionListener(this);
helpAbout.addActionListener(this);
/* Add menu listener */
view.addMenuListener(new MenuListener() {
public void menuCanceled(MenuEvent evt) {}
public void menuDeselected(MenuEvent evt) {}
public void menuSelected(MenuEvent evt)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(MenuTestFrame.this,"Menu test dialog!","Message",JOptionPane.INFORMATION_MESSAGE);
}
});
}
});
/* Add the window listener */
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
dispose();
System.exit(0);
}
});
/* Size the frame */
setSize(200,200);
/* Center the frame */
Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle frameDim = getBounds();
setLocation((screenDim.width - frameDim.width) / 2,(screenDim.height - frameDim.height) / 2);
/* Show the frame */
setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
Object obj = evt.getSource();
if (obj == fileOpen);
else if (obj == fileSaveAs);
else if (obj == editCut);
else if (obj == editCopy);
else if (obj == editPaste);
else if (obj == helpAbout);
}
}