JMenu 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


This alignment is automatic with the windows look and feel, but not
metal or motif. 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);
}


Many thanks in advance.
 
B

Bart Cremers

Why not find a platform independent L&F which does exactly what you
want and stick with that. Other look and feels render in their own way
and overriding is a really dificult task.

If you want to override you'll end up writing your custom L&F code
anyway.

Bart
 
J

JavaEnquirer

I'm very happy with the Metal look and feel/Ocean theme...apart from
the untidy way in which it aligns sub menus and JCheckBoxMenuItems. I
thought that there must be some simple way of tailoring the alignment
with regards this classic assemblage of components.

Cheers.
 

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,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top