• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to stop JTextField collapsing when using GridBagLayout?

 
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a problem with using JTextField in a GridBagLayout that I was wondering if anyone could help me with?

I have posted a short example below that demonstrates my problem. My problem is that when you resize the window to make it smaller the JTextField collapses from its set width to a width of zero.

What I would like to happen is that as you resize the window to make it smaller the JTextField would just shrink down in width to fit the window. Then when you resize the window to make it bigger the JTextField would expand in width until reaching the width I have specified for it.

 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, and I want the label and textfield to always remain in the center of the window. So as you expand the window the label and textfield should remain in the center. The textfield should only expand to the width I have specified, no wider.

What I am trying to create is a simple form. With labels to the left and fields to the right.

I am creating this form on a separate panel. Then I am adding this form panel into the center of my main window. So as my main window resize I want the form to stay in the middle of the window and I don't want the text fields to expand beyond the width that I set for them. If that makes sense?

Below is an image of what I am trying to achieve. I can get this layout with my form centered in the middle. But when I resize the main window to make it smaller in width the text fields resize down to zero.
layout.JPG
[Thumbnail for layout.JPG]
Layout
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any thoughts ?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to set the JTextField's minimum size to something other than zero. I had this problem a while ago and I think what I did was to set it equal to the preferred size. (It was several years ago and I don't have the code at hand.) Anyway if you experiment with setMinimumSize you should be able to come up with something which suits your needs.
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have created the text field with 30 columns, so it calculates the preferred size from that. When it finds that it doesn't have enough space to hold that much size, it takes the minimumSize. So, setting that would solve the problem.

However, for form-type screens, SpringLayout is the right layout manager:
http://download.oracle.com/javase/tutorial/uiswing/layout/spring.html
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies guys. I've tried setting the minimum size. But this does not give the effect that I am looking for. What I have done is create a simple method that sets the minimum width to the preferred width minus one:

When you set the minimum size, yes it jumps down to the minimum size when there is not enough space. But when you further reduce the space the text field does not resize to fit the space. It is simply truncated. The first image display the layout and behaviour that I currently have. The second image below displays the effect and layout that I want to achieve.

Surely this is quite a common set up? I've searched quite a lot online and can't believe there is nothing out there.

I took a look at spring layout. But it looks way to complicated and with no guarantee that it is going to get my required layout and behaviour anymore than GridBagLayout will.




layout2.JPG
[Thumbnail for layout2.JPG]
layout2
actualLayout.JPG
[Thumbnail for actualLayout.JPG]
actualLayout
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Keane wrote:I took a look at spring layout. But it looks way to complicated and with no guarantee that it is going to get my required layout and behaviour anymore than GridBagLayout will.



Nevertheless... if you don't like the way GridBagLayout deals with your text field, you're going to have to use some other layout manager. Whether you download one (a couple have already been mentioned in this thread) or write your own is up to you.
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Sean Keane wrote:I took a look at spring layout. But it looks way to complicated and with no guarantee that it is going to get my required layout and behaviour anymore than GridBagLayout will.



Nevertheless... if you don't like the way GridBagLayout deals with your text field, you're going to have to use some other layout manager. Whether you download one (a couple have already been mentioned in this thread) or write your own is up to you.



Thanks for the advice. I am working on the OMCJD. Time is a constraint. So I don't want to end up going down a rabbit hole.

I've very little GUI experience. But I've read a fair bit on the Oracle site and played around with various approaches in code.

So I am wondering is it definitely not possible to get the layout and behaviour I want using GridBagLayout or any of the other commonly used layout managers? I've tried using combinations of layout managers and various panels, but to no avail.

The only other solution suggested was to use spring layout. Has anyone experience with this? Will this definitely give me the layout and behaviour that I am looking for?

The examples I looked at online for spring layout certainly don't look like they will give me the behaviour I want. This page here has an example of a form using the spring layout. When you resize the window the text fields expand vertically and horizontally. I don't want this behaviour. I never want the text field to get wider than my specified width.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. I re-read your post, and this should work for you:

This always shows the complete textfield - doesn't hide the border even when label is shrinked. This is because more weightage is given to the text field. As you can see, you have to play around with weights (which is not easy and this is mentioned in the tutorial documentation itself)...
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ranganathan Kaliyur Mannar wrote:This always shows the complete textfield - doesn't hide the border even when label is shrinked. This is because more weightage is given to the text field.



Thanks for looking at this Ranganathan. Could you post your complete code example please? Because I made the modifications you suggested and it made no difference.

When I make your modifications two problems that I previously mentioned still exist:

1) The label disappears as you make the window smaller.
2) The right hand border of the text field disappears as you make the window smaller - i.e. it is being truncated, not resized.

If you look at the images on my post from 10 September 2011 14:18:50 you will see these two exact problems. Both of these problems still persist when I make the modifications that you suggested.

For reference, here is my complete example below that includes your modifications:
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I copied your code and was able to reproduce the issue which initially puzzled me. After some trial-error, I think I understand the issue. I am running your example in a Windows XP machine and the initial preferred size for the textfield shows as 334 for width. So, you are setting the minimum width to 333. Now, forget the components for a moment. Suppose you create a JFrame and reduce its size as much as possible. It doesn't reduce beyond a point - i.e it seems to have a minimum width just enough to show these: 'min,max, close' buttons + 'top left icon' + some space to hold the title bar by mouse.

Now, if the text field's minimum size is larger than this minimum size of frame, you get the issue that you are currently facing (of label's right border not visible anymore) - and I think its a logical behaviour. This works as per our code - based on the minimum size we set. Now that the weights solve the issue upto an extent (gives more importance to the field than the label), you have the following ways to achieve what you are looking for:
1) Find out what would be a sensible minimum size for the field and set it...so that even when the frame is reduced to the max limit, the field is fully visible.
2) If you need the field to dynamically recude the size, I think the only option would be to write a ComponentListener for the JFrame. In the componentResized method, you should dynamically set the field's minimum size. I understand this would not be easy to write though...the componentResized only tells us that the comp. is resized - it does not tell us whether it is recuded or increased...
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ranganathan Kaliyur Mannar wrote:2) If you need the field to dynamically recude the size, I think the only option would be to write a ComponentListener for the JFrame. In the componentResized method, you should dynamically set the field's minimum size.



There's another, IMO more correct approach: write your own layout manager that provides the desired behavior. Also not simple, but with work it could result in something reusable.
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers for the advice guys. It seems this is certainly not a trivial task to create the behaviour I would like. So I think I'll have to make some compromises for now as I'm not going down the road of attempting to write my own layout manager. Too much time and no guarantee that I will get my desired behaviour in my desired time frame.

It's a bit disappointing that Java doesn't provide any easy to use solution out of the box for such simple requirements. It's not like I am trying to do anything fancy. All I want is:

1) My components stay centered in the panel.
2) My components are displayed at the size I set them.
3) My text fields resize when there is not enough space to display their preferred size (as opposed to being truncated).
4) My labels only being to resize when the longest label is wider than the longest text field.

Creating forms is quite a common thing, no? I am surprised that it has not come up a lot in the forum. I would have thought that there'd be a few threads already with possible solutions\patterns to follow.
 
reply
    Bookmark Topic Watch Topic
  • New Topic