Manual Component Resizing

Started by
5 comments, last by Glass_Knife 10 years, 2 months ago

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.

Yo dawg, don't even trip.

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.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

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.

Yo dawg, don't even trip.

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.

Yo dawg, don't even trip.

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.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

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!

Yo dawg, don't even trip.

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.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

This topic is closed to new replies.

Advertisement