0

I see this has been asked multiple times and apologize in advance if I'm just missing something simple...

I've created a custom JDialog with the examples provided in the Java docs here and from a similar question asked here.

My main application is a JFrame that contains a JPanel with an array of JButtons that display various employee names. I've added a custom ActionListener to each JButton that calls the mentioned JDialog:

//inner class for handling user button pushes
private class UserButtonHandler implements ActionListener
{
    //handle button event
    @Override
    public void actionPerformed(ActionEvent event)
    {
        statusDialog = new ChangeDialog(statusWindow);
        statusDialog.setVisible(true);
        statusDialog.setLocationRelativeTo(null);

        //set title for dialog box
        String dialogTitle = "Status change for " + event.getActionCommand();
        statusDialog.setTitle(dialogTitle);

        statNum = ((ChangeDialog) statusDialog).getInputStatus();
        System.out.println("Current num is: " + statNum);

        //statNum = statusDialog.getInputStatus();

    }
}

Here is the class for the custom JDialog (ChangeDialog):

class ChangeDialog extends JDialog implements ActionListener, PropertyChangeListener
{
    //create panel where users can modify their status
    private final ChangePanel empStatusChangePanel;

    //text of buttons in dialog
    private String btnString1 = "OK";
    private String btnString2 = "Cancel";
    private String btnString3 = "Clear Time-Off";

    private JOptionPane statusPane;

    //determines message to return for user input
    private int inputStatus; 

    public ChangeDialog(JFrame statusFrame)
    {
        empStatusChangePanel = new ChangePanel();

        //create an array specifying the number
        //of dialog buttons and their text
        Object[] options = {btnString1, btnString2, btnString3};

        //create the JOptionPane
        statusPane = new JOptionPane(empStatusChangePanel,
                JOptionPane.PLAIN_MESSAGE,
                JOptionPane.YES_NO_CANCEL_OPTION,
                null,
                options,
                options[0]);

        //set contents of dialog
        setContentPane(statusPane);

        //handle window closing
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        //register event handler for changes in status pane state
        statusPane.addPropertyChangeListener(this);
        pack();

    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        statusPane.setValue(btnString1);
    }

    @Override
    public void propertyChange(PropertyChangeEvent e)
    {
        String prop = e.getPropertyName();

        if (isVisible()
                && (e.getSource() == statusPane)
                && (JOptionPane.VALUE_PROPERTY.equals(prop)))
        {
            Object value = statusPane.getValue();

            if (value == JOptionPane.UNINITIALIZED_VALUE)
            {
                //ignore reset
                return;
            }

            //Reset the JOptionPane's value. If this is not done,
            //then if the user presses the same button next time,
            //no property change event will be fired
            statusPane.setValue(JOptionPane.UNINITIALIZED_VALUE);

            if(value.equals(btnString1)) //user clicked "OK"
            {
                //validation of user input
                inputStatus = empStatusChangePanel.validateUserInput();

                //handle validation results
                switch (inputStatus)
                {
                case 0: //user input is good
                    JOptionPane.showMessageDialog(this, "Good input given");
                    dispose();
                    break;

                case 1: //one (or both) of the date pickers are empty
                    JOptionPane.showMessageDialog(this, "PTO pickers can't be empty.",
                            "ERROR", JOptionPane.ERROR_MESSAGE);
                    break;

                case 2:
                case 3: //bad date range (start before end or visa-versa)
                    JOptionPane.showMessageDialog(this, "Bad date range.",
                            "ERROR", JOptionPane.ERROR_MESSAGE);
                    break;

                case 99: //dates are equal
                    JOptionPane.showMessageDialog(this, "Single-day PTO");
                    dispose();
                    break;
                }
            }
            else if(value.equals(btnString3)) //user clicked "Clear Input"
            {
                JOptionPane.showMessageDialog(this, "User clicked 'clear input");
                //more processing should be done here
                empStatusChangePanel.recycle();

                //dispose();
            }
            else //user clicked "Cancel" or closed dialog
            {
                JOptionPane.showMessageDialog(this,  "User closed status window");
                dispose();
            }
        }
    }

    //returns value from user validation
    public int getInputStatus()
    {
        return inputStatus;
    }

}

I need to access the method getInputStatus from the custom dialog but each attempt I've tried comes back stating that:

getInputStatus is undefined for the type JDialog

I have looked at several other similar posts but feel that I'm missing something fundamental in trying to solve this problem (or I've been looking at the code too long).

Another thing that has me stumped (and why I left it in the first snippet) is that if I cast the method to the type ChangeDialog

statNum = ((ChangeDialog) statusDialog).getInputStatus();

It suddenly has access (this was a suggestion from Eclipse and doesn't make sense to me). Thanks again for any and all help.

Community
  • 1
  • 1
Probius
  • 79
  • 10
  • Note that although the source of your confusion is obvious, you've not asked a question. What is the question? – Andrew Thompson Mar 20 '17 at 22:14
  • 2
    How is `statusDialog` declared? I'm assuming is something like `private JDialog statusDialog;`? This means that you can only access the functionality of the `JDialog` directly, without casting it, which you have. This is a basic concept of polymorphism – MadProgrammer Mar 20 '17 at 22:15
  • See also [Inheritance](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html) Casting Objects. – Andrew Thompson Mar 20 '17 at 22:16
  • I feel like I may have wasted your time...my question for this post would have been "how do I access the method getInputStatus() from my custom JDialog class ChangeDialog. In regards to the comment from @MadProgrammer I realized that I incorrectly declared statusDialog. It should have been `private ChangeDialog statusDialog;` instead of `private JDialog statusDialog` which is what I used. Really sorry about this, I was looking for my error in the wrong section of my code (and even then I'm not sure if I would have seen it right away). Thanks! – Probius Mar 21 '17 at 13:29

1 Answers1

0

That is how inheritance works, you have defined statusDialog as a JDialog reference and JDialog doesn't have a getInputStatus method. To access the members of ChangeDialog, you have to define statusDialog as variable of ChangeDialog.

Zak
  • 114
  • 3