-1

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();  
                    }
                }
            });
Andrew Brick
  • 276
  • 2
  • 4
  • 18
  • Your question is essentially one of, "why isn't my code working as expected". To get the best and quickest answer, you're going to want to create and post a [minimal example program or mcve](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels Mar 31 '15 at 16:16
  • Also note that you yourself can fire a notification event for the JDialog's SwingPropertyChangeSupport in your code. For instance if a button is pushed, you can call `firePropertyChange(...)`, but if you do this, make sure that you all this method on the same object that holds the listener, else nothing will happen. – Hovercraft Full Of Eels Mar 31 '15 at 16:26
  • so you want me to start my project from scratch, just so I can post a better question. I'm sure the issue isn't that big, and I can't think of how to explain it more clearly than I already have. I simply want to know why one dialog triggers the Property change, when the second dialog doesn't. – Andrew Brick Mar 31 '15 at 16:27
  • 1
    Andrew, I'm not telling you to do anything. However if you don't get an answer soon, then yes I would *suggest* that you consider writing a new program from scratch, one that exposes your problem in all its nakedness as this is a useful tool to use, not only for asking questions here, but also for solving the issue yourself. Whether you do this or not makes no difference to me since I'm not the one asking the question. But I've done this before when I've been in your shoes, and so I respect the tool. – Hovercraft Full Of Eels Mar 31 '15 at 16:29

1 Answers1

3

I would simplify the code, to solve the issue.

You seem to use the same listener for two different components, with a different behavior. I would suggest to separate your listener in two (or more) instances.

Just associate a listener to each dialog, instead of trying to figure out inside it from where it came. It will make it easier to understand what is going on, and easier to read in general.

If you had several properties to monitor, with different outcomes, you could even use addPropertChangeListener(String propertyName, PropertyChangeListener listener) instead, and make one listener per emitter, per property.

Gnoupi
  • 4,715
  • 5
  • 34
  • 50
  • I tried adding a new PropertyListener just for nameDialog, as shown in my edited question, however the nameDialog still doesn't trigger the event. Also is it always best to use an different listener for different components, because I thought splitting a single listener based on the source of the event does the same job. – Andrew Brick Mar 31 '15 at 17:22
  • To Gnoupi, 1+ for a decent answer to what was (for me) a confusing question. @AndrewBrick: hopefully this means that your question has now been fully answered and you know how to proceed from here. Please give us an update on if this is so or not. Thanks. – Hovercraft Full Of Eels Apr 01 '15 at 02:16
  • @AndrewBrick - I see that you put a new PropertyListener, but you kept its content identical. You don't need to check if the dialog is visible, neither do you need to check the source, in this case. When you say it doesn't trigger the event, do you mean you never enter in the propertyChange method when debugging, or that you don't get the actions you coded in it? – Gnoupi Apr 01 '15 at 15:18