无法让 JFormattedTextField 在鼠标单击焦点事件上突出显示

发布于 2024-11-03 14:53:46 字数 2511 浏览 4 评论 0原文

我一直在尝试让 JFormattedTextField 在鼠标单击时突出显示,但没有成功。我已经能够在浏览字段时使其正常工作,但是我想在单击时突出显示所有内容。

如果我在文本字段上单击并按住约 1.5-2 秒,我只能在鼠标单击时突出显示;我不知道为什么。

我搜索并尝试了一些修复,包括扩展类;

class HFTextField extends JFormattedTextField
{
    HFTextField(MaskFormatter formatter)
    {
        super(formatter);
    }

    @Override
    protected void processFocusEvent(FocusEvent e)
    {
        super.processFocusEvent(e);
        if (e.getID() == FocusEvent.FOCUS_GAINED)
        {
            this.selectAll();
        }
    }
}

我还定义了一个(相当冗长!)FocusListener,它使用 SwingUtilities.invokelater;

public static FocusListener CreateHighlightTextFieldFocusListener(final JTextField text_field)
    {
        FocusListener fl =
                new FocusAdapter()
                {
                    public void focusGained(FocusEvent evt)
                    {
                        SwingUtilities.invokeLater(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                text_field.selectAll();
                            }
                        });
                    }
                };

        return fl;
    }

这是创建格式化文本字段的函数;

public static JTextField CreateFormattedTextField(int x, int y, int width, int height,
                            Method action_method, Method changed_method, Method remove_method,
                            Method update_method, String mask_formatter, String banned_chars)
    {
        MaskFormatter formatter = null;

        try {

            formatter = new MaskFormatter(mask_formatter);

        } catch (ParseException e) {
            assert(false);
        }

        if(banned_chars != null)
            formatter.setInvalidCharacters(banned_chars);

        JTextField text_field = new HFTextField(formatter);

        text_field.setBounds(x, y, width, height);

        if(action_method != null)
        {
            text_field.addActionListener(CreateTextFieldActionListener(action_method, text_field));
        }

        text_field.getDocument().addDocumentListener(
                CreateTextFieldDocumentListener(changed_method, remove_method,
                                                update_method, text_field));

        text_field.addFocusListener(CreateHighlightTextFieldFocusListener(text_field));

        return text_field;

任何帮助将不胜感激!

I have been trying with no luck to get a JFormattedTextField to highlight on mouse click. I have been able to get it to work fine while tabbing through fields, however I would like to highlight everything on clicking.

I am only able to highlight on mouse click if I click and hold for about 1.5-2 seconds on the text field; I have no idea why.

I've searched and tried a few fixes including extending the class;

class HFTextField extends JFormattedTextField
{
    HFTextField(MaskFormatter formatter)
    {
        super(formatter);
    }

    @Override
    protected void processFocusEvent(FocusEvent e)
    {
        super.processFocusEvent(e);
        if (e.getID() == FocusEvent.FOCUS_GAINED)
        {
            this.selectAll();
        }
    }
}

I am also defining a (rather verbose!) FocusListener which uses SwingUtilities.invokelater;

public static FocusListener CreateHighlightTextFieldFocusListener(final JTextField text_field)
    {
        FocusListener fl =
                new FocusAdapter()
                {
                    public void focusGained(FocusEvent evt)
                    {
                        SwingUtilities.invokeLater(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                text_field.selectAll();
                            }
                        });
                    }
                };

        return fl;
    }

and this is the function that creates formatted text fields;

