Forums Register Login

Using Gridbag to set sizes of textfields

+Pie Number of slices to send: Send
When I run the code below as a card on a non-descript cardLayout, I get something similar to what you see in the attachment.

Instead, I want all of the boxes to be the same size: large enough to hold the number "255", but no larger.

I was tempted to use the setMaximumSize and setMinimumSize, but I heard recently that those operations should be avoided, and that you could get the same functionality using the layoutManager itself. How would I change my code below to set the size of my textboxes without using those methods?

Thanks a ton!



cardLayout.png
What I see when I run similar code.
[cardLayout.png]
2
+Pie Number of slices to send: Send
The GridBagLayout will respect the preferred size of a component. For a JTextField you need to suggest what the preferred size should be by using:




The text field will now be sized to hold "WWW", which will be a little larger than "999", but that is the best you can do with the default JTextField.

If you want a more precise sizing then you would need to overrid the getPreferredSize() method of JTextField and provide your own calculation.
+Pie Number of slices to send: Send
If it's good enough for you, it's good enough for me. Thanks a ton! Have a good day!
+Pie Number of slices to send: Send
 

Rob Camick wrote:If you want a more precise sizing then you would need to overrid the getPreferredSize() method of JTextField and provide your own calculation.


... or set a monospaced Font.
+Pie Number of slices to send: Send
 

Darryl Burke wrote:... or set a monospaced Font.



Good point!
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1136 times.
Similar Threads
Converting from Swing to Applet
convert swing to applet
To IDE or not to IDE
JTextArea, Focus
palindrome not very proficient...
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:22:05.