简体   繁体   English

Java Swing JTextField setInputVerifier继续关注TextField

[英]Java Swing JTextField setInputVerifier keep focus on TextField

I have a public class JdbDateTextField extends JTextField and in the constructor I add this.setInputVerifier(new ValidDateOrEmptyVerifier()); 我有一个public class JdbDateTextField extends JTextField并在构造函数中添加this.setInputVerifier(new ValidDateOrEmptyVerifier()); .

I use class ValidDateOrEmptyVerifier extends InputVerifier to verify the format of the input. 我使用class ValidDateOrEmptyVerifier extends InputVerifier来验证输入的格式。

If the input is in the wrong format and the user looses the focus of the JdbDateTextField , I return false in the ValidDateOrEmptyVerifier and the focus is gained again to the JdbDateTextField again. 如果输入错误的格式和用户失去了关注的焦点JdbDateTextField ,我返回falseValidDateOrEmptyVerifier和焦点再次上涨到JdbDateTextField一次。

This works if the user switches from the JdbDateTextField to another textField or presses a Button. 如果用户从JdbDateTextField切换到另一个textField或按下Button,则此方法有效。 If pressing a button and the format of the input in the is wrong then no action for the button is performed and the focus is still at the JdbDateTextField . 如果按下按钮并且输入的格式错误,则不执行该按钮的操作,焦点仍然在JdbDateTextField

This is exactly what I want. 这正是我想要的。 The user can not leave the JdbDateTextField until he enters a valid string. 用户在输入有效字符串之前不能离开JdbDateTextField

The problem is that the JdbDateTextField is in a JPanel which is in a JTabbedPane so I have a GUI with several tabs. 问题是JdbDateTextField位于JTabbedPane中的JPanel中,所以我有一个带有几个选项卡的GUI。

If I have the JdbDateTextField selected, enter a invalid input and then directly click on another tab it still switches the tab. 如果我选择了JdbDateTextField ,请输入无效输入,然后直接单击另一个选项卡,它仍会切换选项卡。 So I was able to provide a wrong input. 所以我能够提供错误的输入。

My Question is: 我的问题是:

Is there a way to perform an Input Verification which does not allow to execute any other event before it is true 有没有之前,它是执行输入验证不允许执行任何其它事件的方式true

The best solution I can think of is to assign the JTabbedPane a custom selection model which refuses to allow changing tabs unless the current InputVerifier succeeds: 我能想到的最佳解决方案是为JTabbedPane分配一个自定义选择模型,该模型拒绝允许更改选项卡,除非当前的InputVerifier成功:

int index = tabbedPane.getSelectedIndex();

tabbedPane.setModel(new DefaultSingleSelectionModel() {
    @Override
    public void setSelectedIndex(int index) {
        Component focusOwner =
            FocusManager.getCurrentManager().getFocusOwner();

        if (focusOwner instanceof JComponent) {
            JComponent c = (JComponent) focusOwner;
            InputVerifier verifier = c.getInputVerifier();
            if (verifier != null && !verifier.shouldYieldFocus(c)) {
                return;
            }
        }

        super.setSelectedIndex(index);
    }
});

tabbedPane.setSelectedIndex(index);

I hope that I can do this as an answer: 我希望我能做到这一点作为答案:

The solution above works for the JTabbedPane 上述解决方案适用于JTabbedPane

But I can still select other GUI elements. 但我仍然可以选择其他GUI元素。

My Application is build like this: 我的应用程序是这样构建的: 在此输入图像描述

For every Person ID I show other Birthdate values in the JTabbedPane and I can still switch to another Person ID if the ValidDateOrEmptyVerifier returns false . 对于每个Person ID我在JTabbedPane显示其他Birthdate值,如果ValidDateOrEmptyVerifier返回false ,我仍然可以切换到另一个Person ID

So is there a way to disallow all events in the Main Frame until the ValidDateOrEmptyVerifier returns true . 那么有没有办法禁止主框架中的所有事件,直到ValidDateOrEmptyVerifier返回true

So Basically what I want is that the user can only exit the "Birthdate" JdbDateTextField if he enters a valid Date or the field is empty. 基本上我想要的是用户只有在输入有效日期或字段为空时才能退出"Birthdate" JdbDateTextField

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM