JCheckBoxMenuItem and submenu alignment problem

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top