Jump to content

  • Log In with Google      Sign In   
  • Create Account


Manual Component Resizing


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 boogyman19946   Members   -  Reputation: 1027

Like
0Likes
Like

Posted 20 January 2014 - 09:48 PM

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.


"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

Sponsor:

#2 TheChubu   Crossbones+   -  Reputation: 3698

Like
0Likes
Like

Posted 20 January 2014 - 11:16 PM

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


#3 boogyman19946   Members   -  Reputation: 1027

Like
0Likes
Like

Posted 20 January 2014 - 11:42 PM

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.


"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#4 boogyman19946   Members   -  Reputation: 1027

Like
0Likes
Like

Posted 21 January 2014 - 09:53 AM

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. 


"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#5 Glass_Knife   Moderators   -  Reputation: 3355

Like
1Likes
Like

Posted 21 January 2014 - 10:27 AM

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"
Indie Game Programming

#6 boogyman19946   Members   -  Reputation: 1027

Like
0Likes
Like

Posted 21 January 2014 - 10:45 AM

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!


"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#7 Glass_Knife   Moderators   -  Reputation: 3355

Like
0Likes
Like

Posted 21 January 2014 - 11:30 AM

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"
Indie Game Programming




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS