Since JTextField
holds only a single line of text, you might be better off sizing the JTextField
more appropriately.
As far as I know there is no good way to do exactly what you asked for. Note that while there is setHorizontalAlignment()
, there is no corresponding setVerticalAlignment()
. setAlignmentY()
doesn't do it either (that sets the alignment of components added to a container, which doesn't make sense for a JTextField
).
You can fake it by placing the JTextField
in the north of the BorderLayout
, as follows:
tf.setBorder(null);
f.setBackground(Color.WHITE);
f.add(tf, BorderLayout.NORTH);
f.add(Box.createVerticalGlue(), BorderLayout.CENTER);
But I suspect what you really want may be multiple lines, in which case you should be using a JTextArea
.