How do I get the JTextField
to have a fixed height when the Frame is maximized? I want it to look sort of similar to the Skype application on Ubuntu.
private JTextField username;
private JPasswordField password;
private JLabel usernamelabel;
private JLabel passwordlabel;
public LoginPanel(){
setSize(200,200);
setLayout(new GridLayout(4,4));
setBackground(new Color(85,153,187));
setBorder(BorderFactory.createEmptyBorder(70, 70, 70, 70));
username = new JTextField();
password = new JPasswordField();
usernamelabel= new JLabel("Username");
passwordlabel= new JLabel("Password");
username.setBounds(5, 5, 100, 100);
username.setPreferredSize(new Dimension(80,20));
password.setPreferredSize(new Dimension(80,20));
add(usernamelabel);
add(username);
add(passwordlabel);
add(password);
Don't use a layout other than GridLayout
or put the text field in another panel that has a FlowLayout
that sits inside of your GridLayout
.
I think this solves your problem.
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
JFrame failFrame = new JFrame();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(0, 4));
JPanel leftTextFieldPanel = new JPanel();
leftTextFieldPanel.setLayout(new FlowLayout());
JPanel rightTextFieldPanel = new JPanel();
rightTextFieldPanel.setLayout(new FlowLayout());
JTextField leftTextField = new JTextField();
leftTextField.setPreferredSize(new Dimension(150,20));
JTextField rightTextField = new JTextField();
rightTextField.setPreferredSize(new Dimension(150,20));
leftTextFieldPanel.add(leftTextField);
mainPanel.add(leftTextFieldPanel);
rightTextFieldPanel.add(rightTextField);
mainPanel.add(rightTextFieldPanel);
mainPanel.add(new JPanel());
mainPanel.add(rightTextFieldPanel);
mainPanel.add(new JPanel());
failFrame.add(mainPanel);
failFrame.setSize(600, 400);
failFrame.setLocationRelativeTo(null);
failFrame.setVisible(true);
}
}
I used nested Layouts. On the Top is your GridLayout and the textfields are in a FlowLayout which are added on the gridLayout panel. So the textfields are no longer stretched.
You can refer to here.
The setMaximumSize method can be found in JComponent and the JTextField gets to use this method from JComponent.
EDIT: However, it seems its not a good practice to hardcode the size of the JTextField particularly when you are running it on multiple platforms as you may face issues with the font-size.
Thanks for all the help guys, hope this helps out others in the future. I got the visual look that I was looking for.
private JTextField username;
private JPasswordField password;
private JPanel [] login = {new JPanel(), new JPanel()};
private JLabel usernamelabel;
private JLabel passwordlabel;
public LoginPanel(){
setSize(200,200);
setBackground(new Color(85,153,187));
setBorder(BorderFactory.createEmptyBorder(70, 70, 70, 70));
username = new JTextField();
password = new JPasswordField();
usernamelabel= new JLabel("Username");
passwordlabel= new JLabel("Password");
login[0].add(usernamelabel);
login[0].add(username);
login[1].add(passwordlabel);
login[1].add(password);
for(JPanel p : login){
p.setBackground(new Color(85,153,187));
this.add(p);
}
username.setBounds(5, 5, 100, 100);
username.setPreferredSize(new Dimension(120,20));
password.setPreferredSize(new Dimension(120,20));
}
In a pinch, doing
tf.setMaximumSize(tf.getMinimumSize())
will keep it constant size and platform neutral.