简体   繁体   English

格式化文本时 JFormattedTextfield.selectAll() 不起作用

[英]JFormattedTextfield.selectAll() does not work when formatting the text

I have a lot of different JFormattedTextFields with action and keylisteners.我有很多不同的 JFormattedTextFields 带有动作和键监听器。 Every Field has a keylistener, so when I press enter I will focus the next JFormattedTextField.每个字段都有一个 keylistener,所以当我按下回车键时,我将关注下一个 JFormattedTextField。 The Problem is, for some JFormattedTextFields my code is formatting the input and then sets the text new and for those selectAll() does not work.问题是,对于某些 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();
            }
        }
    });

The function tausenderPunkt(): function tausenderPunkt():

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

So when my cursor is in field a and i press enter the cursor goes to field b but does not select the text or values.因此,当我的 cursor 在字段 a 中并且我按输入 cursor 进入字段 b 但没有 select 文本或值时。 When i do not use setText() i do not have the problem.当我不使用 setText() 时,我没有问题。 Somebody has a solution?有人有解决方案吗?

Edit: For some JFormattedTextFields the solution was to add selectAll() to the keyAdapter, but not for all.编辑:对于一些 JFormattedTextFields,解决方案是将 selectAll() 添加到 keyAdapter,但不是全部。 For example:例如:

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

Edit2: The problem seems to be when i create the JFormattedTextFields. Edit2:问题似乎出在我创建 JFormattedTextFields 时。 When i do not create them with a value in the constructor it works.当我不在构造函数中使用值创建它们时,它可以工作。 But i have to do.但我必须这样做。

Before moving to your next text field you should consider handling all the required conditions for the text field you are currently focused on and this would of course include the formatting of values or text supplied to that field. 在移到下一个文本字段之前,您应该考虑处理当前关注的文本字段的所有必要条件,这当然包括为该字段提供的值或文本的格式。 Once all the desired conditions are met then move on to the next text field. 满足所有所需条件后,请继续下一个文本字段。

In reality this can all be accomplished through the keyPressed event for your particular situation. 实际上,这可以通过针对特定情况的keyPressed事件来完成。 There is no need for the actionPerformed event on any of your text fields, for example: 您的任何文本字段上都不需要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  -------------

Here is a simple method so as to eliminate the need for repetitious code: 这是一种简单的方法,可以消除对重复代码的需求:

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();
    }
}

If you want the contents of your first text field to be highlighted as soon as focus has been established upon it (tabbed to or clicked on) then consider using a FocusGained event for that component or any other component where you desire the same effect. 如果希望在焦点建立(选中或单击)后立即突出显示第一个文本字段的内容,则可以考虑对该组件或需要相同效果的任何其他组件使用FocusGained事件。

I Hope this has helped in some way. 我希望这可以有所帮助。

EDITED! EDITED!

So as to handle OP's particular situation. 从而处理OP的特殊情况。

String str=this.getText();
this.setText(str);
this.selectAll();

You can get the focus owner and remove the focusable feature:您可以获取焦点所有者并删除可聚焦功能:

Component focusOwner = FocusManager.getCurrentManager().getFocusOwner();

When you get the component, put this sentence after load it:拿到组件的时候,在load之后放这句话:

component.setFocusable(false);

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

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