How can I resize a JTextField?
16,818
Solution 1
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.
Solution 2
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.
Author by
LockOn
Yes as you can see i'm still in learning stage of my java dev. journey. :)
Updated on June 04, 2022Comments
-
LockOn almost 2 years
How can I resize a JTextField?
-
Jay Askren about 14 yearsLayout managers will call setSize() and override your size. It is better to use setPreferredSize(), setMinimumSize(), and setMaximumSize().
-
Brian Agnew over 11 years@JayAskren - noted and amended. Thx
-
kleopatra over 11 yearsactually, you shouldn't call setXXSize ever, some reasons: stackoverflow.com/a/7229519/203657 Instead, choose a LayoutManager that fits your need @JayAskren
-
Jay Askren over 11 years@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().