• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pack a JFrame from a different class?  RSS feed

 
Toni Howlett
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class, Calculator, that has a JFrame in it as well as a few other panels and such. One of those panels holds the binary thats required for my project. The details for that panel live in BinaryPanel. In BinaryPanel, there is a toggle method where it hide/shows that panel. When toggle is called, the panel hides, but a gap is left at the bottom of the calculator window. I think calling pack again on the JFrame will solve this... but I'm not sure how to do this from another class.

So whats a way to do this... or a work around?

Any help would be great
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling pack would also resize/relocate your frame which is not a good user experience.
The proper way would be to call a revalidate() and repaint() on the container from which you are removing/hiding the panel. If you are using proper layouts, this will result in the existing children to be laid out properly, accommodating the gap.
 
Toni Howlett
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently calling repaint() and its shifting everything under it up to fill in that gap, but then the resulting gap is at the bottom of the frame... hence my thoughts on repacking the frame.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you calling revalidate() as well? Are you using layouts? (as opposed to null layout and explicitly setting the bounds)
Maneesh Godbole wrote:The proper way would be to call a revalidate() and repaint()
 
Toni Howlett
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried adding revalidate, it didn't do anything.

I have layouts set and dimensions set on all sized panels but NOT the frame.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Toni Howlett wrote:
I have layouts set and dimensions set on all sized panels but NOT the frame.

Explicitly setting the dimensions could be the root cause. Can you without the dimensions? If it doesn't work please post your SSCCE (<= link, click to know what SSCCE really means) code.
 
Toni Howlett
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only have dimensions where they are necessary. For example, if I didn't have them on my keypad panel, the buttons wouldn't line up correctly. They are sized to their absolute minimum.



Uaa ill attempt an SSCCE... it spans quite a few files and pretty much no easy way to copy and paste @_@ Give me a min
 
Toni Howlett
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cleared
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You are doing it wrong.
Maneesh Godbole wrote:The proper way would be to call a revalidate() and repaint() on the container from which you are removing/hiding the panel.

1) Revalidate and repaint should be called in that order.
2) They should be invoked on the parent container
 
Toni Howlett
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swapped the order, still didn't work... but thats then because of your #2.

I can't exactcly call it from the parent container as that container is in a different class from my toggle method and i'm not sure on how else to do it.



Any suggustions on how to get around this or accomplish the same thing?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your BinaryPanel extends JPanel. JPanel has a method (which you have inherited) to get the parent (hint) This method returns a container. You know that binary panel is a JPanel. So you can cast it safely and then invoke the methods on that cast.
 
Toni Howlett
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so its getParent()

I've tried a handful of things... but I keep getting cannot find symbol....


would it be this.getParent()?



I can see what you're getting at... but I'm not sure how to do it....
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Toni Howlett wrote:
I've tried a handful of things... but I keep getting cannot find symbol....
would it be this.getParent()?

You need to copy paste your exact error message to help us figure it out. Your console will have the stack trace. Just copy paste it here (dont for to UseCodeTags though )
 
Toni Howlett
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you just give me correct code or what you think should work for my case?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Toni Howlett wrote:Could you just give me correct code or what you think should work for my case?

If you can't be bothered to copy pase, then I don't think I can be bothered to do anything more. Besides we do not hand out ready made code here at the ranch.
This site is NotACodeMill(<-click). You are expected to ShowSomeEffort(<-click) and DoYourOwnHomework(<-click).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!