Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with size

 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I have an app that has fields that are setVisible(false) when my application runs. If the user clicks a certain button, these fields are supposed to appear. My problem is when they appear, they are tiny. I think it is becuase when the app ran, these fields were not visible, so it didn't pack them.
I need to know how to be able to recognize these fields, even though they are not visible so my frame is large enough for them when they do appear.
I hope this makes sense!
Thanks for any help!
 
Wahid Sadik
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Try to use setPreferredSize() method
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply! I have the set the size of my app. That works. Now the problem I have, is whenever the components are set to "setVisible(false)", Everything shifts so it's centered in the middle. I want all of my other components to stay where they are at!! Is this possibly a GridBagLayout manager issue? If so, what do I need to do to make my components stay where they are at when I set other components' visiblity to false?
Any help is greatly appreciated!!
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jennifer,
The problem is indeed GridBagLayout and there is really nothing you can do about this behavior.
You have two possible solutions:
1. Show the components but use setEnabled() instead of setVisible. This will at least show the user that those controls are not useable in the current "mode".
2. Use a dummy component, such as an empty frame, to swap with the actual component. Some problems with this approach would be: how to get the size for the actual componet ...
My personal choice would be number one because I hate GUIs that have components that appear and disappear.
Regards,
Manfred.
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manfred. I will use setEnabled(false). It works beautifully. However, I was wondering if there was a way to make a whole class not enabled. I have an address class that I am using in my main application and I would like to be able to do a setEnabled on the Address object and have it disable all the fields. Is there any way to do this, or do I have to do the old fashioned way, disabling every field in the Address class one by one?
Thanks a bunch!
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jennifer,
Unfortunately, I think you will need to call the method for each component. I can't seem to get anything else to work myself ...
Regards,
Manfred.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if the address class extends JPanel (or another container...) you could implement a setEnabled( boolean ) method that does this...



It will enable or disable all components held within your address component...

-Nate
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys. You're great!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic