Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Problem with JTextFields and GUI size

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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: 65755
250
  • Mark post as helpful
  • send pies
  • 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: 3072
24
  • Mark post as helpful
  • send pies
  • 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.
 
Bartender
Posts: 3508
150
  • Mark post as helpful
  • send pies
  • 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: 708
10
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I made it using NetBeans. I am confused about how to do it.
 
Campbell Ritchie
Marshal
Posts: 65755
250
  • Mark post as helpful
  • send pies
  • 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: 3072
24
  • Mark post as helpful
  • send pies
  • 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.
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!