[Xpert] Changing the size of a JFrame dynamically

Discussion in 'Java' started by Pierre Vigneras, Jan 23, 2004.

  1. 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/
     
    Pierre Vigneras, Jan 23, 2004
    #1
    1. Advertising

  2. Pierre Vigneras

    Andrew Hobbs Guest

    "Pierre Vigneras" <> wrote in message
    news:...
    >
    > 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


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




    >
    > What is wrong ?
    >
    >
    > --
    > Pierre Vigneras
    > http://www.labri.fr/~vigneras/
    >
    > Distributed Objects Systems
    > LaBRI
    > http://www.labri.fr/
     
    Andrew Hobbs, Jan 24, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nick
    Replies:
    2
    Views:
    44,743
  2. Patrick

    frames question for xpert

    Patrick, Feb 16, 2005, in forum: HTML
    Replies:
    12
    Views:
    674
    mscir
    Feb 17, 2005
  3. Phils
    Replies:
    0
    Views:
    592
    Phils
    Nov 30, 2006
  4. jakester
    Replies:
    3
    Views:
    1,898
    jakester
    Apr 3, 2007
  5. Replies:
    2
    Views:
    439
    markspace
    Aug 27, 2012
Loading...

Share This Page