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 ); }
source share