Pls Help: JMenu with 2 columns
- From: "Rohit" <rohit.dantas@xxxxxxxxx>
- Date: 23 Feb 2006 09:04:14 -0800
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
.
- Follow-Ups:
- Re: Pls Help: JMenu with 2 columns
- From: Vova Reznik
- Re: Pls Help: JMenu with 2 columns
- Prev by Date: Re: Scaling/Resizing 2D graphics in JPanel
- Next by Date: Re: setVisible on a JWindow steals focus from other apps?
- Previous by thread: Re: Why I can't add a JLabel object into JList/JComboBox?
- Next by thread: Re: Pls Help: JMenu with 2 columns
- Index(es):
Relevant Pages
|
|