Is setting preferred size of JFrame
using setPreferredSize()
considered bad?
If it is bad, what is good way to change JFrame
window size to dimension I need.
I know laying components in way to reflect final JFrame dimension that I need. But if I use shortcut to change preferred size using call setPreferredSize()
just before call to pack()
to change final JFrame
size is bad? If so why?
For example I have sample form:
This is displayed without setting preferred size.
Now I can resize form with call to setPreferredSize()
before call to pack()
.
This is displayed with call: setPreferredSize(new Dimension(500, 300));
I can have similar effect with setting components size while laying it out. But what is disadvantage of setting frame size with just call to setPreferredSize()
.
I can think setting preferred size as resizing displayed window manually with mouse after it has been displayed. Isn't it?
Code:
import java.awt.Dimension;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class MyFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField fullNameTextField = new JTextField();
private JTextField emailIDTextField = new JTextField();
private JTextArea addressTextArea = new JTextArea();
private JButton submitButton = new JButton("Submit");
private JButton cancelButton = new JButton("Cancel");
public MyFrame(){
super("MyFrame");
layoutComponents();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationByPlatform(true);
setPreferredSize(new Dimension(500, 300));
pack();
}
private void layoutComponents(){
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
JLabel fullNameLabel = new JLabel("Full Name:");
JLabel emailIDLabel = new JLabel("Email ID:");
JLabel addressLabel = new JLabel("Address:");
JScrollPane addressScrollPane = new JScrollPane(addressTextArea);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(fullNameLabel)
.addComponent(emailIDLabel)
.addComponent(addressLabel)
)
.addGap(15)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(fullNameTextField, 10, 200, Short.MAX_VALUE)
.addComponent(emailIDTextField)
.addComponent(addressScrollPane)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(submitButton)
.addGap(10)
.addComponent(cancelButton)
)
)
)
)
.addGap(10)
)
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(fullNameLabel)
.addComponent(fullNameTextField)
)
.addGap(5)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(emailIDLabel)
.addComponent(emailIDTextField)
)
.addGap(5)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(addressLabel)
.addComponent(addressScrollPane, 20, 60, Short.MAX_VALUE)
)
.addGap(15)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(submitButton)
.addComponent(cancelButton)
)
.addGap(10)
)
);
layout.linkSize(submitButton, cancelButton);
getRootPane().setDefaultButton(submitButton);
}
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MyFrame().setVisible(true);
}
});
}
}