0
votes

is it possible to change the size (length & width) of a JPanel at runtime? I have a JFrame, inside it I have a JPanel (placed inside a jscrollPane) and a JButton. When pressing on the JButton I want the size of the JPanel to change taking random values for length and width. To do this, I used the following as the actionListener for the JButton :

 public void actionPerformed(ActionEvent actionEvent) {

        jPanel1.repaint(); 
        jPanel1.revalidate();       

 }

and I tried to set the preferred size of the jPanel in the paintComponent() method taking random values for its dimension, but this didn't work. Any help would be appreciated.

2
For better help sooner, post an SSCCE. - Andrew Thompson

2 Answers

0
votes

What I did was set the preferred size and then invalidate.

panel.setPreferredSize(sizes.getCurrentScrollableDrawingPanel());
panel.invalidate();

Then you repaint the parent.

scrollPane.repaint();
0
votes

For me it works:

        getContentPane().remove( panel1);
        getContentPane().repaint();
        getContentPane().add(panel1);
        panel1.setBounds(0, 48, 100, 100);
        getContentPane().repaint();