zebediah49
zebediah49

Reputation: 7611

Create a custom JComponent with fixed aspect ratio

As part of a project, I have a custom JComponent that implements a piece of a user interface. It's a rather strange control involving moving around a set of x-y points, but that shouldn't really matter, because that has been implemented and works properly.

This component has a usable area which is square -- any excess area is letterboxed away.

How can I tell a layout manager that I want this component to be square, of whatever size the layout manager decides is appropriate? It doesn't break functionality of extra area is allocated, but it does waste space that could better be used by other components.

Upvotes: 0

Views: 146

Answers (2)

ave4224
ave4224

Reputation: 19

This is a little crude but may be required. You can readjust the size according to what the user changed it to. For example, if the user adjusts the size to 500x600 set it to 500x500.

Upvotes: 1

Durandal
Durandal

Reputation: 20069

You have only three knobs to tell the LayoutManager what your component wants:

getPreferredSize(); getMinimumSize(); getMaximumSize();

Note that not all LayoutManagers honor (especially the latter two) those hints. Apart from that the client using the component in a Layout has much more options with some LayoutManagers (e.g. GridBagLayout) to hint if and how the control is to be resized.

So in short, return a proper preferred size and leave the rest to the layout using the component.

Upvotes: 2

Related Questions