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
Related
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.
Here's the code:
JOptionPane pane = new JOptionPane(findArray, JOptionPane.QUESTION_MESSAGE, JOptionPane.DEFAULT_OPTION);
pane.setOptions(new Object[]{findPreviousButton, findNextButton});
final JDialog dialog = pane.createDialog(myJFrame, "Find");
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
findArray consists of JLabel findLabel and JTextField findField. myJFrame is the JFrame. findPreviousButton and findNextButton are the two JButtons I am replacing the default "OK" and "Cancel" buttons with. The both have custom icons and no text. The JDialog window is making their icons a certain size making them look pixelated. How do I resize the buttons so that the width is 60 and the height is 30? The method .setSize(int, int) doesn't work and neither does .setBounds(int, int, int, int)
Adjusting the button margins should help:
http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setMargin(java.awt.Insets)
You'll probably want to use .setPreferredSize() instead of .setSize(). Usually when I want to override a component's natural size (meaning how it's laid out by whatever layout manager I'm using), I'll use that along with setting the minimum size as well. If a layout manager is modifying the component's natural size, the preferredSize() might be a better option. There's a good stackoverflow discussion about the differences here:
Java: Difference between the setPreferredSize() and setSize() methods in components
I want to make a layout with two componenents in a vertical row.
The first component is an instance of my own class, ImagePanel, that extends Panel and shows an image, and I want it to take up the exact space that it needs to show the entire image. The remaining space should then be filled by the other component (in this case another Panel with a GridLayout). See the picture.
In Android you can do this by using the weight property, but I have not been able to find anything like that in Java, and I canĀ“t see that any of the standard layout managers in Java would be suitable for this.
I tried putting the ImagePanel in BorderLayout.NORTH and the other panel in BorderLayout.CERNTER, but the second panel was then overlapping the image, so that didn't work.
I also thougth about using a GridLayout but the grid would not care about the size of the image, so I don't think that would work either.
Any help is appreciated.
A BorderLayout should work, provided the image panel's getPreferredSize() returns the correct dimension (i.e. the dimension of the image it displays).
Using a simple JLabel containing an ImageIcon and no text instead of your custom ImagePanel would do that for you.
The most powerfull layout in java is GridBagLayout. It has weightx and weighty properties, anchor, fill, etc.
By default, with GridBagLayout each component has all the necessary space to show fully. If you want the second panel to expand, it should be enough with weighty=0.0 in your image and weighty=1.0 in your other panel.
The javax.swing.Box should be exactly what you want. There are 2 types of boxes, vertical and horizontal, you need a vertical:
Box box = Box.createVericalBox();
box.add(comp1);
box.add(Box.createVerticalStrut(5)); // add some empty space
.
.
.
add(box);
Box will only force one side to be equal, for vertical, it's the width, the length could be any size.
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.
The text in my JOptionPanes are way to small I was wondering how I could change the font of the text inside the pane. Also, I would like to be set a space between two buttons.
Something like
|Canecl| |DoneSave| |Save|
I guess you mean the fonts on your JButtons inside the JOptionPane are too small.
For that I suggest using Swing Utils by Darryl Burke. Now you can easily do e.g.
for (JButton button : SwingUtils.getDescendantsOfType(JButton.class, pane)) {
button.setFont(new Font("Verdana", Font.BOLD, 32));
}
to set the fonts on all JButtons inside of JOptionPane pane.
If you want to set the font of the JOptionPane message itself, use e.g.
UIManager.put(
"OptionPane.messageFont",
new FontUIResource(new Font("Verdana", Font.BOLD, 32))
);
In regard to the "spacing of buttons" question: I don't know if that can be done without extending & make a customized JOptionPane yourself.
You cannot specify a spacing between just 2 buttons out of 3 (as per the OP's question) but you can increase the size of the spacing between ALL the buttons :
JPanel buttonPanel = (JPanel) myOptionPane.getComponent(1);
BasicOptionPaneUI.ButtonAreaLayout lm2 = (BasicOptionPaneUI.ButtonAreaLayout) buttonPanel.getLayout();
lm2.setPadding(20);
lm2.setSyncAllWidths(false); // buttons will vary in size as needed.
or perhaps something like :
lm2.setPadding(lm2.getPadding() * 2) // double the spacing between ALL buttons.
If there were just 2 buttons, then using these LayoutManager calls you could achieve the desired outcome.
However for varying-sized spaces between the buttons you need to implement your own JDialog where you control the layout of its JPanel yourself.
A JOptionPane is made up of 2 JPanels... one for the message (and icon) itself, and one for the buttons, that's why we getComponent(1) above.
I know the question is 4 years old but I made this answer because I had a similar need today and couldn't find the answer ANYWHERE on Stack overflow or elsewhere.