Java - Swing - JOptionPane padding - java

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.

Related

Java swing metal JButton focus border size

If I set Metal theme for Swing GUI, JButton border is painted around the whole button, but focus border is painted only around JButton's text and icon, so if text size is smaller than button size, the border appears inside button.
Most replies to this problem simply suggest to disable global focus border painting by
UIManager.getLookAndFeelDefaults().put("Button.focus", new ColorUIResource(new Color(0, 0, 0, 0)));
or per-button by jButton.setFocusPainted(false);
But I actually want focus border to be painted - like in Nimbus LAF around the whole button. I suppose I need to override focus painter with default one for Button.border, but can't find which one. Maybe someone can help me?

How to add a scrollbar to JOptionPane.showConfirmDialog(...)

I have a JOptionPane full of JLabels, JTextFeilds, and Buttons, but I have so many things inside the dialogue box that it is starting to become bigger than my screen.
How do I shrink the dialog box and add a scroll bar to a JOptionPane?
I created the dialog box by creating a panel, adding the all myJLabels, JTextFeilds, and Buttons to it, adding the panel to my frame, and then:
JOptionPane.showConfirmDialog(frame1, panel1, "Please Enter Character Information", JOptionPane.OK_CANCEL_OPTION);
This is what I want to add a vertical scroll bar to
You have part of the answer already, instead of passing panel1 as the "message" parameter, wrap in a JScrollPane first
JOptionPane.showConfirmDialog(frame1, new JScrollPane(panel1), "Please Enter Character Information", JOptionPane.OK_CANCEL_OPTION);
Now, this might only solve part of the problem. Since JScrollPane uses the preferredSize of the component as a bases for calculating the viewport's size, this might not help you.
You might need to implement the Scrollable interface be provide a smaller view rectangle via the Scrollable#getPreferredScrollableViewportSize. The JScrollPane will then use this value as part of it's own preferredSize calculation

Scroll bar not visible in JPanel

I have created a GUI in Java which looks as shown below -
'panel_mid' is the white panel in the middle. I have added it to a scrollpane called 'panel_mid_scrollpane'.
Apart from 'panel_mid' there are more panels -
panel_left (containing 'back' button)
panel_right (visible on right hand side)
Revelant code for this gui is -
panel_mid.setBorder(grayborder);
panel_mid.setBounds(0, 0, 1100, 1060);
panel_mid.setBackground(Color.white);
panel_mid.add(obj.create_test_add_section);
panel_mid_scrollpane = new JScrollPane(panel_mid);
panel_mid_scrollpane.setLocation(150, 20);
panel_mid_scrollpane.setSize(1000, 660);
panel_mid_scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
The Add Section button shown in panel_mid, adds a section to the middle panel, every time it is clicked. When this button is clicked multiple times, the gui looks like -
As you could see, the scrollbar does not appear automatically as panels are added, the last panel is thus only half visible. What could be causing this problem ?
Thanks !
Scrollbars appear automatically when the preferred size of the component added to the scrollpane is greater than the size of the scroll pane.
You appear to be using a null layout.
//panel_mid.setBounds(0, 0, 1100, 1060);
panel_mid.setBackground(Color.white);
panel_mid.add(obj.create_test_add_section);
panel_mid_scrollpane = new JScrollPane(panel_mid);
//panel_mid_scrollpane.setLocation(150, 20);
//panel_mid_scrollpane.setSize(1000, 660);
Don't use a null layout with setSize() and setLocation. Swing was designed to be used with layout managers. If you use layout managers then the scrollbar will work automatically and the size and location will be calculated automatically for you.
Read the Swing tutorial on Layout Mangers.
You must tell the GUI to refresh, so that the containers are laid out again. This will show the container that it also has to show a scrollbar.
So in the ActionListener or whatever you use to add a section, add code like:
container_with_sections.validate();
container_with_sections.repaint();
where container_with_sections is the container (JContainer) which contains the JScrollPane, or a container which contains a container which contains the JScrollPane, and so on.

Netbeans null layout causing border around background when resizable unchecked

I'm making a simple JFrame with the GUI editor in netbeans with a background image set as an icon in a label as suggested by the netbeans site, with a label and a button centered. I was having a very hard time centering them without using the null layout and setting the pixels to center them. I have an 800X600 image as the background, and I don't want the window to be resizeable. So I unchecked resizeable in the properties, and on the code tab I have designer size set to 800, 600, generate size is checked, and the form size automatically sets to 816, 638. This then gives me a border around the right and bottom sides of a few pixels. If I change the Form Size to 800, 600, then the background image is cut off by a few pixels. One other thing that I set that may impact that is in the properties=>bounds set to 800, 600, 800, 600.
Any advice on how to get rid of the border without allowing the window to be resizeable as well as any on whether a different layout can help with centering would be greatly appreciated. I did find some information that Grid Bag layout would help, but I wasn't quite able to get it working correctly. I suppose that writing out the code instead of using the GUI editor may also be a better alternative, but I'm pretty new so any advice on that would be great as well.
Don't use null layout when you can center components quite easily if you use the correct layout or combination of layouts. For instance if you want a JLabel next to a JButton and have them centered in a JPanel, put the JLabel and JButton into their own JPanel first (make sure to have this JPanel's opaque property set to false) and then have the containing JPanel use GridBagLayout. If you add one component (the inner JPanel) without GridBagConstraints, the component is centered automatically, even if the containing JPanel is resized. It's almost idiot-proof, whereas null layout is a recipe for difficult hard to maintain code.

Java's FlowLayout does not resize correctly

I created a JFrame initialized with a BorderLayout and a JScrollPane as its CENTER element.
The scroll pane is set with VERTICAL_SCROLLBAR_ALWAYS and HORIZONTAL_SCROLLBAR_NEVER policies. The intent of my frame is to have a controlled width, while the height should grow/shrink as data is added/removed.
Inside my scroll pane, I added a simple JPanel (lets call it the content panel) which is initialized with a FlowLayout (and LEADING policy).
In order to test this, I simply populate my content panel with 20 JLabel("Item " + n) components where n is the loop counter.
I would expect to see my labels shown on a single row if the frame is large enough and the labels wrap to other lines when I shrink the width. But instead, there is only a single line displayed with no wrapping... ever.
Does anyone know why the flow layout does not wrap when a scroll pane is involved?
If I remove the scroll pane all together and put the content panel directly in the frame, the desired wrapping effect occurs, but if the frame height is shrunk smaller than the content panel height it just disappears.
The idea is that I want my labels to be wrapped when necessary but also always be visible if it means having to scroll up/down.
Any suggestions on how to fix this?
Thanks.
Wrap Layout gives an explanation and a solution.
If you work with the designer, you have to set the prefferedSize property to null (delete what is set) then set the preferred size by clicking the triple dots [...] button next to the prefferedsize property name and put your preferred value.
I encountered the same problem and it works for me.

Categories

Resources