There are CheckBoxMenuItems и ButtonGroup. When I set the listener for current CheckBoxMenuItem, the condition is checked and the error is produced in this listener. I have active another CheckBoxMenuItem, and it is not necessary for me, even I will write "return". Problem is that the method cannot throw exceptions and the class is anonymous. Here is the code:
mUserMode.addActionListener(new ActionListener() {
@Override
public void action开发者_Python百科Performed(ActionEvent e) {
if(currentCard == 0) {
return;
}
boolean IsEmptyFields = true, isCheckedAnswers = false;
// check if all fields is fill in ...
endOfCycle: for(Component component: panelForAddingQuesions.getComponents()) {
if(component instanceof JTextField) {
JTextField question = (JTextField)component;
if(question.getText().length() == 0) {
IsEmptyFields = false;
break endOfCycle;
}
}
}
// and if there is one correct answer in every question
// check if all fields is fill in ...
for(Entry<JTextField, ArrayList<JCheckBox>> entrySets: equivalenceOfQuestionFiledsAndItsAnswers.entrySet()) {
isCheckedAnswers = false;
for(JCheckBox checkbox: entrySets.getValue()) {
if(checkbox.isSelected()) {
isCheckedAnswers = true;
}
}
}
if(IsEmptyFields) {
JOptionPane.showMessageDialog(MainActivity.this,
"Error", "Error",
JOptionPane.ERROR_MESSAGE);
}
else if(isCheckedAnswers) {
JOptionPane.showMessageDialog(MainActivity.this,
"Error","Error",
JOptionPane.ERROR_MESSAGE);
}
else {
cardLayout.last(cardPanel);
currentCard = 0;
}
// It doesn't help
//MainActivity.this.mAdminMode.setEnabled(true);
}
});
There is the method(аctionPerformed) in anonymous class. I want on a condition to cancel switching ChechBoxItem of elements i.e. to stop this operation. But as ,anyway , the method аctionPerformed is completed, there will be automatic a switching of checkboxes as it will be notified View. And I need to prevent it directly in a method actionPerformed
You should call MainActivity.this.mAdminMode.setSelected(true);
, not setEnabled(true)
.
精彩评论