set minimum size for GridBagLayout
Hi all,
I have a dialog which using GridBagLayout. I spent quite a lot of time to test, but still got the following problems. Could anyone give me some solutions?
1. How do I set the dialog to have a minimum size ?
Since the dialog is sizeable, so I have to make this limitation to prevent the dialog being too small .
I tried the following code:
this.getContentPane().setMinimumSize(new Dimension(686,520));
this.setMinimumSize(new Dimension(686,520));
but it's still not work... , I can still resize it too small..any other way ?
2. How do I set its default size when dialog pop up?
I don't know why the dialog always auto popup in maximum size.
I have tried to usethis.setSize(new Dimension(686, 536));inside jbInit() function, but still not work...
Please help ... Thanks a lot.
# 1
Why dont you try this.setResizable(false);
?
Anyway I have given a sample program here using Gridbag layout and this dialog cannot be resized.
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SunExample28 extends JDialog {
public SunExample28() {
setTitle("JDialog with Fixed size");
JLabel nameLbl = new JLabel("Name:");
JLabel ageLbl = new JLabel("Age:");
JTextField nameFld = new JTextField(10);
JTextField ageFld = new JTextField(10);
GridBagConstraints gbc = new GridBagConstraints();
Insets in = new Insets(5,5,5,5);
JPanel compPanel = new JPanel(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
compPanel.add(nameLbl,gbc);
gbc.insets = in;
gbc.gridx = 1;
gbc.gridy = 0;
compPanel.add(nameFld, gbc);
gbc.insets = in;
gbc.gridx = 0;
gbc.gridy = 1;
compPanel.add(ageLbl, gbc);
gbc.insets = in;
gbc.gridx = 1;
gbc.gridy = 1;
compPanel.add(ageFld, gbc);
JButton okBtn = new JButton("OK");
JButton cancelBtn = new JButton("Cancel");
JPanel btnPanel = new JPanel();
btnPanel.add(okBtn);
btnPanel.add(cancelBtn);
getContentPane().add(compPanel, BorderLayout.CENTER);
getContentPane().add(btnPanel, BorderLayout.PAGE_END);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(500,300);
setResizable(false);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new SunExample28().setVisible(true);
}
}
# 3
If this.setResizable(false);
The dialog cannot be resized. But my dialog need to be resizeable, coz sometime user need to view more things..
now just want to fix a minimum size for user resize to...
e.g. the dialog can only be resize to 600 * 400 in minimum...
I still can't find method to do this....