开发者

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

开发者 https://www.devze.com 2023-03-31 07:00 出处:网络
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

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 clie开发者_开发问答nt.

Any clues?


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:

  • How to Use CardLayout


If you really have to:

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

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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消