How to ensure a square JComponent?

J

Jacob

I have a JComponent inside some container
and want it to be square (equal width
and height) always but still resizeable
when its container is resized.

I can think of a few solutions, but none of
them are very elegant.

Thanks!
 
V

VisionSet

Jacob said:
I have a JComponent inside some container
and want it to be square (equal width
and height) always but still resizeable
when its container is resized.

I can think of a few solutions, but none of
them are very elegant.

Thanks!

public Dimension getPreferredSize() {

Dimension dim = super.getPreferredSize();

dim.width = Math.min(dim.width, dim.height);
dim.height = Math.min(dim.width, dim.height);

return dim;
}

you may need to substitute getMaximumSize() for getPreferredSize() or use
both depending on what manager you use to layout the container.
 
J

Jacob

VisionSet said:
public Dimension getPreferredSize() {

Dimension dim = super.getPreferredSize();

dim.width = Math.min(dim.width, dim.height);
dim.height = Math.min(dim.width, dim.height);

return dim;
}

I guess you mean "getParent()" instead of "super"?

And then it breaks by a stack overflow exception as the
parent preferred size is determined by the children sizes
combined with the layout manager.

Or did I miss something?
 
J

Jacob

Roedy said:
you could write a Square Layout that takes one component and makes it
square.

It is easy to make something square if you know its
exact size. But if it should be resizeable then I
seem to get into a chicken-egg problem inside the
getPreferedSize() method.
 
R

Roedy Green

I can think of a few solutions, but none of
them are very elegant.

the essential logic goes like this:

max = min(height,width);

min = max( height, width);

pref = Math.sqrt ( height * width );

or max( height, width );
 
A

Andrey Kuznetsov

I guess you mean "getParent()" instead of "super"?wrong guess. he means super.

Andrey
 
V

VisionSet

Jacob said:
I guess you mean "getParent()" instead of "super"?

And then it breaks by a stack overflow exception as the
parent preferred size is determined by the children sizes
combined with the layout manager.

Or did I miss something?

You missed something, I meant super.

super calls the superclasses method of the same name.
So if you extend JComponent, the super class has some notion of what size it
should be, it is the super class that by default manages the size. So the
simplest solution, not knowing any specifics about your situation, is to use
that default. So I get the superclasses idea of a preferredSize dimension
and modify it by constraining the size to a square based on its minimum
dimension. Rough and ready, but only you have the real specification.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top