I have a problem with my code below. Everytime i uncheck the checkbox, the checkbox itself disappears. I have tried several things alrady (like setMinimumSize and setSize) for the panel where the checkbox is located. Can anyone please tell me what i could do to resolve the problem?
class userGUIFrame extends JFrame
{
public userGUIFrame()
{
setSize(300,200);
setTitle("Sample GUI");
setResizable(false);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
});
contentPane = getContentPane();
/* Functions to add a scrollable status text area */
/* Tentatively creates a JList with and empty null model */
DefaultListModel model = new DefaultListModel();
logDisp = new JTextArea();
logDisp.setEditable(false);
scrollPane = new JScrollPane(logDisp);
/* Functions to add button to the JPanel */
JPanel actionPanel = new JPanel(new GridLayout(2,1));
/* Choice Panel */
JPanel choicePanel = new JPanel();
JCheckBox logCB;
logCB = addChoices(choicePanel,"Show Logs",true);
logCB.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent evt)
{
if (evt.getStateChange() == ItemEvent.DESELECTED)
{
contentPane.remove(scrollPane);
contentPane.setSize(300,150);
setSize(300,150);
}
else if (evt.getStateChange() == ItemEvent.SELECTED)
{
setSize(300,200);
contentPane.setSize(300,200);
contentPane.add(scrollPane,"Center");
}
contentPane.validate();
contentPane.repaint();
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(new JButton("OK"));
actionPanel.add(buttonPanel,"North");
actionPanel.add(choicePanel,"Center");
actionPanel.setMinimumSize(buttonPanel.getSize());
/* Populate the entire frame with the components */
contentPane.add(scrollPane,"Center");
contentPane.add(actionPanel,"South");
}
private JCheckBox addChoices(Container c,
String caption,boolean selected)
{
JCheckBox checkBox = new JCheckBox(caption,selected);
c.add(checkBox);
return checkBox;
}
private Container contentPane;
private JScrollPane scrollPane;
public JTextArea logDisp;
}