How do I center a JTextfield in a JButton [duplicate]

1 week ago 10
ARTICLE AD BOX

How can I center a JTextField.

NOTE! I do not want to center the text in the JTextField (which is achieved using setHorizontalAlignment(JTextField.CENTER), but rather I want to center the actual JTextField in the space it is in, just as jLabel.setHorizontalAlignment(SwingConstants.CENTER) would do.

mKorbel's user avatar

mKorbel

110k20 gold badges138 silver badges331 bronze badges

asked Jan 2, 2013 at 5:18

CodeGuy's user avatar

4

Are you using any layout? Using GridBagLayout with appropriate GridBagConstraints can do this. <BR>The setHorizontalAlignment behaves the same way for both JTextField and JLabel, both have their content aligned accordingly. You probably do not notice it in JLabel due to the gray colour of the background panel.

2013-01-02 05:25:48 +00:00

Commented Jan 2, 2013 at 5:25

You have to use proper layout for that. Post your code if you cant get.

2013-01-02 05:27:11 +00:00

Commented Jan 2, 2013 at 5:27

FlowLayout.CENTER - not sure, but something like that if you are using flowlayout

2013-01-02 05:29:39 +00:00

Commented Jan 2, 2013 at 5:29

I am using null layout. There must be a way with null layout.

2013-01-02 05:31:56 +00:00

Commented Jan 2, 2013 at 5:31

Read Entire Article