【问题标题】:Unable to get JFormattedTextField to highlight on mouse click focus event无法让 JFormattedTextField 在鼠标单击焦点事件上突出显示
【发布时间】:2011-08-14 22:02:57
【问题描述】:

我一直在尝试让 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();
        }
    }
}

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

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;

任何帮助将不胜感激!

【问题讨论】:

  • 添加一个 focusListener 并在 focusGained 中调用 selectAll 是可行的解决方案 - 通常 :-) 如果它不在您的上下文中,则需要一个演示问题的小型可运行示例来跟踪区别。
  • 顺便说一句:无需硬编码对 textField 的引用 - 事件有一个方法 getComponent 来获取其发送者

标签: java swing highlight jformattedtextfield


【解决方案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();
    }
};

【讨论】:

  • 不客气,但请注意:一定要避免混合 FocusListener 和另一个 Listeners,例如 JComboBox 是 FocusListener 和 ItemListener 死锁组合
【解决方案2】:

试试下面的代码

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

【讨论】:

    【解决方案3】:

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

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

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

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

    【讨论】:

    • 那也行不通。如果我单击并按住大约一秒钟左右,它仍然会突出显示。
    • 如果不查看代码的其余部分,就很难看出事情在哪里陷入困境。我最好的猜测是您正在通过侦听器引入一个复杂的循环。向您的代码添加一些调试打印,尤其是对您的侦听器,并查看打印语句爆炸发生的位置。
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      基本上您可以使用此代码(不确定是否适用于每个格式化程序和输入掩码),但对于 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...

      【讨论】:

      • 没有人知道您添加到 JTextField 的什么/哪些侦听器,并且没有办法通过使用常规 JFormattedTextField 来更改它,因为对于今天的 Java6,此代码看起来像 Sado - *M...,嗯,也许我会发布另一个课程,您将尝试将其传递给您的 aka JTextField 代码
      • 并且您可以即时添加DocumentListener download.oracle.com/javase/tutorial/uiswing/events/… 或者如果需要,那么您的FocusListener 应该看起来像myText.removeDocumentLister(myDocumentListener) 然后任何用于高亮JTextFields 内容的woodoo 然后添加DocumentListener
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多