Forums Register Login

Width issue with JTextField and JComboBox in GridBagLayout

+Pie Number of slices to send: Send
Hi,
I have a JTextField and JComboBox placed side by side (in a row) in a JPanel when I am using GridBagLayout. Doing so I am facing 2 issues
1. Hight of ComboBox is more than that of TextField; and;
2. Width of ComboBox is too big.
Is there a way to fix the above two issues? I have tried different ways but somehow could not achieve.
Thanks
Ashish
+Pie Number of slices to send: Send
Post your code where you are setting up the constraints for the text field and combo box and adding them to the layout...
+Pie Number of slices to send: Send
Thanks Nathan for help.
Here is the code where I create a panel and add fields. I would like to have JButton also of same height and smaller width. Also I noticed that width of JComboBox depends on the biggest entry in it, which is also not desirable.
Any help, is appreciated.
Thanks
Ashish
+Pie Number of slices to send: Send
How do you set the size of the entryPanel after constructing it? Do you add it to another component? If so, what is that component's layout? Do you pack() the frame that displays these panels, or do you do a setSize() with a specific size? Trying to size the entryPanel below the size needed for the preferred size of it's contents will cause weird things to happen to the layout... labels start appearing over textfields, some components resize to be very small, etc.

For some reason, the default preferred height of a JComboBox is 5 pixels taller than the preferred height of a JTextField ( at least in Metal Look & Fell in the 1.4.2 JDK...). If you create a JTextField instance using the constructor that specifies the number of characters to display then you can just do this to get the combo box to match -


This also has the side effect that the combo box will no longer widen to fit the longest string, so your other problem is also fixed. You can set a different preferred width on the JButton also.
+Pie Number of slices to send: Send
Hello Nathan,
Thanks for your reply and suggestions.
'entryPanel' is added to another panel 'mainPanel' having layout 'BorderLayout'. This 'mainPanel' forms one od the tabs of the tabbed pane.
I am using 'setSize()' for the Frame. I am not using 'pack()'. Will you suggest 'pack()' over 'setSize()'?
I will try to use 'setPreferredSize()' method as you have suggested. For some reason when I have used it in past, I did not noticed it's effect. Maybe I was not using it correctly.
Regards.
Ashish
[ December 02, 2003: Message edited by: Ashish Gupta ]
+Pie Number of slices to send: Send
You can use the SteppedCombobox example. It allows for a box smaller than the largest item but expands when opened.
+Pie Number of slices to send: Send
Thanks Paul,
Example was really helpful.
Thanks
Ashish
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 5322 times.
Similar Threads
Focus Between JComboBox and JTextField
JComboBox
Setting the size of JComboBox
how to search in a JComboBox
Regarding JTextfield and Jlabel
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 10:08:06.