-1

I want to print a string in a jFormattedTextField in a swing JFrame. The string is different every time. It consist of one to twelve integers ranging form one to eighty plus a whitespace and a comma between each number. The width of the jFormattedTextField doesn't change. I want the string to fill the width of the jFormattedTextField. examples of the string is "1" or "2, 40" or "23, 40, 50, 60".

thanks in advance for any time or effort.

2
  • Do you mean that you want the textfield to shrink/grow according to its content? Or do you want the text within the textfield to spread across the available width of the textfield? Jun 21, 2013 at 13:15
  • I want the text inside the textfield to spread across the available width of the textfield Jun 21, 2013 at 13:29

1 Answer 1

1

Have you tried setting its width programatically?

myFormattedTextField.setSize(new Dimension(newWidth, myFormattedTextField.getSize().getHeight()));

And compute newWidth according to the contents, as in:

newWidth = newString.length()
3
  • I don't want to change the size of myFormattedTextField. I want the sum of every character in the string equals the width of the myFormattedTextField Jun 21, 2013 at 13:19
  • getSize().getHeight returns double the second argument for the constructor of the class dimession expects int Jun 21, 2013 at 13:27
  • 2
    @IoannisPapaioannou you could simply use getHeight() directly, or cast the double to an int or use getSize().height. But anyway, this will not produce what you want. It will more or less size the field to its content. Btw, using setSize() is a very bad and poor idea Jun 21, 2013 at 13:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.