何时在Swing应用程序中显示无效输入的错误消息 [英] When to display error messages for invalid input in Swing application

查看:121
本文介绍了何时在Swing应用程序中显示无效输入的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个 JFormattedTextField 来为Swing应用程序设置整数输入。无效输入的默认行为是静默地(即没有错误消息)将文本字段重置为其先前值。我想在将焦点返回到 JFormattedTextField 之前,使用 JOptionPane 显示错误消息。



我的原始解决方案是为我的文本字段实现 FocusListener 。我考虑将相同的 FocusListener 子类的实例添加到每个子类中,但由于我使用NetBeans GUI Builder,所以遇到问题。 InputVerifier 。我已经实现了以下自定义InputVerifier:

  public class ValidTextFormatInputVerifier extends InputVerifier {

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

在进一步研究的过程中,我得到的印象是 JFormattedTextField 在内部使用 InputVerifier 。 (我还没有发现任何地方,但是,尽管如此。)因此,我的自定义 InputVerifier 似乎是多余的。



更大的问题是文本字段仍然静默重置,没有任何错误消息。我可以在这里添加一个对 JOptionPane.showMessageDialog()的调用。但是,这似乎不是显示错误消息的适当位置。 InputVerifier 不知道无效输入的确切性质,也不知道有效输入的格式,这对于提供有意义的错误消息更为重要。 p>

想到的第一个解决方案是添加一个构造函数,它需要一个包含错误消息的 String 参数来显示。这似乎是将错误消息逻辑放在错误的地方,但是。



是否有一个常见的Java成语用于错误处理并向用户提供消息?你如何建议我设计我的应用程序以清理显示错误消息?此外,如果我决定使用不同的平台(例如命令行,移动设备等)为软件设计接口,我希望能够尽可能多地重用现有的代码库。 (当然,因为这个大部分是UI问题,也许这个要求并不像我想要的那么强烈)。

解决方案

我的第一个意见是,当文本字段焦点丢失时,向用户显示一个JOptionPane可能对用户非常烦人。当用户点击您的表单的确定按钮时,我会显示一条错误消息。错误消息可能包含所有错误消息的列表,或者仅包含第一个错误消息,并将元素重点放在错误中。



另一个选择是使用常规 JTextField 并编写自己的自定义验证器。当焦点更改时,您的IntegerValidator(或其他实现)将验证输入字符串,UI可能会在该字段旁边显示错误或警告图标,与某些Web应用程序相同。


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.)

解决方案

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.

这篇关于何时在Swing应用程序中显示无效输入的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