Jump to content
  • Advertisement
Sign in to follow this  
boogyman19946

Manual Component Resizing

This topic is 2101 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In Java, does anyone know of some method by which I can let a layout manager resize components, but at the same time keep them in an aspect ratio? Currently the only way I found is to manually resize them, and I can't find a way to do this simply. My general way of constructing UIs is to set the properties of all the components before I add them to a container, and this gives me a lot of grief when it comes to resizing the components because I don't know how much space is available until I add the component to a container and the layout manager sizes it. I thought ComponentListeners would work, but I don't get a componentResized event unless I manually drag the border of the JFrame.

Share this post


Link to post
Share on other sites
Advertisement

I guess you could setPrefferedSize() then pack() every container so all sub components get the size you set with setPreferredSize() (maybe just packing the JFrame is enough).

 

That covers the default layout sizing (ie, when the components gets created and shown), after that you can handle manual resizing with the listener you already have.

Share this post


Link to post
Share on other sites

Wouldn't pack() resize my window though? In that case I don't want to pack the JFrame every time I switch between the menus just to resize the components inside it. 

I forgot to mention in my original post, and that's entirely my fault, that I still want the objects to scale with the window, but I want some of the components inside it to maintain a consistent width/height ratio. You see, I'm making a card game and I'm extending JButtons to represent the cards. If the button scales one dimension instead of both at once, then the cards end up stretched. What I generally do in this situation is probe the parent container to get the available space, and calculate the dimensions of the card to maximize the card's size while making sure it still looks like a card. I don't have a static value to pass to setPreferredSize because I generally calculate that based on the available space I have.

Share this post


Link to post
Share on other sites

Woot!

I don't know what it is about this forum, but many times when I posted the answer just sort of dawned on me shortly after.

 

I have managed to solve this ordeal by writing a custom layout manager that works like a FlowLayout, but, again, maintains a set aspect ratio. This provided me with a really nice solution and relieves me from having to include code for the layout where it shouldn't be. 

Share this post


Link to post
Share on other sites

It may not work for you this time, because of the learning curve, but I highly recommend MigLayout:

 

http://www.miglayout.com/

 

I know that you can layout out components and set the size to 40%, so it is always 40% of the current window size, and

you don't have to do anything.  While it can take a little while to figure out all the nuances, it is time well spent.

Share this post


Link to post
Share on other sites

Hey that looks like some good stuff! But you're right, it's a little complex to stick in my current project. I'll make sure to check it out for the next one though!

Share this post


Link to post
Share on other sites

On a side note, before Oracle bought Java, MigLayout was going to be included as a new LayoutManager in Java 7.0.  When Oracle took over it didn't happen, but Sun thought it was good enough to make the cut.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!