Reputation: 6272
I'm creating a JDialog with netbeans as my IDE. I change the size to how I want it in netbeans, and I can see it updating the prefferredSize, but when I run my application it looks like this:
Is there something else I need to set to set the size? Or at least make it sized properly so I can see the controls... (theres 6 on it)
Upvotes: 3
Views: 5225
Reputation: 1819
Download latest Netbean IDE from https://netbeans.org/downloads/
And set the following property:
Good luck!
Upvotes: 1
Reputation:
In the form designer switch to the "Code" tab of the properties window. There is a property named "Form Size Policy".
This defaults to pack() which means that the dialog is resized to be as big as the contained components require it. For more details please read the Javadocs for the pack() method. Without knowing what components you put on the dialog, it's hard to tell why pack() doesn't work for you.
If you change that to "Generate Resize Code" the dialog will stick to the size you define:
Upvotes: 4
Reputation: 12092
I guess you might be using the Netbeans GUI builder, possible solutions/suggestions,
dialog.pack();
btw, this works for me ,
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DialogsTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setSize(400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
JPanel message = new JPanel();
message.add(new JLabel("This is a dialog :)"));
JDialog dialog = new JDialog(f, "Dialog");
dialog.setContentPane(message);
dialog.pack();
dialog.setLocationRelativeTo(f);
dialog.setVisible(true);
}
});
}
}
Upvotes: 3
Reputation: 2860
Use the setSize()
method instead. For some reason this works, but setPreferredSize()
doesn't (at least not as expected).
By the way, it's usually advised not to set a specific size on components.
Upvotes: 0