0

I found that JTextFields can be manipulated easily with acm functions such as setLocation(), as in:

    textfield.setLocation(x,y);

However I cannot seem to be able to find a method that can set the default size of a JTextField. I tried using setPreferedSize() but it didn't change the size of the JTextField.

Any help would be fantastic.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
adm
  • 29
  • 1
  • 1
  • 2

1 Answers1

3

I cannot seem to be able to find a method that can set the default size of a JTextField.

I don't know that the ACM library has any special methods to do this. I would just look at the API for the JTextField:

JTextField textField = new JTextField(10);

Will make the text field larger enough to hold a minimum of 10 characters.

I found that JTextFields can be manipulated easily with acm functions such as setLocation()

Not sure what the ACM functions are, but you should not be using methods like setLocation(). That is the job of the layout manager to set the location of a component based on the rules of the layout manager that you are using.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • +1 Nice, though some had posted some other method for a second there – MadProgrammer Jun 28 '13 at 04:09
  • I must have articulated my problem poorly. I don't mean the amount of slots in the JTextField for new characters. I mean the actual width and length of JTextField. – adm Jun 28 '13 at 04:22
  • That was my point. You should NOT set the pixel width manually because you don't know how large to make the text field. The size of the text field will change as the Font changes and different platforms can use different Fonts. Swing was designed to be used with layout managers. The layout manager will set the size and location of the component for you automatically. Read the Swing tutorial on [Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) for more information and working examples. – camickr Jun 28 '13 at 04:33