有什么可以阻止 FocusListener 正常工作吗?

问题描述 投票:0回答:1

我试图在一些

FocusListener
JTextFields
上添加
JFormattedTextFields
,做得很好,当类加载时,监听器工作正常,这是一个简单的方法,可以在焦点时选择 TextFields 上的所有文本获得了,但是
TextField.selectAll()
只能工作一次,然后当我关注其他东西时,ant 将焦点返回到 Field 它什么都不做,但监听器本身正在工作,我尝试添加
System.out.println(evt.getComponent())
并打印出结果,但是焦点不再选择所有文本...

整个 Frame 类很大,并且非常具体,有一些 IDE 生成的代码 ́\_(ツ)_/́,这就是为什么我只添加两个字段,我相信这可能有助于理解我在这里得到的内容......

    tfVencimento = new javax.swing.JFormattedTextField(dateMask());
    tfValorBruto = new javax.swing.JFormattedTextField(decimalMask());
    tfValorBruto.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));

    tfValorBruto.setText("0,00");
    tfValorBruto.setMargin(new java.awt.Insets(2, 4, 2, 4));
    tfValorBruto.setMinimumSize(new java.awt.Dimension(70, 22));
    tfValorBruto.setPreferredSize(new java.awt.Dimension(70, 22));

    tfValorBruto.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
            FocusGained(evt);
        }
    });
    tfVencimento.setMargin(new java.awt.Insets(2, 4, 2, 4));
    tfVencimento.setMinimumSize(new java.awt.Dimension(70, 22));
    tfVencimento.setPreferredSize(new java.awt.Dimension(70, 22));
    tfVencimento.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
            FocusGained(evt);
        }
    });
    private void FocusGained(java.awt.event.FocusEvent evt) {                             
        JTextField field = (JFormattedTextField) evt.getComponent();
        field.selectAll();
    }

我认为这可能是覆盖我的听众的东西,但没有找到任何东西......
也许是我应用监听器的方式,尝试了几种方法,然后全部实现了......

如果我在每个字段上应用一个侦听器,每个字段都使用不同的方法(如下面的代码),它可以工作,但我有 10 多个字段,所以做起来很无聊,并且很难维护代码......

    tfVencimento.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
            tfVencimentoFocusGained(evt);
        }
    });

    private void tfVencimentoFocusGained(java.awt.event.FocusEvent evt) {                             
        tfVencimento.selectAll();
    }
java swing events listener focuslistener
1个回答
0
投票

解决方案

我看到了几个选项:

选项 A. 致电

tfValorBruto.setCaret(new DefaultCaret());

选项 B。在

FocusGained
中:将调用移至稍后调用的 Runnable 内的
selectAll()
,因此它会稍后运行。像这样:

    private void FocusGained(java.awt.event.FocusEvent evt) {
        JTextField field = (JFormattedTextField) evt.getComponent();
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                field.selectAll();
            }
        });
    }

问题

我用这段代码来帮助调试它:

        tfValorBruto = new javax.swing.JFormattedTextField() {

            public void moveCaretPosition(int pos) {
                super.moveCaretPosition(pos);
            }

            public void setCaretPosition(int position) {
                super.setCaretPosition(position);
            }
        };

您可以在

moveCaretPosition(int)
中添加调试器断点。它显示在 FOCUS_GAINED 事件上有人打电话给
moveCaretPosition(0)

Breakpoint reached
    at com.test.FocusTest$1.moveCaretPosition(FocusTest.java:22)
    at com.apple.laf.AquaCaret.focusGained(AquaCaret.java:135)
    at java.awt.AWTEventMulticaster.focusGained(AWTEventMulticaster.java:228)
    at java.awt.AWTEventMulticaster.focusGained(AWTEventMulticaster.java:227)
    at java.awt.AWTEventMulticaster.focusGained(AWTEventMulticaster.java:227)
    at java.awt.Component.processFocusEvent(Component.java:6509)
    at javax.swing.JFormattedTextField.processFocusEvent(JFormattedTextField.java:645)
    at java.awt.Component.processEvent(Component.java:6376)
    at java.awt.Container.processEvent(Container.java:2266)
    at java.awt.Component.dispatchEventImpl(Component.java:4995)
    at java.awt.Container.dispatchEventImpl(Container.java:2324)
    at java.awt.Component.dispatchEvent(Component.java:4827)
    at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1952)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:1061)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:687)
    at java.awt.Component.dispatchEventImpl(Component.java:4876)
    at java.awt.Container.dispatchEventImpl(Container.java:2324)
    at java.awt.Component.dispatchEvent(Component.java:4827)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:775)
    at java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.awt.EventQueue$4.run(EventQueue.java:714)
    at java.security.AccessController.executePrivileged(AccessController.java:776)
    at java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
    at java.awt.EventQueue$5.run(EventQueue.java:747)
    at java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.security.AccessController.executePrivileged(AccessController.java:776)
    at java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:744)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

所以在我的例子中:默认的外观实现(Aqua)在收到 FOCUS_GAINED 事件时试图设置一些默认行为。

我们可以更换插入符(这是解决方案 A),或者我们可以在 Aqua 之后再次更改选择(这是解决方案 B)

© www.soinside.com 2019 - 2024. All rights reserved.