Java Swing pack() on a Window un-maximizes it, how to avoid that?
Asked Answered
A

4

7

I've a window and since I dinamically change it's children (sometimes I swap JPanels), I found no other solution than calling pack() on the window to get the new element displayed. Otherwise it will show up only if I resize manually the window.

The problem with this is that the if the window is maximized, after pack() it won't be anymore, which is not what I could give to the client.

Any clues?

Airsickness answered 29/8, 2011 at 15:25 Comment(1)
Resize frame after executing pack(), just a thought! By resize I mean programeticallyUlrika
T
5

First of all, I hope that you're using CardLayout for panel swapping, since this functionality is built into that particular layout manager. And typically, you'll want to invoke validate/revalidate and repaint on the container to refresh the display.

See also:

Treadway answered 29/8, 2011 at 15:28 Comment(4)
No I was using borderLayout, I'm not swing specialist I've been assigned this project and God knows how much I hate it. Thanks :DAirsickness
Is it possible to add panels dynamically to card layout?Airsickness
@gotch4, I would like to say yes, but I have not tested that yet.Treadway
You add panels to CardLayout and then show() them with their name. Why dynamically adding them? You dynamically show() them.Sink
S
4

If you really have to:

int state = frame.getExtendedState();
frame.pack();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

... Otherwise @mre's solution is (much) better! :)

Semeiology answered 29/8, 2011 at 15:29 Comment(0)
B
0

I got the same problem as you and for me I m satisfied with my solution I share it may be it help you on your context

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Housseyn
 */
public class JPanelTools {

public static final void ShowPanel(JPanel target, JPanel object) {
    target.removeAll();

    Dimension size = object.getSize();
    size.setSize(size.width, target.getHeight());
    target.setSize(object.getSize());

    GridBagLayout gridBagLayout = new GridBagLayout();

    target.setLayout(gridBagLayout);

    GridBagConstraints gbc = new GridBagConstraints(target.getX(),
            target.getY(),
            target.getWidth(),
            target.getHeight(),
            0, 0,
            GridBagConstraints.ABOVE_BASELINE,
            0,
            new Insets(5, 5, 5, 5),
            0, 0);

    target.add(object, gbc);
    target.invalidate();
    target.revalidate();
    target.validate();
    target.repaint();
    target.show();
    object.validate();
    object.repaint();
    object.show();

    Container Frame = target.getParent();
    Container Current = target.getParent();
    while ((Current != null)) {
        System.out.println("current =" + Current.getClass().getName());
        Frame = Current;
        Current = Current.getParent();
    }
    System.out.println("frame " + Frame.getClass().getName());
    if (Frame != null) {
        System.out.println("pack");
        JFrame MyFrame = (JFrame) Frame;
        int extendedState = MyFrame.getExtendedState();
        if (extendedState != JFrame.MAXIMIZED_BOTH) {

            MyFrame.pack();
            MyFrame.setExtendedState(extendedState);
        }
    }
}

}

I designed an empty panel on my main Frame and in a button I call this

  MyDesignedPanel myPanel = new MyDesignedPanel();
  JPanelTools.ShowPanel(JemptyPanel, myPanel);

that works perfectly for me

Bayly answered 12/11, 2016 at 14:40 Comment(1)
I use the same background color for my empty panel and my inserted panelBayly
B
0

That my new way of handling this issue :

public static void showForms(JFrame frame,JPanel[] jPanels){
    for (JPanel jPanel : jPanels) {
        showForms(frame, jPanel,false);
    }
    int extendedState = frame.getExtendedState();
    if (extendedState==JFrame.MAXIMIZED_BOTH) {
        return;
    }
    frame.pack();
}

public static void showForms(JFrame frame, JPanel jPanel, boolean doPack) {
    jPanel.setVisible(true);
    if (doPack) {
         int extendedState = frame.getExtendedState();
        if (extendedState==JFrame.MAXIMIZED_BOTH) {
        return;
    }
        frame.pack();
    }
}

public static void hideForms(JFrame frame, JPanel[] jPanel) {
    for (JPanel panel : jPanel) {
        hideForms(frame, panel, false);
    }
     int extendedState = frame.getExtendedState();
    if (extendedState==JFrame.MAXIMIZED_BOTH) {
        return;
    }
    frame.pack();
}

public static void hideForms(JFrame frame, JPanel jPanel, boolean doPack) {
    jPanel.setVisible(false);
    if (doPack) {
         int extendedState = frame.getExtendedState();
        if (extendedState==JFrame.MAXIMIZED_BOTH) {
        return;
    }
        frame.pack();
    }
}

I m using this methods to hide and show jpanels on my jframe.

sample on a button code

    JFrameTools.showForms(this,searchPanel,false);
    JFrameTools.showForms(this,insertingPanel,true);
    JFrameTools.showForms(this,new jPanel[]{insertingPanel,searchPanel,printingPanel});

the same for the hiding.

Bayly answered 28/2, 2017 at 23:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.