How to align text inside JTextField?

I have a JTextField with fairly large borders, however the text does not behave the way I want.

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

how would I do this so that my text is aligned as follows

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

Edit: Using JTextAreafixed my problem. Thank.

+5
source share
2 answers

JTextArea aligns up.

Or use JLabel:

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

and call:

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

Layout managers are another way to do this: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

+11
source

This is a pretty old question, but I needed it, and it solved it for me.

: , i18n . i18n, javax.swing.plaf.basic.BasicTextFieldUI.create(Element), .

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

}

0

All Articles