How to resize component in JFrame

Suppose I have a JPanel in a JFrame. When I call a method that changes the preferred size of this JPanel, it does not change.

The code looks something like this:

public class SomePanel extends JPanel{ public SomePanel(){ setPreferredSize( new Dimension( 390, 40 ) ); setBackground( Color.BLACK ); } public void expand(){ setPreferredSize( new Dimension( 390, 200 ) ); } public static void main( String args[] ){ JFrame frame = new JFrame(); frame.setSize( 450, 500 ); frame.setLayout( new FlowLayout() ); SomePanel somePanel = new SomePanel(); frame.add( somePanel ); frame.setVisible( true ); somePanel.expand(); } } 

Is there something I need to do first? I tried to check the size of the JPanel when calling the expand () function. The height of the JPanel before and after setting the preferred size remains at 40.

I also tried using the Dimension variable, and that didn't work either.

  Dimension dimension; public SomePanel(){ dimension = new Dimension( 390, 40 ); ... } public expand(){ dimension.setSize( 390, 200 ); setPreferredSize( dimension ); } 
+2
source share
3 answers

Add frame.pack(); after somePanel.expand(); into your main() method. It will be done.

+3
source

You need to invalidate container hierarchy in order to redo the components.

Just call invalidate and then revalidate to the component you changed.

Here is a small example ...

 public class TestComponentHierarcy { public static void main(String[] args) { new TestComponentHierarcy(); } public TestComponentHierarcy() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new GridBagLayout()); frame.add(new Test()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class Test extends JPanel { private Dimension size = new Dimension(10, 10); public Test() { setLayout(new GridBagLayout()); addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { size.width += 10; size.height += 10; invalidate(); revalidate(); } }); } @Override public Dimension getPreferredSize() { return size; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.RED); g.drawRect(0, 0, getWidth() - 1, getHeight() - 1); } } } 
+3
source

+1 for everyone.

I usually use a combination of:

revalidate() and pack() . ( see @GagandeepBali and @StanislavL answer here to talk more about my choice of revalidate() ) regarding pack() , this allows the JFrame size JFrame fit the content.

  • don't call setPreferredSize rather override getPreferredSize from JPanel .

  • also do not call setSize(..) on the JFrame use the correct LayoutManager , which adjusts to the entire size of the added components and simply calls pack() before installing the JFrame .

  • And finally, but not enough to emphasize that building the foundation and manipulating Swing components in SwingUtilities.invokeXXX block / Event Dispatch Thread

Here is an example I made:

Basically a JPanel that overrides getPreferredSize and has a setPanelSize(int w,int h) method that changes the variables in the JPanel instance to return a new Dimension for getPreferredSize . after that I call revalidate() and pack() on the JFrame to discard the changes:

pic 1

enter image description here

 import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class Test { public Test() { initComponents(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Test(); } }); } private void initComponents() { final JFrame frame = new JFrame(); frame.setTitle("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final MyPanel myPanel = new MyPanel(); final JButton changeSizeButton = new JButton("Change size to 300x300"); changeSizeButton.addActionListener(new ActionListener() { boolean resized = false; @Override public void actionPerformed(ActionEvent ae) { if (resized) { myPanel.setPanelSize(200, 200); resized = false; changeSizeButton.setText("Change size to 300x300"); } else { myPanel.setPanelSize(300, 300); resized = true; changeSizeButton.setText("Change size to 200x200"); } frame.revalidate(); frame.pack(); } }); frame.add(myPanel); frame.add(changeSizeButton, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); } } class MyPanel extends JPanel { private int width, height; public MyPanel() { super(true); width = 200; height = 200; } @Override public Dimension getPreferredSize() { return new Dimension(width, height); } public void setPanelSize(int width, int height) { this.width = width; this.height = height; } } 
+2
source

All Articles