public static JTextField CreateFormattedTextField(int x, int y, int width, int height,
                            Method action_method, Method changed_method, Method remove_method,
                            Method update_method, String mask_formatter, String banned_chars)
    {
        MaskFormatter formatter = null;

        try {

            formatter = new MaskFormatter(mask_formatter);

        } catch (ParseException e) {
            assert(false);
        }

        if(banned_chars != null)
            formatter.setInvalidCharacters(banned_chars);

        JTextField text_field = new HFTextField(formatter);

        text_field.setBounds(x, y, width, height);

        if(action_method != null)
        {
            text_field.addActionListener(CreateTextFieldActionListener(action_method, text_field));
        }

        text_field.getDocument().addDocumentListener(
                CreateTextFieldDocumentListener(changed_method, remove_method,
                                                update_method, text_field));

        text_field.addFocusListener(CreateHighlightTextFieldFocusListener(text_field));

        return text_field;

Any help would be greatly appreciated!

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

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

发布评论

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

评论(5

冰火雁神 2024-11-10 14:53:46

也许您在 EDT 方面遇到问题,

如何使用 JTextField 的方法/如何为 JTextField 添加值

与 JTextField、JFormateddTextField、JComboBox 以及 AutoCompleted 功能一起使用 http://www.java2s.com/Code/Java/Swing-JFC/AutocompleteTextField.htm

   private FocusListener focsListener = 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();
        if (c instanceof JFormattedTextField) {
            ((JFormattedTextField) c).requestFocus();
            ((JFormattedTextField) c).setText(((JFormattedTextField) c).getText());
            ((JFormattedTextField) c).selectAll();
        } else if (c instanceof JTextField) {
            ((JTextField) c).requestFocus();
            ((JTextField) c).setText(((JTextField) c).getText());
            ((JTextField) c).selectAll();
        }
    }

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

maybe you have got problems with EDT,

how method you use for/how you added value to JTextField

works with JTextField, JFormateddTextField, with JComboBox too, and with AutoCompleted funcionalies http://www.java2s.com/Code/Java/Swing-JFC/AutocompleteTextField.htm

   private FocusListener focsListener = 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();
        if (c instanceof JFormattedTextField) {
            ((JFormattedTextField) c).requestFocus();
            ((JFormattedTextField) c).setText(((JFormattedTextField) c).getText());
            ((JFormattedTextField) c).selectAll();
        } else if (c instanceof JTextField) {
            ((JTextField) c).requestFocus();
            ((JTextField) c).setText(((JTextField) c).getText());
            ((JTextField) c).selectAll();
        }
    }

    private String name(Component c) {
        return (c == null) ? null : c.getName();
    }
};
只是我以为 2024-11-10 14:53:46

尝试下面的代码

 yourTextField.addFocusListener(new java.awt.event.FocusAdapter() {
                public void focusGained(java.awt.event.FocusEvent evt) {
                    SwingUtilities.invokeLater( new Runnable() {
                                    @Override
                                    public void run() {
                                            yourTextField.selectAll();              
                                    }
                            });
                }
            });

Try the following code

 yourTextField.addFocusListener(new java.awt.event.FocusAdapter() {
                public void focusGained(java.awt.event.FocusEvent evt) {
                    SwingUtilities.invokeLater( new Runnable() {
                                    @Override
                                    public void run() {
                                            yourTextField.selectAll();              
                                    }
                            });
                }
            });
小巷里的女流氓 2024-11-10 14:53:46

我不想给出一个简单的答案,但是您是否尝试过使用 MouseListener 接口(或 MouseAdapter 类)?

您是否尝试过这样的事情:

fieldName.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            JTextComponent text = (JTextComponent) e.getSource();
            text.selectAll();
        }      
    });

另外,我不建议异步执行此操作。

I hate to give a simple answer, but have you tried using the MouseListener interface (or MouseAdapter class)?

Have you tried something like this:

fieldName.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            JTextComponent text = (JTextComponent) e.getSource();
            text.selectAll();
        }      
    });

Also, I would not recommend doing this asynchronously.

顾挽 2024-11-10 14:53:46

如果您想要鼠标单击的特殊行为,请将 MouseAdapter 添加到 JTextFiled,并在 mouseClicked 事件处理程序中显式更改背景。

If you want specialized behavior for a mouse click, then add a MouseAdapter to your JTextFiled, and in the mouseClicked event handler, explicitly alter the background.

花辞树 2024-11-10 14:53:46

基本上你可以使用这个代码(不确定对于每个格式化程序和输入掩码),但是对于数字,日期和字符串你可以使用以下代码,确保这个 JFormattedTextField 没有实现 AutoCompleted

    myTextField.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            myTextField.requestFocus();
            myTextField.setText(myTextField.getText());
            myTextField.selectAll();
        }

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

确保你可以将其打包到 InvokeLate...

basically you can use this code (not sure that for each formatter and input masks), but for Number, Date and String you can use following, with ensure that this JFormattedTextField doesn't implements AutoCompleted

    myTextField.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            myTextField.requestFocus();
            myTextField.setText(myTextField.getText());
            myTextField.selectAll();
        }

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

sure you can pack that into InvokeLate...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文