简体   繁体   中英

Java - Swing - JOptionPane padding

From my main form I want to display a new form.

When the new form is opened, it needs to be treated as a child in that clicking on the parent brings forward the child for focus. JFrames aren't modal apparently, so I'm using:

JOptionPane.showOptionDialog(null, MYPANEL, "Title", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[]{}, null);

...Where MYPANEL looks identical to what I need in my form.

This works 100% as intended EXCEPT that there is padding (about 20px or so) between the panel edges and the JOptionPane dialog frame.

How can I get rid of this?

Setting the border of the panel to an empty border with 0 as dimensions changed nothing.

Setting the border of the panel to an empty border with 0 as dimensions changed nothing.

You need to set the Border of the content pane of the dialog. Don't know if it will work on all LAF's but you can use the following for Metal and Windows:

UIManager.put("OptionPane.border", new EmptyBorder(0, 0, 0, 0) );

See UIManager Defaults for more information.

This will change the property for all option panes. So you may want to save the default border, create your option pane and then restore the original border for creating other option panes.

Maybe another option is to add an ContainerListener to the panel. Then you could handle the componentAdded(...) event. When the event is generated you can get the parent panel and remove the Border.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM