When to display error messages for invalid input i

2019-01-29 00:30发布

I am using a JFormattedTextField for to format integer input for a Swing app. The default behavior for invalid input is to quietly (i.e. no error message) reset the text field to it's prior value. I want to display an error message with JOptionPane before returning focus to the JFormattedTextField.

My original solution was to implement FocusListeners for my text fields. I considered adding instances of the same FocusListener subclass to each one, but have encountered problems since I am using the NetBeans GUI Builder. @HovercraftFullOfEels suggested that I use an InputVerifier. I have implemented the following custom InputVerifier:

public class ValidTextFormatInputVerifier extends InputVerifier {

    @Override
    public boolean verify(JComponent jc) {
        return ((JFormattedTextField)jc).isEditValid();
    }
}

While looking into this further, I get the impression that JFormattedTextField uses InputVerifier internally. (I haven't found this explicitly stated anywhere yet, though.) Because of this, my custom InputVerifier seems superfluous.

The bigger issue is that the text field is still reset silently without any error message. I could add a call to JOptionPane.showMessageDialog() here. However this does not seem an appropriate place to display error messages. The InputVerifier does not know the exact nature of the invalid input, nor does it know the format of valid input, which is even more crucial to providing a meaningful error message.

The first solution that comes to mind is to add a constructor which takes a String parameter that contains the error message to display. This seems like I'm putting the error message logic in the wrong place, though.

Is there a common Java idiom for error handling and providing messages to the user? How do you suggest that I design my application to cleanly display error messages? Also, if I decide to design an interface for the software using a different platform (e.g. command-line, mobile, etc.), I want to be able to reuse as much of my existing code-base as possible. (Of course, since this is mostly a UI issue, perhaps that requirement doesn't apply as strongly as I would like here.)

2条回答
小情绪 Triste *
2楼-- · 2019-01-29 00:59

My first opinion is that showing a JOptionPane to the user whenever the text field focus is lost could be very annoying for users. I would display an error message when the user hits the OK button of your form. The error message could contain a list of all error messages or only the first one and focus the element with the error.

Another choice would be to use regular JTextField and write your own custom validators. When focus changes, your IntegerValidator (or other implementation) would validate the input string and the UI could display an error or warning icon next to the field, the same as some web applications do.

查看更多
Explosion°爆炸
3楼-- · 2019-01-29 01:12

I'd have a look at JLayer (Java 7) or JXLayer (Java 6 & below)

This would allow you to decorate the field or form with a verity of different states.

As to when.

I'd start by using some kind of highlighter, Dan suggest using icons, which is a great idea, coupled with JLayer/JXLayer this would reduce the overall effect on the form.

I'd wait until the user tries to submit the form before displaying a dialog & forcing focus back to the invalid fields.

The basic idea would be to allow the user the freedom to move about the form without to much restriction (cause not all people think in a linear manner) but encourage them to correct any possible errors they are producing along the way without having to wait till they submit the form to find the errors (think about the worse web form you've ever used and don't do that)

Obviously, if they try and submit the form with errors, you'll want to display and error message and highlight the first offending field

For examples of JLayer/JXLayer have a look at Validation overlays using JXLayer & How to decorate components

查看更多
登录 后发表回答