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

时间:2011-04-28 17:20:36

标签: java swing highlight jformattedtextfield

我一直试图在没有运气的情况下让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;

非常感谢任何帮助!

5 个答案:

答案 0 :(得分:1)

尝试以下代码

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

答案 1 :(得分:1)

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

你有没有试过这样的事情:

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

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

基本上你可以使用这段代码(不确定每个格式化程序和输入掩码),但是对于Number,Date和String你可以使用以下,同时确保这个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 ......

答案 4 :(得分:1)

也许你有EDT的问题,

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

也可以使用JTextField,JFormateddTextField和JComboBox,还可以使用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();
    }
};