This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 157791 - Bug in tutorial: Creating a Custom Desktop Database Application
Summary: Bug in tutorial: Creating a Custom Desktop Database Application
Status: RESOLVED WONTFIX
Alias: None
Product: guibuilder
Classification: Unclassified
Component: Code (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: issues@guibuilder
URL: http://www.netbeans.org/kb/docs/java/...
Keywords:
Depends on:
Blocks:
 
Reported: 2009-02-02 16:20 UTC by rcushman
Modified: 2011-10-19 09:33 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description rcushman 2009-02-02 16:20:21 UTC
Bug in tutorial: Creating a Custom Desktop Database Application

I traced strange behavior I noticed in the Order Editor dialog of this tutorial to a bug in the DateVerifier class.

Justification for calling this a bug:
      - Exhibit A: There should be no need to "reinstall" the input verifier. This code raises a red flag.
      - Exhibit B: If one enters an invalid date, then clicks Save or Cancel, the button remains drawn as being pressed,
apparently due to the MOUSE_RELEASED and MOUSE_CLICKED messages not being dispatched.
      - Exhibit C: Commenting out the showMessageDialog() call in the DateVerifier changes the functionality: The
algorithm no longer prevents the dialog from closing in case the Save button is clicked and the date is invalid.

Apparently the reason for the strange behavior is that Swing is not designed to accommodate a call to
showMessageDialog() from shouldYieldFocus().  Is this a valid restriction on the part of Swing?

My original solution was to move the showMessageDialog() call to OrderEditor.saveOrder().  Unfortunately this prevents
the message dialog from being shown in case of attempted focus changes other than clicking Save.  (IMO the message
dialog should not be shown if the Cancel button is clicked, but it does need to be shown in other cases such as pressing
the Tab key.)

Following is my original solution.

Replacement class DateVerifier:

public class DateVerifier extends InputVerifier {
    public DateVerifier() {
        inputOK = true;
    }

    public boolean shouldYieldFocus(JComponent input) {
        inputOK = verify(input);
        return inputOK;
    }

    public boolean verify(JComponent input) {
        if (!(input instanceof JFormattedTextField))
            return true;
        return ((JFormattedTextField) input).isEditValid();
    }

    private boolean inputOK;
   
    public boolean isInputOK() {
        return inputOK;
    }
}

Replacement method OrderEditor.saveOrder():

    private void saveOrder(java.awt.event.ActionEvent evt) {
        if (dateVerifier1.isInputOK()) {
            setOrderConfirmed(true);
            setVisible(false);
        } else {
            String failedVerificationMessage = "Date must be in the MMM DD, YYYY format. For example: Apr 17, 2008";
            JOptionPane.showMessageDialog(null, //no owner frame
                                          failedVerificationMessage,
                                          "Invalid Date Format", //title
                                          JOptionPane.WARNING_MESSAGE);
        }
    }

I'll work out a better solution, time permitting.
Comment 1 rcushman 2009-02-03 00:26:22 UTC
My revised solution solves A and C, but not B.

Perhaps a bug in Swing is causing B.

Following is my revised solution:

Replacement class DateVerifier:

public class DateVerifier extends InputVerifier {
    public DateVerifier() {
        inputOK = true;
    }

    public boolean shouldYieldFocus(JComponent input) {
        inputOK = verify(input);
        if (!inputOK) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    if (!inputOK) {
                        String failedVerificationMessage = "Date must be in the MMM DD, YYYY format. For example: Apr
17, 2008";
                        JOptionPane.showMessageDialog(null, //no owner frame
                                                  failedVerificationMessage,
                                                  "Invalid Date Format", //title
                                                  JOptionPane.WARNING_MESSAGE);
                    }
                }
            });
        }
        return inputOK;
    }

    public boolean verify(JComponent input) {
        if (!(input instanceof JFormattedTextField))
            return true;
        return ((JFormattedTextField) input).isEditValid();
    }

    private boolean inputOK;

    public boolean isInputOK() {
        return inputOK;
    }

    public void setInputOK(boolean input) {
        inputOK = input;
    }
}

Replacement methods OrderEditor.saveOrder() and OrderEditor.cancelOrder():

    private void saveOrder(java.awt.event.ActionEvent evt) {
        if (dateVerifier1.isInputOK()) {
            setOrderConfirmed(true);
            setVisible(false);
        }
    }

    private void cancelOrder(java.awt.event.ActionEvent evt) {
        dateVerifier1.setInputOK(true);
        setOrderConfirmed(false);
        setVisible(false);
    }
Comment 2 Jan Stola 2011-10-19 09:33:13 UTC
NetBeans support of Swing Application Framework (i.e., Java Desktop Applications) has been discontinued. Hence, I am closing this issue as 'will not fix'.