Mark L.
Mark L.

Reputation: 573

Swing JDialog width too wide

I am trying to make a modeless dialog menu in Swing that is displayed upon the press of a button. The dialog contains several menu items. My problem is that the dialog window is much wider than necessary. I am looking for help on setting the window width.

Here's what the output looks like. Notice that the window containing the menu items is much wider than the items themselves. That's what I want to fix.

enter image description here

Here's minimal code that shows this problem:

import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

 import javax.swing.BoxLayout;
 import javax.swing.JButton;
 import javax.swing.JDialog;
 import javax.swing.JFrame;
 import javax.swing.JMenuItem;
 import javax.swing.SwingUtilities;

public class Test {

public static void main(String[] args) {
    new Test().run();

}

TestDialog testDialog;

private void run() {
    JFrame jframe = new JFrame();
    JButton jbutton = new JButton("test");
    jframe.add(jbutton);
    jbutton.setBounds(130, 100, 100, 40);
    jframe.setSize(400, 500);
    jframe.setLayout(null);
    jframe.setVisible(true);
    
    testDialog = new TestDialog(SwingUtilities.windowForComponent(jframe));
    
    jbutton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            
            testDialog.show();
        }
    });
}

private class TestDialog {
    
    JDialog jdialog;
    
    public TestDialog(Window parent) {
        jdialog = new JDialog(parent, "Test", Dialog.ModalityType.MODELESS);
        jdialog.setPreferredSize(new Dimension(100, 0));
        jdialog.setLayout(new BoxLayout(jdialog.getContentPane(), BoxLayout.Y_AXIS));
        JMenuItem jmenuItem1 = new JMenuItem("MenuItem 1");
        Dimension jmiDimension = jmenuItem1.getPreferredSize();
        System.out.printf("jmenuItem1 is %f x %f%n", jmiDimension.getWidth(), jmiDimension.getHeight());
        jdialog.add(jmenuItem1);
        jdialog.add(new JMenuItem("MenuItem 2"));
        jdialog.pack();
        Dimension d = jdialog.getSize();
        System.out.printf("jdialog is %f x %f%n", d.getWidth(), d.getHeight());
    }
    
    public void show() {
        jdialog.setVisible(true);
    }
    
}

}

The program prints this output, showing that the dialog is 324 pixels wide but the menu items are 87:

jmenuItem1 is 87.000000 x 21.000000
jdialog is 324.000000 x 88.000000

I have also tried using the jdialog.setSize() and jdialog.setMaximumSize() methods. And I've tried setting the maximum size of the menu items. None of them seem to have any affect upon the dialog's window size.

I also tried a GridLayout, rather than a BoxLayout - that also made no difference.

I also tried setting the width of the dialog's content pane and layered pane. Still no difference.

I noted that the dialog has no owner nor parent.

Upvotes: 0

Views: 268

Answers (1)

camickr
camickr

Reputation: 324098

testDialog = new TestDialog(SwingUtilities.windowForComponent(jframe));

You don't need to use the windowForComponent(...) method. You already have a reference to the parent frame:

testDialog = new TestDialog( jframe );

Don't attempt to hard code sizes. Each component will determine its own preferred size and the then layout manager will use this information to determine the overall size.

//jdialog.setPreferredSize(new Dimension(100, 0));

A JMenuItem was designed to be used on a JMenu. Instead you should be using a JButton to add to a regular panel.

I don't have a problem with the size. The width/height of the dialog is as expected.

Note I use JDK 11 on Windows 10. When I ran your original code, the dialog had no size since the setPreferredSize() statement caused the height to be 0. So I'm not sure how you get the display shown in your image.

And yes the dialog width is wider than the component added to the frame because there is a minimum width to the dialog to be able to be able to display the title bar even if no components are added to the dialog.

Upvotes: 2

Related Questions