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 ??
Success is not the key to happiness. Happiness is the key to success. If you love what you are doing, you will be successful
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.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
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.
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.
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.
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.
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.
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.
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.
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!