JCheckbox があり、チェックボックスをクリックすると、ラベル内にテキストを設定したい。
これまでのところ、変数を宣言し、チェックボックスとラベルのフレーム内の設定を行いました。
JCheckBox chckbxIncludeExt;
JLabel lblNewLabel_1;
JCheckBox chckbxIncludeExt = new JCheckBox("Include ext");
chckbxIncludeExt.setForeground(UIManager.getColor("Button.background"));
chckbxIncludeExt.setBackground(UIManager.getColor("Button.darkShadow"));
chckbxIncludeExt.setBounds(0, 264, 113, 25);
panel.add(chckbxIncludeExt);
JLabel lblNewLabel_1 = new JLabel("");
lblNewLabel_1.setBounds(12, 311, 230, 16);
panel.add(lblNewLabel_1);
クラスを作成し、項目リスナーをチェックボックスに追加しました。
CheckboxStatus checkboxd = new CheckboxStatus();
chckbxIncludeExt.addItemListener(checkboxd);
これは私が作成したクラスです:
private class CheckboxStatus implements ItemListener{
public void itemStateChanged(ItemEvent event){
if (chckbxIncludeExt.isSelected()){
lblNewLabel_1.setText("You have selected the checkbox");
}
}
}
これにより、次のようなエラーが表示されます。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.example.android.apis.appwidget.VFSTool$CheckboxStatus.itemStateChanged(VFSTool.java:199)
at javax.swing.AbstractButton.fireItemStateChanged(Unknown Source)
エラーは、ラベルが 199 行目にテキストを設定する 199 行目にあります。ボックスをチェックすると、これが表示され、テキストは表示されません。