3

How can I resize a JTextField?

Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
LockOn
  • 313
  • 2
  • 6
  • 16
  • 1
    I, and apparently 1910 others, do not see why this question was closed as not constructive. It doesn't need to be a super long question to be constructive. – CodyBugstein Dec 04 '12 at 22:46

2 Answers2

4

If you look at the Javadoc, you'll see that JTextField derives from Component, and that has a setSize() method. If you don't have a layout manager then that's of use.

If you do have a layout manager, then setPreferredSize()/setMinimumSize()/setMaximumSize() is the way to go. See this SO answer for more details.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • 6
    Layout managers will call setSize() and override your size. It is better to use setPreferredSize(), setMinimumSize(), and setMaximumSize(). – Jay Askren Mar 14 '10 at 12:41
  • @JayAskren - noted and amended. Thx – Brian Agnew Aug 24 '12 at 15:52
  • actually, you shouldn't call setXXSize _ever_, some reasons: http://stackoverflow.com/a/7229519/203657 Instead, choose a LayoutManager that fits your need @JayAskren – kleopatra Aug 24 '12 at 16:13
  • @Kleopatra That is fair. If you use one of the third party layout manager's mentioned, you may not need to use setXXXSize(). If you use standard Java layout managers, especially GridBagLayout, you can't get away without using setXXXSize(). Either way, you should never call setSize(). – Jay Askren Aug 24 '12 at 17:50
4

The jtextfield tutorial tells you about setColumns(), which might be what you want. Otherwise, you might need to learn about how to use layout managers - here's a tutorial.

John
  • 6,701
  • 3
  • 34
  • 56