sg552
sg552

Reputation: 1543

Empty space still visible after hiding panel

I use Netbean 8 and I hide Jpanel with this code

public NewJFrame() {
    initComponents();

    jPanel3.setVisible(false);
    jPanel4.setVisible(false);
}

but I still have empty space because of the hidden element. Below is what I'm trying to achieve. When I select radio button the hidden element will be shown and the frame will expand downward. enter image description here

Please help and thanks in advance.

Upvotes: 0

Views: 1066

Answers (2)

sg552
sg552

Reputation: 1543

Can't believe how easy it was. Using pack() method will automatically resizes the JFrame based on the size of the components. So I just add pack() to my code:

public NewJFrame() {
    initComponents();
    jPanel3.setVisible(false);
    jPanel4.setVisible(false);

    pack();
}

and it works.

Upvotes: 0

k4yaman
k4yaman

Reputation: 485

Maybe you wanna setBounds(100, 100, 450, 150); on Change event where you evaluate the checked status:

JCheckBox checkBox = new JCheckBox("Student");
checkBox.addChangeListener(new ChangeListener() {
   public void stateChanged(ChangeEvent e) {
       System.out.println("Checked? " + checkBox.isSelected());
   }
});

Upvotes: 1

Related Questions