1
I’m adding a panel at the bottom of a JFrame
, when a checkbox
this selected, and when I uncheck it, remove the panel, and leave only the checkbox, however, I’m not getting, I think the layout manager is preventing the effect I wanted.
My question is, how can I remove the panel, and leave the check box always in the lower left corner, taking up little space ?
Ex:
What I did:
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame {
private JDesktopPane desktopPane = new JDesktopPane();
public Test() {
setTitle("Teste");
getContentPane().add(desktopPane);
add("South", new Info(comp()));
setSize(700, 450);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private JComponent comp() {
JPanel painel = new JPanel();
painel.add(new JLabel("Informações ... "));
return painel;
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
new Test();
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
class Info extends JPanel {
private JCheckBox checkBox = new JCheckBox("Click");
private JComponent component;
public Info(JComponent component) {
this.component = component;
setPreferredSize(new Dimension(30, 30));
add(checkBox);
checkBox.addActionListener(e -> {
if (checkBox.isSelected()) {
component.setVisible(true);
component.setPreferredSize(new Dimension(500, 30));
} else {
component.setVisible(false);
component.setPreferredSize(new Dimension(0, 0));
}
revalidate();
});
add(checkBox, BorderLayout.WEST);
add(component, BorderLayout.EAST);
}
}
Why are you using desktoppane for something so trivial? What’s the need?
– user28595
In my system, I use, it has more screens, menus and etc. I didn’t want to waste time putting it here, since it won’t influence.
– AlunoOracle
You need a panel for that Jlabel anyway?
– user28595
The label was just to show, I will put other things, the important thing, is to exbir and remove this panel, leaving the label always there in the corner
– AlunoOracle