J
JavaEnquirer
I have a JMenu which contains a sub menu and a couple of
JCheckBoxMenuItems. I'm wondering how to go about aligning the text
such that the sub menu label is in line with the text of the check
boxes. Like so:
A Menu
A submenu >
[] Item1
[] Item2
I've included the code below. Thoughts so far:
1. Call some simple methods I've foolishly overlooked.
2. Dynamically scale a transparent gif and set it as the image icon of
the submenu. Problem - how to calculate the pad icon width in a
platform independent, screen resolution independent way i.e. the width
of the checkbox part of the JCheckBoxMenuItem ignoring the text. (
Note: a text less one gives me a wdith too large. )
import javax.swing.*;
public class AligmentTest
{
public static void main(String []args)
{
JFrame frame = new JFrame();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("A menu");
JMenu subMenu = new JMenu("A submenu");
menu.add(subMenu);
menuBar.add(menu);
frame.setJMenuBar(menuBar);
JCheckBoxMenuItem item1 = new JCheckBoxMenuItem("Item1");
JCheckBoxMenuItem item2 = new JCheckBoxMenuItem("Item2");
JCheckBoxMenuItem item3 = new JCheckBoxMenuItem("Item3");
menu.add(item1);
menu.add(item2);
subMenu.add(item3);
frame.setSize(100,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
JCheckBoxMenuItems. I'm wondering how to go about aligning the text
such that the sub menu label is in line with the text of the check
boxes. Like so:
A Menu
A submenu >
[] Item1
[] Item2
I've included the code below. Thoughts so far:
1. Call some simple methods I've foolishly overlooked.
2. Dynamically scale a transparent gif and set it as the image icon of
the submenu. Problem - how to calculate the pad icon width in a
platform independent, screen resolution independent way i.e. the width
of the checkbox part of the JCheckBoxMenuItem ignoring the text. (
Note: a text less one gives me a wdith too large. )
import javax.swing.*;
public class AligmentTest
{
public static void main(String []args)
{
JFrame frame = new JFrame();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("A menu");
JMenu subMenu = new JMenu("A submenu");
menu.add(subMenu);
menuBar.add(menu);
frame.setJMenuBar(menuBar);
JCheckBoxMenuItem item1 = new JCheckBoxMenuItem("Item1");
JCheckBoxMenuItem item2 = new JCheckBoxMenuItem("Item2");
JCheckBoxMenuItem item3 = new JCheckBoxMenuItem("Item3");
menu.add(item1);
menu.add(item2);
subMenu.add(item3);
frame.setSize(100,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}