10

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);
jzd
  • 23,473
  • 9
  • 54
  • 76
unleashed
  • 109
  • 1
  • 1
  • 4

5 Answers5

6

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.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • So your saying create JPanels that have a FlowLayout and add the TextFields to them then add them to the the Panel containing the the Grid Layout? I can see how that would work but isn't there a neater way of doing this? I'm totally against Netbeans GUI genrators. :) – unleashed Apr 01 '11 at 21:07
  • Yes, layering different layouts is an easy and common way to generate desired layout effects. – jzd Apr 01 '11 at 21:15
  • +1 for nested layouts. As a convenience, `JPanel` defaults to `FlowLayout`. – trashgod Apr 01 '11 at 21:20
  • 1
    -1 for nested layouts ;-) That's only emergency hacking if 3rd party LayoutManagers are not an option. If they are, do some research and choose the one that best fits, likely landing at one of the "big three" (FormLayout, MigLayout, DesignLayout): they all are powerful as well as easy to handle. – kleopatra Apr 02 '11 at 10:52
4

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.

Jan Koester
  • 1,178
  • 12
  • 26
3

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.

Community
  • 1
  • 1
Shankar Raju
  • 4,356
  • 6
  • 33
  • 52
  • 11
    In a professional Java GUI app, *never* set the preferred size explicitly - you are making an assumption that the font size for the component will *always* fit in 20 pixels of height. If the user has large fonts set on their desktop, that will not be true. Use a layout manager that honors the preferred size of the JTextField - which defaults to the right height for the desktop the application is running on. Hard-coding is guaranteed to be wrong some of the time (Mac OS, in particular, has larger default font heights than Windows). This sort of thing is one of the reasons layout managers exist. – Tim Boudreau Apr 02 '11 at 06:04
  • Oh. I didn't know about that. Seems to be a valid point. Will edit my post. – Shankar Raju Apr 02 '11 at 06:08
0

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));

} 
unleashed
  • 109
  • 1
  • 1
  • 4
0

In a pinch, doing

tf.setMaximumSize(tf.getMinimumSize())

will keep it constant size and platform neutral.

Awais Qarni
  • 17,492
  • 24
  • 75
  • 137
  • That's correct, so if the LayoutManager respects these hints it will work, if not, it won't. Works for Flow and Box layouts and keeps the field constant size, both height and width. – aptappman Nov 08 '11 at 15:18
  • not entirely correct either: a) max has no effect whatever on Flow b) true: box respects max - but most probably (didn't try, though ;-) will get confused if min < pref (which holds true for most components. Anyway, it's _wrong_ to ever use any of the setXXSize methods in application code: http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi - instead, use a LayoutManager that does what you need – kleopatra Nov 08 '11 at 15:39