JDialog preferred size is not big enough
Hello,
i 'have a JDialog with "lots" of JComponent in it. I would like to set
the size of the JDialog with the preferred size of itself, but it
doesn't work (the preferred height seems too small). here is a sample code.
I guess i must add some height, but which one?
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class TestPreferred extends JDialog {
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JButton jbOk = null;
private JButton jbCancel = null;
private JPanel jPanel1 = null;
private JLabel jLabel = null;
private JComboBox jComboBox = null;
private JSlider jSlider = null;
private JTextField jTextField = null;
private JCheckBox jCheckBox = null;
private JCheckBox jCheckBox1 = null;
public TestPreferred(Frame owner) {
super(owner);
initialize();
setSize(getPreferredSize());
}
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.add(getJbOk(), new GridBagConstraints());
jPanel.add(getJbCancel(), new GridBagConstraints());
dumpSizes("jPanel", jPanel);
}
return jPanel;
}
private JButton getJbOk() {
if (jbOk == null) {
jbOk = new JButton();
jbOk.setText("OK");
dumpSizes("jButton", jbOk);
}
return jbOk;
}
private JButton getJbCancel() {
if (jbCancel == null) {
jbCancel = new JButton();
jbCancel.setText("Cancel");
dumpSizes("jButton1", jbCancel);
}
return jbCancel;
}
private JPanel getJPanel1() {
if (jPanel1 == null) {
GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
gridBagConstraints5.gridx = 1;
gridBagConstraints5.gridy = 4;
GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
gridBagConstraints4.gridx = 1;
gridBagConstraints4.gridy = 3;
GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
gridBagConstraints3.fill = GridBagConstraints.VERTICAL;
gridBagConstraints3.gridy = 2;
gridBagConstraints3.weightx = 1.0;
gridBagConstraints3.gridx = 1;
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.fill = GridBagConstraints.VERTICAL;
gridBagConstraints2.gridy = 1;
gridBagConstraints2.weightx = 1.0;
gridBagConstraints2.gridx = 1;
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.fill = GridBagConstraints.VERTICAL;
gridBagConstraints1.gridy = 0;
gridBagConstraints1.weightx = 1.0;
gridBagConstraints1.gridx = 1;
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
jLabel = new JLabel();
jLabel.setText("JLabel");
jPanel1 = new JPanel();
jPanel1.setLayout(new GridBagLayout());
jPanel1.add(jLabel, gridBagConstraints);
jPanel1.add(getJComboBox(), gridBagConstraints1);
jPanel1.add(getJSlider(), gridBagConstraints2);
jPanel1.add(getJTextField(), gridBagConstraints3);
jPanel1.add(getJCheckBox(), gridBagConstraints4);
jPanel1.add(getJCheckBox1(), gridBagConstraints5);
dumpSizes("jPanel1", jPanel1);
}
return jPanel1;
}
private JComboBox getJComboBox() {
if (jComboBox == null) {
jComboBox = new JComboBox();
}
return jComboBox;
}
private JSlider getJSlider() {
if (jSlider == null) {
jSlider = new JSlider();
}
return jSlider;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setColumns(20);
}
return jTextField;
}
private JCheckBox getJCheckBox() {
if (jCheckBox == null) {
jCheckBox = new JCheckBox();
jCheckBox.setText("check1");
}
return jCheckBox;
}
private JCheckBox getJCheckBox1() {
if (jCheckBox1 == null) {
jCheckBox1 = new JCheckBox();
jCheckBox1.setText("check2");
}
return jCheckBox1;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestPreferred(null).setVisible(true);
}
});
}
private void initialize() {
this.setContentPane(getJContentPane());
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJPanel(), BorderLayout.SOUTH);
jContentPane.add(getJPanel1(), BorderLayout.CENTER);
dumpSizes("ContentPane", jContentPane);
}
return jContentPane;
}
private void dumpSizes(String name, JComponent jcomp) {
System.err.println(name + " : pref size = " + jcomp.getPreferredSize());
}
}