Pls Help: JMenu with 2 columns



Hi,

I am creating a JMenu that put its items in 2 columns, with the first
column holding JMenuItems and the second menu holding submenus
(JMenus). It looks somewhat like this:

-----------
| Load |
-----------------------------------------------
| 'JMenuItem' | 'JMenu' > |
| 'JMenuItem' | 'JMenu' > |
| 'JMenuItem' | 'JMenu' > |
| 'JMenuItem' | 'JMenu' > |
-------------------------------------

MY PROBLEM: The size of the 'JMenu' items on the second column is being
set to the same size as the biggest item on the first column
'JMenuItem' even though it actually has no text, BUT I want to only
display the ">" arrow that submenus have in any case. Therefore I would
like to set the width of the JMenus on hte 2nd column to something like
5 pixels or so but if I pass JMenus with this size to my menu it does
not work.

Does anyone know how ot fix this? Anyways here is a runnable sample of
my code:

import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MenuBarTest extends JMenuBar {
private JMenu load;
public void createAndShowMenuBar() {
load = new JMenu("Load");
load.setName("Load");

// Most important line (allows for 2 columns for this menu)
load.getPopupMenu().setLayout(new GridLayout(0, 2));

for (int i = 0; i < 10; i++) {
JMenu menu = new JMenu();

// Has NO Effect
menu.setSize(10, menu.getHeight());

for (int j = 0; j < i; j++) {
JMenuItem menuItem = new JMenuItem("InnerItem: " + i + "." + j);
menu.add(menuItem);
}
JMenuItem menuItem = new JMenuItem("MenuItem " + i);
load.add(menuItem);
load.add(menu);
}

add(load);
}
public static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
MenuBarTest mbTest = new MenuBarTest();
mbTest.createAndShowMenuBar();
frame.setJMenuBar(mbTest);
frame.setSize(300, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

Thanks.
Rohit

.



Relevant Pages

  • JMenu/JPopupMenu disappear on mouseEntered?
    ... JPopupMenu appears full of JMenuItems. ... dialog is closed, click the JMenu again. ... JMenuItem jmi = new JMenuItem; ... public void windowClosing ...
    (comp.lang.java.programmer)
  • Re: JMenuBar menuitem position off
    ... BorderLayout borderLayout1 = new BorderLayout; ... private void jbInitthrows Exception { ... public void start{ ... one JMenuItem("Exit") under JMenu. ...
    (comp.lang.java.help)
  • Re: Pls Help: JMenu with 2 columns
    ... I am creating a JMenu that put its items in 2 columns, ... column holding JMenuItems and the second menu holding submenus ...
    (comp.lang.java.gui)