aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Resize components inside a JPane when using Null Layout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Resize components inside a JPane when using Null Layout" Watch "Resize components inside a JPane when using Null Layout" New topic
Author

Resize components inside a JPane when using Null Layout

Dic scott
Greenhorn

Joined: Jun 23, 2011
Posts: 17

Hi All,
I am using Null Layout. Reason, I should be able to move the components anywhere on the screen. I also have features like resizing the component once its added to the frame and deleting them if user prefers to. Basically the component I am talking here is a JLabel with an image icon set on it. I realize that a component listener has to be added to JFrame. So whenever it resizes, the components inside it should resize(Imagine like the one in GridBag Layout). So I need to set the label's bounds using setBounds. But how do I calculate the value for parameters , that is the new x,y position and width n height of the label ??

Please help!


Success is not the key to happiness. Happiness is the key to success. If you love what you are doing, you will be successful
-Herman Cain
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3375
    
    9
Welcome to CodeRanch!

Dic pari wrote:I am using Null Layout.

This is a serious issue. Don't do this. You can perfectly resize your components and hide them and move them around while using layout managers. You will be hard pressed finding anyone willing to help you if you use null layout.
Dic scott
Greenhorn

Joined: Jun 23, 2011
Posts: 17

Hi Stephan,
I initially used GridBagLayout. As you said, I can move components, resize them etc. BUT, the moment I resize the frame, the components, I hand-positioned, loose them and center themselves in the frame which obviously doesn't fulfill the need.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

As for how you should compute the required size and position of one of your components, that isn't a question that we can answer. At least not with the information you provided. Presumably you have some rules about how the components interact with each other and with their container? If you don't, then you should get some. Because those are the rules which will determine how you position components when their container's size changes. Essentially you are designing your own LayoutManager without calling it that.

And if you do have those rules, then you would have to tell us what the rules are if you wanted us to help with figuring out those calculations.
Dic scott
Greenhorn

Joined: Jun 23, 2011
Posts: 17

Hey,

My requirement is like this. Resize any window, say your browser . You see that the BookMarks Menu, home button, previous Button and other components are still there, yet they reduced/increased in size depending on whether you made the window smaller or larger. The point is RELATIVE POSITION of the component from top of the window is same. It just reduced in size. So my question is how do i

1. Find the position of the component from top-left corner of the frame
2. How do I preserve the position even when the frame is resized.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

All Swing components have a "getX()" and a "getY()" method... was that what you meant?

As for "preserve the position", given that you threw in the term "relative position", you're going to have to explain what you mean by that.

Also when I resize the browser and make the window smaller, some of the components shrink and some don't. You're going to have to decide if you want all of your components to shrink or whether some of them should stay the same size.
Dic scott
Greenhorn

Joined: Jun 23, 2011
Posts: 17

Hey,
Thanks for your timely reply. "Relative Position" I mean this. Whether the browser is resized or not, the brown container/ display pane in which my post/reply is visible to you is always occupying the three-fourth the height of the browser window. Breadth wise, it occupies the entire window. I want to do something like this.

and regarding your other question, yes I want to shrink/expand ALL components in the frame.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

Okay, so you've got the (X, Y) position of the container and the (X, Y) position of the component. And you've got the size of the container (before and after resize) and the size of the component (before resize). So to get the size of the component after resize, you just use a simple bit of arithmetic. You multiply it by the resize factor, which is (size of component after resize) / (size of component before resize). Likewise you multiply the offset of the component from the container by that same factor.
Dic scott
Greenhorn

Joined: Jun 23, 2011
Posts: 17

Cool. I will try it out. Thanks a ton for your timely reply. Cheers!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2091
    
    7
A BoxLayout will allow components to grow/shrink as the window is resized. GridBagLayout also allows components to grow/shrink.

The solution will be to use a layout manager. If you can't find one that does what you want, then you will need to write your own.

Post your SSCCE demonstrating the problem and maybe we can suggest the layout manager to use.

You should NOT be using a null layout!
Dic scott
Greenhorn

Joined: Jun 23, 2011
Posts: 17

Paul Clapham wrote:Okay, so you've got the (X, Y) position of the container and the (X, Y) position of the component. And you've got the size of the container (before and after resize) and the size of the component (before resize). So to get the size of the component after resize, you just use a simple bit of arithmetic. You multiply it by the resize factor, which is (size of component after resize) / (size of component before resize). Likewise you multiply the offset of the component from the container by that same factor.


Hey,
Did you mean to say the resize factor = Size of the CONTAINER after resize / size of the CONTAINER before resize ???
Because, I need to find size of the COMPONENT after resize. So the resize factor cannot be (size of component after resize) / (size of component before resize). Correct me if am wrong. Cheers!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Resize components inside a JPane when using Null Layout
 
Similar Threads
Specify exact height of panel or component
Layout Managers
Reg:Component Resize
JButtons inside JTooltip
how to resize JPanel size to JFrame size