[Xpert] Changing the size of a JFrame dynamically

P

Pierre Vigneras

Hi,

I've the following problem:

two frames A and B are displayed, A is resizable whereas B is not. A
contains a JPanel Pa (A.setContentPane(Pa)), and B a JPanel Pb
(B.setContentpane(Pb)). Each JPanel contains no components, they are
only used for painting.

When the user resize A, I want to resize B as well to exactly the same
size. So, I use the following code:

Pa.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
public void ancestorResized(HierarchyEvent e) {
resizeOtherFrame();
}
});


where resizeOtherFrame() is:

void resizeOtherFrame() {
B.setSize(A.getSize());

Pb.setSize(Pa.getSize());
}


Hence, when the user resize frame A with the mouse, the B frame is
resized too. But the problem is that when Pb draws, it only draws on a
portion of the region available. As if something between B and Pb has
not been resized (maybe the RootPane?). Sun tutorial says that some
LayoutManager does not use the size attribute (such as
BorderLayout). But others do (BoxedLayout, SpringLayout). The problem
is that my JPanel Pa and Pb does not have to layout something, hence
they have a LayoutManager set to null! It is the JFrame wich must
layout my JPanels (its content pane!). Maybe I must specify a
LayoutManager such as SpringLayout (which uses the size attribute) to
the JFrame B, but how?

What is wrong ?


--
Pierre Vigneras
http://www.labri.fr/~vigneras/

Distributed Objects Systems
LaBRI
http://www.labri.fr/
 
A

Andrew Hobbs

Pierre Vigneras said:
Hi,

I've the following problem:

two frames A and B are displayed, A is resizable whereas B is not. A
contains a JPanel Pa (A.setContentPane(Pa)), and B a JPanel Pb
(B.setContentpane(Pb)). Each JPanel contains no components, they are
only used for painting.

When the user resize A, I want to resize B as well to exactly the same
size. So, I use the following code:

Pa.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
public void ancestorResized(HierarchyEvent e) {
resizeOtherFrame();
}
});


where resizeOtherFrame() is:

void resizeOtherFrame() {
B.setSize(A.getSize());

Pb.setSize(Pa.getSize());
}


Hence, when the user resize frame A with the mouse, the B frame is
resized too. But the problem is that when Pb draws, it only draws on a
portion of the region available. As if something between B and Pb has
not been resized (maybe the RootPane?). Sun tutorial says that some
LayoutManager does not use the size attribute (such as
BorderLayout). But others do (BoxedLayout, SpringLayout). The problem
is that my JPanel Pa and Pb does not have to layout something, hence
they have a LayoutManager set to null! It is the JFrame wich must
layout my JPanels (its content pane!). Maybe I must specify a
LayoutManager such as SpringLayout (which uses the size attribute) to
the JFrame B, but how?

LayoutManagers are only involved in laying out child Components. If you are
only using the panels for painting then layout managers will not help you.

How have you written the painting methods. If in absolute terms (eg
g.drawLine(100, 100, 200, 200) you need to rewrite them in terms related to
the size of the panel. That is you need to specify each position in a
drawing method as a percentage of the panel height or width.

eg g.drawLine(0.2 * getWidth(), 0.2 * getHeight(), 0.4 * getWidth(), 0.4 *
getHeight());

Then as the panel changes size, the drawing expands or contracts to fit the
panel.

Cheers

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top