I have two similar JDialogs in my applet, both act on the input given in the text fields and have Enter and Cancel buttons. I've added a property listener to both both dialogs, however only one of the dialogs triggers the property listener and not the second. Is there some sort of constraint on using PropertyListeners I'm abusing and unaware of. I've presented my code for the propertyChange() method below. the addQuestionDialog works perfectly fine, however the nameDialog just doesn't trigger the propertyChange event as none of the print statements I've added in appear in my console. At first I thought maybe the condition (JOptionPane.VALUE_PROPERTY.equals(prop) || JOptionPane.INPUT_VALUE_PROPERTY.equals(prop)) was never true, but taking those out of the "if" block didn't make a difference. Second I thought maybe I just didn't add the Property listener for nameDialog, but I've checked and it's definitely been added. I don't know what else could cause the issue, since addQuestionDialog is almost similar and works perfectly fine.
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (addQuestionDialog.isVisible() && (e.getSource() == addQuestionPane) && (JOptionPane.VALUE_PROPERTY.equals(prop) || JOptionPane.INPUT_VALUE_PROPERTY.equals(prop))) {
Object value = addQuestionPane.getValue();
if (value == JOptionPane.UNINITIALIZED_VALUE) {
//ignore reset
return;
}
//Reset the JOptionPane's value.
//If you don't do this, then if the user
//presses the same button next time, no
//property change event will be fired.
addQuestionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
if (value.equals("Enter")) {
String questionTypedText = questionTextField.getText();
String mqlYesTypedText = mqlYesTextField.getText();
String mqlNoTypedText = mqlNoTextField.getText();
sqlModel.addQuestion(questionTypedText, mqlYesTypedText, mqlNoTypedText);
questionTextField.setText("");
mqlYesTextField.setText("");
mqlNoTextField.setText("");
} else { //user closed dialog or clicked cancel
addQuestionDialog.setVisible(false);
}
}
else if (nameDialog.isVisible() && (e.getSource() == namePane) && (JOptionPane.VALUE_PROPERTY.equals(prop) || JOptionPane.INPUT_VALUE_PROPERTY.equals(prop))) {
Object value = namePane.getValue();
System.out.println("Entered name dialog box");
if (value == JOptionPane.UNINITIALIZED_VALUE) {
//ignore reset
return;
}
//reset value
namePane.setValue(JOptionPane.UNINITIALIZED_VALUE);
if (value.equals("Enter")) {
System.out.println("gonna add new celeb from input");
sqlModel.addCelebrity(alreadyAskedQuestions, columnValues, nameTextField.getText());
}
//else, user clicked cancel, in either case, close the Dialog box
nameDialog.setVisible(false);
initGUI();
}
}
EDIT: I've tried adding a separate PropertyListener for the nameDialog, but the nameDialog still doesn't trigger the event.
nameDialog.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (nameDialog.isVisible() && (e.getSource() == namePane) && (JOptionPane.VALUE_PROPERTY.equals(prop) || JOptionPane.INPUT_VALUE_PROPERTY.equals(prop))) {
Object value = namePane.getValue();
System.out.println("Entered name dialog box");
if (value == JOptionPane.UNINITIALIZED_VALUE) {
//ignore reset
return;
}
//reset value
namePane.setValue(JOptionPane.UNINITIALIZED_VALUE);
if (value.equals("Enter")) {
System.out.println("gonna add new celeb from input");
sqlModel.addCelebrity(alreadyAskedQuestions, columnValues, nameTextField.getText());
}
//else, user clicked cancel, in either case, close the Dialog box
nameDialog.setVisible(false);
initGUI();
}
}
});