获得焦点和失去焦点事件

发布于 2024-12-21 14:14:46 字数 182 浏览 3 评论 0原文

我的 java swing 表单中有 4 个 JTextfield。问题是我需要通过 java 代码而不是使用 Tab 键将焦点从一个 JTextField 移动到另一个 JTextField。

如果JTextField2获得了Focus,则表示需要选择JTextField2中的内容。我不知道该怎么做,请将您正确的代码与此问题相关联

I have 4 JTextfields in my java swing form. The problem is I need to move the Focus from one JTextField to other through java code not by using tab key.

If the Focus gained by JTextField2 means the content in the JTextField2 need to be selected. I don't know how to do this plz put your proper code associate with this issue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯然#的苍凉 2024-12-28 14:14:46

您可以调用 requestFocusInWindow () 代表您想要焦点的文本字段。

You can call requestFocusInWindow() for the textfield you want focus.

英雄似剑 2024-12-28 14:14:46

这可能有点复杂,

您必须包装并延迟您的 ActionActionListenerinvokeLater(),并放入其中(最安全的方法是设置以下代码行)

  • JTextField2.setText(JTextField2.getText);

  • JTextField2.selectAll( );

编辑至@Andrew Thompson

private FocusListener fcsListener = new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            dumpInfo(e);
        }

        @Override
        public void focusLost(FocusEvent e) {
            //dumpInfo(e);
        }

        private void dumpInfo(FocusEvent e) {
            System.out.println("Source  : " + name(e.getComponent()));
            System.out.println("Opposite : " + name(e.getOppositeComponent()));
            System.out.println("Temporary: " + e.isTemporary());
            Component c = e.getComponent();//works for editable JComboBox too
            if (c instanceof JFormattedTextField) {
                ((JFormattedTextField) c).selectAll();
            } else if (c instanceof JTextField) {
                ((JTextField) c).selectAll();
            }//both methods not correct required setText(getText()) inside invokeLater
        }

        private String name(Component c) {
            return (c == null) ? null : c.getName();
        }
    };

that could be little bit complicated

you have to wrap and delay your Action or ActionListener into invokeLater(), and put inside (most safiest way is to set there follows code lines)

  • JTextField2.setText(JTextField2.getText);

and

  • JTextField2.selectAll();

edit to @Andrew Thompson

private FocusListener fcsListener = new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            dumpInfo(e);
        }

        @Override
        public void focusLost(FocusEvent e) {
            //dumpInfo(e);
        }

        private void dumpInfo(FocusEvent e) {
            System.out.println("Source  : " + name(e.getComponent()));
            System.out.println("Opposite : " + name(e.getOppositeComponent()));
            System.out.println("Temporary: " + e.isTemporary());
            Component c = e.getComponent();//works for editable JComboBox too
            if (c instanceof JFormattedTextField) {
                ((JFormattedTextField) c).selectAll();
            } else if (c instanceof JTextField) {
                ((JTextField) c).selectAll();
            }//both methods not correct required setText(getText()) inside invokeLater
        }

        private String name(Component c) {
            return (c == null) ? null : c.getName();
        }
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文