如何在点击/聚焦时在JFormattedTextField的末尾设置插入位置?

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

我有JFormattedTextField的框架。我的简化代码可能如下所示:

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100,100);
frame.setLayout(new GridLayout(2,2));

JFormattedTextField field1 = new JFormattedTextField(NumberFormat.getInstance());
field1.setValue(0.4);
frame.add(new JLabel("value A"));
frame.add(field1);

JFormattedTextField field2 = new JFormattedTextField(NumberFormat.getInstance());
field2.setValue(0.8);
frame.add(new JLabel("value B"));
frame.add(field2);

frame.setVisible(true);

产生:

enter image description here

目标

当我单击/关注任何JFormattedTextField时,我希望它自动将插入符号放在最后

enter image description here enter image description here

问题

我在调用frame.setVisible(true);之前尝试使用以下解决方案,但它们似乎都没有用

java swing caret jformattedtextfield
1个回答
2
投票

为我工作没有问题....

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            JTextField textField = new JTextField("This is a test");
            add(textField, gbc);

            JButton button = new JButton("This is a button");
            add(button, gbc);
            button.setFocusable(false);
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (textField.getCaretPosition() != 0) {
                        textField.setCaretPosition(0);
                    } else {
                        textField.setCaretPosition(textField.getText().length());
                    }
                    textField.requestFocusInWindow();
                }
            });
        }

    }

}

如果您仍有问题,请提供runnable example which doesn't work

Update with JFormattedTextField....

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            JFormattedTextField textField = new JFormattedTextField("This is a test");
            textField.setValue(0.8d);
            add(textField, gbc);

            JButton button = new JButton("This is a button");
            add(button, gbc);
            button.setFocusable(false);
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (textField.getCaretPosition() != 0) {
                        textField.setCaretPosition(0);
                    } else {
                        textField.setCaretPosition(textField.getText().length());
                    }
                    textField.requestFocusInWindow();
                }
            });
        }

    }

}

Updated with "set at beginning"

好吧,我只想指出我个人不喜欢JFormattedTextField,它有时会做很多“事情”并不总是有意义的。

我使用的“旧”技巧,在实现“自动选择所有焦点增益”时,将请求卸载到事件调度线程的末尾,这将请求放在JFormattedTextField的所有“时髦的东西”之后当场地变得集中时......

JFormattedTextField textField = new JFormattedTextField("This is a test");
textField.setValue(0.8d);
add(textField, gbc);
textField.addFocusListener(new FocusAdapter() {
    @Override
    public void focusGained(FocusEvent arg0) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                textField.setCaretPosition(textField.getText().length());
            }
        });
    }
});

是的,我很认真......

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