I'm adding a panel at the bottom of a JFrame
, when a checkbox
is selected, and when I unmark it, remove the panel, and leave only the checkbox, however, I'm not getting it, I think the manager layout 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:
WhatIdid:
importjavax.swing.*;importjava.awt.*;publicclassTestextendsJFrame{privateJDesktopPanedesktopPane=newJDesktopPane();publicTest(){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);
}
}