Width issue with JTextField and JComboBox in GridBagLayout
Ashish Gupta
Ranch Hand
Joined: Apr 27, 2003
Posts: 61
posted
0
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
Post your code where you are setting up the constraints for the text field and combo box and adding them to the layout...
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Ashish Gupta
Ranch Hand
Joined: Apr 27, 2003
Posts: 61
posted
0
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
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.
Ashish Gupta
Ranch Hand
Joined: Apr 27, 2003
Posts: 61
posted
0
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 ]
Paul Stevens
Ranch Hand
Joined: May 17, 2001
Posts: 2823
posted
0
You can use the SteppedCombobox example. It allows for a box smaller than the largest item but expands when opened.
Ashish Gupta
Ranch Hand
Joined: Apr 27, 2003
Posts: 61
posted
0
Thanks Paul, Example was really helpful. Thanks Ashish
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Width issue with JTextField and JComboBox in GridBagLayout