Java Swing pack () in the window does not maximize it, how to avoid this?

I have a window, and since I dynamically change it (sometimes I exchange JPanels), I could not find a solution other than calling package () in the window to display a new element. Otherwise, it will only be displayed if I manually resize the window.

The problem is that if the window is maximized, then after the package () it will no longer be, which is not what I could give the client.

Any clues?

+6
source share
4 answers

First of all, I hope you use CardLayout to exchange panels, as this functionality is built into this particular layout manager, and usually you want to call validate / revalidate and repaint in the container to update the display.

See also:

+5
source

If you really need to:

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

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

+4
source

I have the same problem as you and me. I am satisfied with my decision, which I share, perhaps this will help you in 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 created an empty panel on my main frame and button, call it

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

which works great for me

0
source

What is my new way to solve this problem:

 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 use these methods to hide and show jpanels on my jframe.

sample button code

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

same for hiding.

0
source

Source: https://habr.com/ru/post/896132/


All Articles