File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Width issue with JTextField and JComboBox in GridBagLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Width issue with JTextField and JComboBox in GridBagLayout" Watch "Width issue with JTextField and JComboBox in GridBagLayout" New topic
Author

Width issue with JTextField and JComboBox in GridBagLayout

Ashish Gupta
Ranch Hand

Joined: Apr 27, 2003
Posts: 61
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
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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
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
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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
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
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
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
 
Similar Threads
Setting the size of JComboBox
Focus Between JComboBox and JTextField
Regarding JTextfield and Jlabel
JComboBox
how to search in a JComboBox