This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Resize components inside a JPane when using Null Layout

 
Dic scott
Greenhorn
Posts: 17
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Stephan van Hulst
Bartender
Pie
Posts: 5553
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool. I will try it out. Thanks a ton for your timely reply. Cheers!
 
Rob Camick
Ranch Hand
Posts: 2559
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic