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
Related
I have two simple JDialog dialogs that should be small. The first has a JTextField of 5 columns and a vertical slider under it. The second has a JLabel, a JSpinner and two JButton(s). Both, after pack() and setVisible(), are too wide. For example, the first dialog has a preferred size of about 80, but shows up with width of 258.
Here are a few things that do not work: 1) a custom layout with the right preferred size computations vs. GridBagLayout; 2) overriding getPreferredSize(); 3) setSize and setBounds before or after setVisible; 4) a custom root pane UI with a custom title pane; 5) a forced constant width set on componentResized; 6) removing the slider (in case there is confusion between vertical and horizontal sliders width and height); 7) removing all controls and the dialog title.
In general, the dialogs can be resized by the user to have smaller width (by dragging the corners), but not programmatically (they can be resized programmatically with setBounds to have smaller height).
The dialogs do have the right sizes under some commercial look and feels, but not under Metal or Nimbus. Under both Metal or Nimbus, the preferred and minimum sizes of the dialog, root panes, glass panes, layered panes make sense. The size of the dialog itself doesn't.
I have tested this without setting a look and feel (which, on Windows, presumably means Metal) and it does not work.
I know that the width of 258 is set in addNotify in Dialog, on getComponentFactory().createDialog(this).
I assumed this could be related to the title portion of the dialog, but the icons there are not of some significant size.
Any ideas are appreciated.
My next move will be to create a simple standalone JDialog or a JFrame that calls a JDialog, outside of the main application with which I am working now. However, I cannot see anything special happening in the larger application.
Here is the code that will produce a dialog with the same width (looks like):
public class Main
{
public static void main(String[] args)
{
JDialog dlg = new JDialog();
dlg.pack();
dlg.setVisible(true);
}
}
I have a JTextField in my JFrame and I set the text in the JTextField.
When the text is long, JTextField gets the text's length and then jTextField's new width is equal to the text. It changes my window's shape and other components' places. How can I make JTextFields have static width so they won't be resized based on the length of the text that pass in?
The simplest thing you can do is set the column width, usually during initialization of the JTextField, e.g.:
new JTextField("Hello World!", 5);
new JTextField(10);
But the container will have a layout manager -- if you didn't specify it explicitly, it likely has a default. JFrame starts with BorderLayout in the content pane, although if you've added any other panels between the JFrame and the JTextField, we'd need to know that to have a better sense of the layout manager.
Some layout managers will constrain the width of the field as well, which is another way you might address your problem.
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
I have a JDialog and I want to have it a certain, given size:
JDialog dialog = new JDialog();
dialog.setSize(800, 600);
dialog.setResizable(false);
Then I add a component:
JLabel label = new JLabel("Test");
dialog.add(label);
Now I could make the dialog visible and check the size of the component
dialog.setVisible(true);
System.out.println(label.getSize());
The answer would be "[width=784,height=562]". Obviously the component was resized to fill the whole client area / content pane of the dialog window. That's fine and as I want it.
Question: How can I obtain the final size of the components before calling setVisible(true)?
getPreferredSize() will not be the size I want because I want the component to adapt to the given dialog's size
dialog.pack() is also not the right thing because it resizes the dialog to the preferred size of the components which I don't want
dialog.validate() does nothing useful here, the size of the component is still 0
dialog.getLayout().layoutContainer(dialog) also does not set the size of the components
So I am at a loss here. I want to make the layoutmanager calculating the right sizes of all components and sub components before showing the dialog, adapted to the overall size of the dialog. But I don't know how.
I now found that it can be done as follows:
JDialog dialog = new JDialog();
JLabel label = new JLabel("Test");
dialog.add(label);
// pack(), setSize(), validate() in this order will
// set sizes on all components as wished
dialog.pack();
dialog.setSize(800, 600);
dialog.validate();
System.out.println(label.getSize());
Also here the output is "[width=784,height=562]" but the dialog is not yet visible. The important part is the combination of pack(), setSize(desiredSize) and validate() in this order. The pack() probably determines a new size of the dialog (preferred sizes of all components), that's why here the size has to be set afterwards and the validate() is responsible for the resizing of the components. Probably setVisible(true) which arrives at the same sizes is doing internally something similar.
It seems a bit of a waste to resize the components several times but without pack() also setSize() and validate() do not have any effect.
I guess the other answers were based on some misunderstanding because they implicitly always assumed that you want to have the preferred size, but there are cases, e.g. if the user resizes the dialog or if the dialogs size is fixed from the beginning where you cannot attain the preferred size and some components just have to fill the available space.
That was the layout problem here, having a given global size of the dialog and determining the size of the components as they fill the available space. LayoutManagers solve this problem quite nicely, however usually only after setVisible(true).
I tested a bit more:
// new dialog
JDialog dialog = new JDialog();
// new label, prints messages if resized or painted
JLabel label = new JLabel("Test") {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("Component painted.");
}
};
label.addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(ComponentEvent e) {
System.out.println("Resized: " + e.getComponent().getSize());
}
});
dialog.add(label);
System.out.println("Size after new JLabel: " + label.getSize());
// pack dialog - necessary for setSize/validate to work
dialog.pack();
System.out.println("Size after pack: " + label.getSize());
// set a size and validate changes sizes
dialog.setSize(800, 600);
dialog.validate();
System.out.println("Size after setSize and validate: " + label.getSize());
// set visible would have also done the trick
dialog.setVisible(true);
System.out.println("Size after setVisible(true): " + label.getSize());
// and another resizing (no validation neccessary)
dialog.setSize(300, 200);
// dispose
dialog.dispose();
And the output is
Size after new JLabel:java.awt.Dimension[width=0,height=0]
Size after pack: java.awt.Dimension[width=116,height=16]
Size after setSize and validate: java.awt.Dimension[width=784,height=562]
Size after setVisible(true): java.awt.Dimension[width=784,height=562]
Resized: java.awt.Dimension[width=284,height=162]
Resized: java.awt.Dimension[width=284,height=162]
Component painted.
I learned more about the inner workings of Swing:
ComponentResized events are not fired before setVisible(true) even if components are resized (their size changes)
ComponentResized events even with the same size can be fired several times in a row
Components might not be painted in between resizing if they follow each other fast enough
The first painting is in any case after setVisible(true) and the component will have the desired size (preferred size or defined by other constraints as here) by then.
If for some reason you must know the size of the components before the first drawing, do it with pack(), setSize(), validate()
I tested some more, also with maximized frames and now can combine all the results into: The first painComponent() is always with the right size and the related componentResized() event always follows afterwards, sometimes two times.However the LayoutManager must know before, otherwise the examples would not be drawn correctly. So in case one draws the background by itself, either read out the right size in every paintComponent or implement a custom layout manager or wait for the resized event and invoke repaint, so the component is drawn two times but it should work. Apllications include cases where the number of components to show depend on the size (as in my geographical map application).
Just to complete the picture I think the flow goes like this in case a user maximized or resized a frame/dialog:
frame/dialog.setSize()
LayoutManager.layoutContainer(frame/dialog) using the actual size
frame/dialog paint() using the layouted sizes
Resized() events fired for all components etc.
And pack() probably just calls setSize(layout.preferredLayoutSize()) as the first step.
So in case depending on the size you have to add or remove components for example, it could be a good idea to override setSize() and listen there for changes. I initially was listening for Resized() events but they arrive too late for the first drawing.
Top-Level Containers return own Size, PreferredSize, Bounds in two cases (if they are)
already visible
after call pack(), in your case dialog.pack();
have to calculating with Borders and ToolBar came from Native OS
have to get this size from Top-Level Containers#getContentPane().getWhatever
most of JComponents returns own PreferredSize, then there no reason to sizing for Standard Layout Manager
I search for a method, which gets called if the jpanel is shown on the display, because i have to fetch the real size of the panel.
Any suggestions?
Have you tried adding a ComponentListener to the JPanel? That would be where I would start with my code in the componentShown(...) method. For this to work, I think that you must call setVisible(true) on the JPanel after adding it to the display.
The other option is to simply query its size after calling pack(), or setVisible(true) on your GUI.
Edit
You state:
I added the panel to the gui designer.. when the window pops up, i wanna now the real size of the jpanel, because it can change it.
If you want to know the size of a component held by a window "when the window pops up", then add a WindowListener to the window and check the size of the component from the windowOpened(...) method.
Edit 2
Then you state:
after i have the real size, i add some subpanels, in relation to the size of this panel. so e.g. size/6 & the subpanel has now the size-height of size/6.
One Solution: Better not to set the sizes of anything but instead to use the right combination of layout managers and then let them set the proper sizes based on their rules.
Set the visibility using function setVisible(true);