7

I have a JTextField with rather big bounds, the text however does not behave like I want it to.

 _________________
|                 |
|                 |
|text             |
|                 |
|_________________|

how would i do it so my text aligns like this

 _________________
|text             |
|                 |
|                 |
|                 |
|_________________|

Edit: Using JTextArea fixed my problem. Thank you.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
Adrian Jandl
  • 2,985
  • 4
  • 23
  • 30
  • Why do you have such a text field in the first place? Why not let it take its preferred height, and have a good-looking UI? – JB Nizet Mar 23 '13 at 07:48
  • How do I do that? Currently I have a `setBounds(10,10,200,400)` do I change that to `setLocation(10,10)` and `setPreferredSize(200,400)`? – Adrian Jandl Mar 23 '13 at 07:53
  • You should never call `setBounds()` and `setLocation()`. That's the job of the layout manager. You shouldn't call setPreferredSize() either. The JTextField knows its preferred size based on the number of columns you specified when constructing it. Use layout managers: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html – JB Nizet Mar 23 '13 at 07:58
  • 2
    Why not you go for JTextArea? – Vishal K Mar 23 '13 at 08:37
  • 4
    Why do you commentators insist on questioning what looks good on a user interface you've never seen? – Erick Robertson Apr 25 '14 at 15:10

3 Answers3

13

JTextArea aligns to the top.

Or use a JLabel:

JLabel myLabel = new JLabel("my text");

and the call:

myLabel.setHorizontalAlignment(SwingConstants.LEFT);
myLabel.setVerticalAlignment(SwingConstants.TOP);

Layout managers is the another way of doing this: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

Boann
  • 48,794
  • 16
  • 117
  • 146
1ac0
  • 2,875
  • 3
  • 33
  • 47
1

It's a quite old question but I needed it and this solved it for me.

Please note: This solution assumes that no i18n is used. If you enabled i18n functions, then have a look at javax.swing.plaf.basic.BasicTextFieldUI.create(Element) to find the right class.

You can customize the vertical text position in the FieldView:

public static class MyTextfieldUI extends com.sun.java.swing.plaf.windows.WindowsTextFieldUI /* TODO which UI class is really used? */ {

    public static ComponentUI createUI(JComponent c) {
        return new MyTextfieldUI();
    }

    @Override
    public View create(Element elem) {
        return new FieldView(elem) {

            @Override
            protected Shape adjustAllocation(final Shape shape) {
                if (shape instanceof Rectangle) {
                    final Rectangle result = (Rectangle) super.adjustAllocation(shape);
                    /* set vertical text position to top */
                    result.y = ((Rectangle) shape).y;
                    return result;
                }

                return super.adjustAllocation(shape);
            }
        };
    }
}

public class Test extends JPanel {

private Test() {
    super(new BorderLayout());

    final JTextField northField = new JTextField();
    northField.setBackground(Color.YELLOW);
    northField.setText("north");

    final JTextField eastField = new JTextField();
    eastField.setBackground(Color.GREEN);
    eastField.setText("east");
    eastField.setPreferredSize(new Dimension(200, -1));

    final JTextField centerField = new JTextField();
    centerField.setBackground(Color.CYAN);
    centerField.setText("center");
    centerField.setHorizontalAlignment(SwingConstants.CENTER);

    final JTextField westField = new JTextField();
    westField.setBackground(Color.GRAY);
    westField.setText("west");
    westField.setHorizontalAlignment(SwingConstants.RIGHT);
    westField.setPreferredSize(new Dimension(200, -1));

    final JTextField southField = new JTextField();
    southField.setBackground(Color.MAGENTA);
    southField.setText("south");
    southField.setHorizontalAlignment(SwingConstants.RIGHT);

    add(northField, BorderLayout.NORTH);
    add(eastField, BorderLayout.EAST);
    add(centerField, BorderLayout.CENTER);
    add(westField, BorderLayout.WEST);
    add(southField, BorderLayout.SOUTH);
}

public static void main(String[] args) throws Exception {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

    /* Set custom look and feel for all text fields */
    UIManager.put("TextFieldUI", MyTextfieldUI.class.getName());

    final JFrame frame = new JFrame("TextDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new Test());
    frame.setSize(800, 500);
    frame.setVisible(true);
}

}

Schaedling
  • 11
  • 1
0
mytext.setHorizontalAlignment(SwingConstants.LEFT);
mytext.setVerticalAlignment(SwingConstants.TOP);

this is enough, and works for JTextField

Suitcases
  • 17
  • 2