Program not close but when i click my checkbox i get an exception like this;
my class is here and it is extends from JPanel,my class is here and it is extends from JPanel,my class is here and it is extends from JPanel,my class is here and it is extends from JPanel
public class MyJTestPanel extends JPanel {
private JCheckBox cboxInfo;
private JLabel lblResult;
private JButton btnAgain;
public MyJTestPanel(String testName, String testContext){
setLayout(new MigLayout());
setMaximumSize(new Dimension(400, 30));
setName(testName);
cboxInfo = new JCheckBox(testContext);
add(cboxInfo, "gapright 10, height 30, width 250, cell 0 0");
lblResult = new JLabel();
lblResult = new JLabel();
lblResult.setBackground(Color.RED);
lblResult.setOpaque(true);
add(lblResult, "gapright 10, height 30, width 30, cell 1 0");
btnAgain = new JButton("Test again");
btnAgain.setVisible(false);
add(btnAgain,"height 30, width 80, cell 2 0");
}
public JCheckBox getCboxInfo() {
return cboxInfo;
}
public JLabel getLblResult() {
return lblResult;
}
public JButton getBtnAgain() {
return btnAgain;
}
}
and my clicklistener is here
cboxSelectAll.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
boolean isChecked = ((JCheckBox) e.getItem()).isSelected();
for (int i = 0; i < getCenterPanel().getComponents().length; i++) {
((MyJTestPanel) getCenterPanel().getComponents()[i]).getCboxInfo().setSelected(isChecked);//exception is here...
}
}
});
Is there any idea for this exception?
You are trying to cast the result of getCenterPanel().getComponents()[i] to a MyJTestPanel, and one of the Components you encounter is a JPanel.
If all the Components you encounter in that loop are a MyJTestPanel / extend MyJTestPanel then the cast would work, but at least one, the JPanel, does not meet this criteria and so the cast fails.