JMenu with no JMenuItem

Hi!I have a JMenuBar with one JMenu (File) and its JMenuItems.I want to add another JMenu (settings) but with no JMenuItems, so when the user clicks on settings a new window is shown.Is this possible?
[228 byte] By [naskara] at [2007-11-26 14:02:00]
# 1
YesWhy not just try it first before coming here?
tjacobs01a at 2007-7-8 1:44:28 > top of Java-index,Desktop,Core GUI APIs...
# 2
Yes, but why not just put a JMenuItem on the bar?Or have the only option under the setting menu be "Open settings dialog" or something like that. (I like that the best, it is the most intuitive.)
zadoka at 2007-7-8 1:44:28 > top of Java-index,Desktop,Core GUI APIs...
# 3
Of course I tried it before...But it doesnt work...
naskara at 2007-7-8 1:44:28 > top of Java-index,Desktop,Core GUI APIs...
# 4
> But it doesnt work...Please expand on this. Doesn't work is not very descriptive.
zadoka at 2007-7-8 1:44:28 > top of Java-index,Desktop,Core GUI APIs...
# 5

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 ;)

naskara at 2007-7-8 1:44:28 > top of Java-index,Desktop,Core GUI APIs...
# 6

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);

}

JBriena at 2007-7-8 1:44:28 > top of Java-index,Desktop,Core GUI APIs...
# 7
any more ideas?
naskara at 2007-7-8 1:44:28 > top of Java-index,Desktop,Core GUI APIs...
# 8
Try using a mouseListener instead.
Rodney_McKaya at 2007-7-8 1:44:28 > top of Java-index,Desktop,Core GUI APIs...
# 9

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);

}

}

paternostroa at 2007-7-8 1:44:28 > top of Java-index,Desktop,Core GUI APIs...
# 10
Very thanks, thats just what I want ;)
naskara at 2007-7-8 1:44:28 > top of Java-index,Desktop,Core GUI APIs...