• Post Reply Bookmark Topic Watch Topic
  • New Topic

Width issue with JTextField and JComboBox in GridBagLayout  RSS feed

 
Ashish Gupta
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your code where you are setting up the constraints for the text field and combo box and adding them to the layout...
 
Ashish Gupta
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the SteppedCombobox example. It allows for a box smaller than the largest item but expands when opened.
 
Ashish Gupta
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul,
Example was really helpful.
Thanks
Ashish
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!