JMenu problem

Discussion in 'Java' started by JavaEnquirer, Feb 10, 2006.

  1. JavaEnquirer

    JavaEnquirer Guest

    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

    and not:

    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. ( I don't want
    to customize a look and feel. )


    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. )

    Many thanks in advance.

    ---------------------------------------------------------------------------­--------------------------------



    Here's some code if anyone wants to give it a go.


    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.
     
    JavaEnquirer, Feb 10, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ike
    Replies:
    4
    Views:
    975
  2. Sam Brightman
    Replies:
    18
    Views:
    3,901
    Andrew Thompson
    Dec 10, 2003
  3. Angus Parvis

    Canvas3D overlaps JMenu

    Angus Parvis, Jun 13, 2005, in forum: Java
    Replies:
    1
    Views:
    1,261
    Bill Tschumy
    Jun 13, 2005
  4. JavaEnquirer

    JMenu problem

    JavaEnquirer, Feb 8, 2006, in forum: Java
    Replies:
    2
    Views:
    520
    JavaEnquirer
    Feb 8, 2006
  5. JavaEnquirer

    JMenu problem

    JavaEnquirer, Feb 10, 2006, in forum: Java
    Replies:
    0
    Views:
    395
    JavaEnquirer
    Feb 10, 2006
Loading...

Share This Page