【问题标题】:JFormattedTextfield.selectAll() does not work when formatting the text格式化文本时 JFormattedTextfield.selectAll() 不起作用
【发布时间】:2016-02-28 10:03:12
【问题描述】:

我有很多不同的 JFormattedTextFields,带有动作和按键监听器。每个字段都有一个 keylistener,所以当我按下回车键时,我将关注下一个 JFormattedTextField。问题是,对于某些 JFormattedTextFields,我的代码正在格式化输入,然后将文本设置为新的,而对于那些 selectAll() 不起作用。

JFormattedTextField a = new JFormattedTextField(someDouble);
JFormattedTextField b = new JFormattedTextField(someDouble2);
a.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            leasingfaktor1Field.selectAll();
            if(...) {
                //do something
                a.setText(tausenderPunkt(someValue));
            }   
        }
    });
a.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == 10) {
                b.requestFocusInWindow();
            }
        }
    });
b.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            leasingfaktor1Field.selectAll();
            if(...) {
                //do something
                b.setText(tausenderPunkt(someValue));
            }   
        }
    });
b.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == 10) {
                c.requestFocusInWindow();
            }
        }
    });

函数tausenderPunkt():

public String tausenderPunkt(double value) {
    String s = String.format("%1$,.2f", value);
    return s;
}

所以当我的光标在字段 a 中并且我按下回车键时,光标转到字段 b 但不选择文本或值。当我不使用 setText() 时,我没有问题。有人有解决方案吗?

编辑:对于某些 JFormattedTextFields,解决方案是将 selectAll() 添加到 keyAdapter,但不是全部。 例如:

b.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == 10) {
                c.requestFocusInWindow();
                c.selectAll();
            }
        }
    });

编辑2: 问题似乎出在我创建 JFormattedTextFields 时。 当我不在构造函数中使用值创建它们时,它可以工作。 但我必须这样做。

【问题讨论】:

  • 您是否尝试过切换,即先执行setText,然后执行selectAll()?您也可以尝试在invokeLater 块内执行selectAll
  • 我无法切换顺序,因为首先程序应该 selectAll() 以便用户可以覆盖该值,然后 setText() 对其进行格式化(例如数字)。我试过invokeLater,但它什么也没做。 mouseAdapter 和 selectAll() 工作正常。
  • 每次我遇到这样的场景时,始终有效的解决方案是使用SwingWorker在后台进行初始化,在方法@​​987654330@完成后,我使用@ 987654331@方法完成任务。
  • 不应该在 AbstractFormatter 中进行格式化吗? docs.oracle.com/javase/7/docs/api/javax/swing/…

标签: java settext jformattedtextfield selectall


【解决方案1】:

在转到下一个文本字段之前,您应该考虑处理当前关注的文本字段的所有必需条件,这当然包括提供给该字段的值或文本的格式。一旦满足所有所需的条件,然后转到下一个文本字段。

实际上,这一切都可以通过针对您的特定情况的 keyPressed 事件来完成。您的任何文本字段都不需要 actionPerformed 事件,例如:

a.addKeyListener(new KeyAdapter() {
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            checkConditions(a, b);
        }
    }
});

b.addKeyListener(new KeyAdapter() {
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            checkConditions(b, c);
        }
    }
});
//----------  and so on  -------------

这里有一个简单的方法来消除重复代码的需要:

private void checkConditions(JFormattedTextField fieldA, JFormattedTextField fieldB) {
    // Make sure something is contained within fieldA and 
    // that it's actually numerical text.
    if(!fieldA.getText().isEmpty() && 
                fieldA.getText().matches("([-]?)\\d+([,]\\d+)?(([.]\\d+)?)")) {
        // Convert the supplied text to Double and
        // ensure the desired numerical formating.
        String res = (String)tausenderPunkt(Double.parseDouble(fieldA.getText().replace(",","")));
        fieldA.setText(res);
        // Set Focus to our next text fieldB.
        fieldB.requestFocusInWindow();
        // Highlight the contents (if any) within the
        // next text fieldB.
        fieldB.selectAll();
    }
    // If fieldA is empty or fieldA does not contain 
    // numerical text then inform User and re-highlight
    // the entry in fieldA.
    else {
        JOptionPane.showMessageDialog (null, "Please Enter Numerical Values Only!", 
                "Incorrect Entry", JOptionPane.WARNING_MESSAGE);
        fieldA.selectAll();
    }
}

如果您希望在第一个文本字段的内容在其上建立焦点后立即突出显示(选项卡或单击),然后考虑为该组件或您希望相同的任何其他组件使用 FocusGained 事件效果。

我希望这在某种程度上有所帮助。

已编辑!

以便处理OP的特殊情况。

【讨论】:

  • 谢谢!但是现在当我不更改 JFormattedTextField 中的值时,当我按下回车键时,光标不会跳转到下一个字段。我必须添加一个 KeyListener 还是有一个更好的解决方案?
  • Uhhhh....我明白你的意思@mavok。在这种情况下,只需将 keylistener 与 KeyPressed 事件处理程序一起使用,而不是 ActionListener。我也修改了我的答案以适应这种特殊情况。我还修改了匹配正则表达式以在条目中容纳逗号。
【解决方案2】:
String str=this.getText();
this.setText(str);
this.selectAll();

【讨论】:

    猜你喜欢
    • 2018-08-03
    • 2023-03-13
    • 1970-01-01
    • 2022-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    相关资源
    最近更新 更多