2

As in title, I struggle to get my JMenu to resize when programatically adding components. In my application I need JMenu with JCheckBoxes. Whenever I tick one of them, JSlider should appear just below. Below is the code that makes that happen. The problem I have is that when there's lot of sliders visible, items in JMenu get clumped - see image below.

How can I force redraw/resize/expansion of JMenu to perserve original checkboxes/sliders height?

Also note - JMenu stays visible at all times when selecting checkboxes. It closes only when I click outside of it. But after such 'restart' menu grows and problem is no longer present.

Many thanks in advance!

public class Window extends JFrame implements ActionListener {
    private JPanel panel;
    private JMenuBar menuBar;
    private JMenu menu;
    private JSlider slider1;
    private JCheckBoxMenuItem checkBox1;
    private JCheckBoxMenuItem checkBox2;
    private JCheckBoxMenuItem checkBox3;

    public Window() {
        super("Example");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(300,300));

        panel = new JPanel();
        panel.setMinimumSize(new Dimension(300,300));

        menuBar = new JMenuBar();
        menu = new JMenu("Options");

        checkBox1 = new JCheckBoxMenuItem("option 1");
        checkBox2 = new JCheckBoxMenuItem("option 2");
        checkBox3 = new JCheckBoxMenuItem("option 3");

        checkBox1.addActionListener(this);
        //prevent JMenu from closing after selecting CheckBox
        checkBox1.setUI(new BasicCheckBoxMenuItemUI() {
            @Override
            protected void doClick(MenuSelectionManager msm) {
                checkBox1.doClick(0);
            }
        });

        slider1 = new JSlider();
        slider1.setVisible(false);

        menu.add(checkBox1);
        menu.add(slider1);
        menu.add(checkBox2);
        menu.add(checkBox3);
        menuBar.add(menu);
        setJMenuBar(menuBar);
        add(panel);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(checkBox1)) {
            slider1.setVisible(checkBox1.isSelected());
        }
    }
}

Here's screen from my main application:

Clumped items

5
  • I used swing ages back. it had issues back then too. revalidate() is supposed to do the trick, but i would say a use case like this is not what menus are meant for? why not use a dialog for dynamic components?
    – tgkprog
    Sep 16, 2021 at 17:07
  • Unfortunately none of these methods helps, behavour stays the same. I can't force menuitem list to grow. I agree that it's not your usual menu use case, but my requirement was to mimic other program and keep ui as similar as possible.
    – brtk
    Sep 16, 2021 at 18:00
  • You have a custom menu item UI which is probably causing the problem.
    – camickr
    Sep 16, 2021 at 18:36
  • @camickr thanks for suggestion. I checked that and removed doClick override from my checkboxes (I think that's what you're referring to). Still, JMenu behaves the same way even when all of its components are plain old swing objects - i used JButton added to JMenu to trigger visibility change for slider.
    – brtk
    Sep 16, 2021 at 19:54

2 Answers 2

2

Hiding and reshowing the popup menu will cause it to be resized:

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(checkBox1)) {
        slider1.setVisible(checkBox1.isSelected());
        menu.setPopupMenuVisible(false);
        menu.setPopupMenuVisible(true);
    }
}
0
0

Call revalidate() it is supposed to do the trick, call it on the newly added components. https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Component.html#revalidate()

But i would say a use case like this is not what menus are meant for? why not use a dialog for dynamic components?

For background https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Component.html#revalidate() call it o the newly added component and see https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Component.html#invalidate() and https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Container.html#isValidateRoot()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.