• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Problem with JTextFields and GUI size

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I made a program that allows the user to select a shape.
Given the selected shape, properties appear (e.g. radius for a circle, length and width for a rectangle, etc.).
The program then displays calculations such as area, volume, perimeter, etc.

My problem is that I used four JLabels/corresponding JTextFields, and I simply hide some of them, depending upon the selected shape.
I change the label display when a new shape is selected, and it works, but it also looks stupid, because there is extra space on the bottom for those shapes that require less fields.
The attached picture demonstrates how silly circle looks with all of that empty space.

Is there a way for me to either keep the JTextField dimensions intact regardless of whether they are visible? (It is the collapsing of them that causes the problem).
Even better, is there a way for me to actually control the amount of empty space on the bottom?
I need to hand this in as homework tomorrow and I want it to look okay.










Thank you,
Rob
java.jpg
[Thumbnail for java.jpg]
Example
 
Marshal
Posts: 75878
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have to provide more details before we can help. What layout are you using? Have you set any sizes on the text fields? How are you setting the size of the whole GUI?
 
Rancher
Posts: 3267
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is there a way for me to either keep the JTextField dimensions intact regardless of whether they are visible?



Use a different layout manager.

For example you could use a BorderLayout on the main panel.

Then you create a panel for the text Fields and add this to the "CENTER" of the main panel.

Then you create a second panel to hold the other components and add this panel to the "PAGE_END". (Note this panel could also have child panels if you wish)

Now the panel at the bottom will only take up the space it requires and the top panel will get all the extra space.

My problem is that I used four JLabels/corresponding JTextFields, and I simply hide some of them, depending upon the selected shape.  



Another approach is to use a "CardLayout". This allows you to swap panels when you select the shape. Check out the section from the Swing tutorial on How to Use CardLayout for a working example that swaps panele based on a selection from a combo box. This is a better design as it allows you to create custom panels each with custom logic for the data required for that panel.
 
Saloon Keeper
Posts: 4994
186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But that still leaves the problem of the textfield area having different sizes, according to the shape choosen. An idea might be to put the drawing area to the right of the textfields instead of underneath, making it large enough. Then the textfield part does not matter so much, I reckon.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the other hand, if you do eliminate the extra space at the bottom, whenever you select a different shape the application window would keep changing size, which can be very annoying to the user. Possibly a better approach is to use a BorderLayout, placing the two bottom buttons in SOUTH, the shape selection field in NORTH, and the other fields and drawing in CENTER.

The CENTER could two panels -  for the input questions (radius, width/height, etc.) placed vertically on the left, and a panel with the drawing of the object on the right.

As you can see, there are many possibilities.
 
Michael Von Thorn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made it using NetBeans. I am confused about how to do it.
 
Campbell Ritchie
Marshal
Posts: 75878
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Von Thorn wrote:I made it using NetBeans. . . .

Did you use the Matisse tool? If so, it will have group layout.
 
Rob Camick
Rancher
Posts: 3267
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I made it using NetBeans. I am confused about how to do it.  



Which is why you shouldn't use an IDE to generate the form. You are spending time learning the tool and not learning Java/Swing. If you move to another IDE the code will not be portable.

Instead write the layout code manually. I gave you a link to the Swing tutorial on "How to Use a CardLayout" which has a working example to get your started. Download the example and customize the code for your custom panels.
 
reply
    Bookmark Topic Watch Topic
  • New Topic