当组件未聚焦时阻止 FocusEvents

发布于 2024-11-08 04:34:23 字数 2330 浏览 0 评论 0原文

我有以下示例代码:

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class SampleFocus extends JFrame {

  public SampleFocus(String titel) {
    setTitle(titel);
    JTextField txtField1 = new JTextField("default-click");
    JTextField txtField2 = new JTextField("alternative-Text");
    JTextField txtField3 = new JTextField("own diaolog textfield");

    JTextArea dummyLabel = new JTextArea(10, 20);
    dummyLabel.setText("empty textarea, which is focusable");

    setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
    add(txtField1);
    add(dummyLabel);
    add(txtField2);

    JDialog altDialog = new JDialog(this);
    altDialog.add(txtField3);
    altDialog.setVisible(true);
    altDialog.pack();

    FocusAdapter myFocusListner = new FocusAdapter() {

      @Override
      public void focusGained(FocusEvent e) {
        if (e.getComponent() instanceof JTextField) {
          System.out.println("gained for TextField: "
                             + ((JTextField) e.getComponent()).getText());
        } else {
          System.out.println("gained for component: " + e.getComponent());
        }
      }
    };

    txtField1.addFocusListener(myFocusListner);
    txtField2.addFocusListener(myFocusListner);
    txtField3.addFocusListener(myFocusListner);

    // dummyLabel.addFocusListener(myFocusListner);
  }

  public static void main(String[] args) {
    JFrame frame = new SampleFocus("FocusListener  - sample");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

  }
}

当我从框架切换到对话框时,我得到了正确的事件。当我切换回框架时,我也得到了正确的 FocusEvents。我的问题是,当我切换回来时,我也会得到我不感兴趣的组件的 FocusEvents。

例如,

选择“默认单击”==>对话框/文本字段==> Frame/'empty textarea'

结果:尽管组件没有焦点,但我得到了“默认单击”的 FocusGained-Event。

期望的结果:

  • 组件“default-click”未获取 FocusEvent 或

  • 区分组件是否确实正确接收了事件(例如,我也可以单击进入)

我发现的解决方法:

附加到 JTextArea 以及 FocusListener。问题是,这意味着我需要将侦听器附加到我的所有组件。这几乎是不可能的。有什么想法吗?

有什么想法如何获得结果吗?

谢谢LeO

I have the following sample-code:

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class SampleFocus extends JFrame {

  public SampleFocus(String titel) {
    setTitle(titel);
    JTextField txtField1 = new JTextField("default-click");
    JTextField txtField2 = new JTextField("alternative-Text");
    JTextField txtField3 = new JTextField("own diaolog textfield");

    JTextArea dummyLabel = new JTextArea(10, 20);
    dummyLabel.setText("empty textarea, which is focusable");

    setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
    add(txtField1);
    add(dummyLabel);
    add(txtField2);

    JDialog altDialog = new JDialog(this);
    altDialog.add(txtField3);
    altDialog.setVisible(true);
    altDialog.pack();

    FocusAdapter myFocusListner = new FocusAdapter() {

      @Override
      public void focusGained(FocusEvent e) {
        if (e.getComponent() instanceof JTextField) {
          System.out.println("gained for TextField: "
                             + ((JTextField) e.getComponent()).getText());
        } else {
          System.out.println("gained for component: " + e.getComponent());
        }
      }
    };

    txtField1.addFocusListener(myFocusListner);
    txtField2.addFocusListener(myFocusListner);
    txtField3.addFocusListener(myFocusListner);

    // dummyLabel.addFocusListener(myFocusListner);
  }

  public static void main(String[] args) {
    JFrame frame = new SampleFocus("FocusListener  - sample");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

  }
}

When I switch from the Frame to the dialog I get the proper event. When I switch back to the Frame I get as well the proper FocusEvents. My problem is, that when I switch back, I get as well the FocusEvents for components which I am not interested in.

e.g.

Select 'default-click' ==> Dialog/Textfield ==> Frame/'empty textarea'

Result: I get a FocusGained-Event for 'default-click' although the component has NOT the focus.

Desired Result: Either

  • Component 'default-click' does not get the FocusEvent OR

  • distinguish if the component really received the Event properly (e.g. I could have clicked into it as well)

Workaround I found:

Attach to the JTextArea as well a FocusListener. Problem is, that this would mean, I need to attach to ALLLL of my components a Listener. Which is hardly possible. Any ideas?

Any ideas how to get the result?

Thx LeO

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

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

发布评论

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

评论(2

流绪微梦 2024-11-15 04:34:23

它按预期工作。
您选择“默认单击”。它获得焦点。
您选择对话框。框架和“默认单击”失去焦点。
您选择文本区域。现在,事情变得有趣了。对话框失去焦点。框架获得焦点。 “default-click”也会获得焦点,因为当 Frame 处于活动状态时他就拥有了焦点。然后“默认单击”失去焦点,文本区域获得焦点(因为您单击了文本区域)。

尝试结合 focusGained 和 focusLost 事件。

或者告诉我更多你想要实现的目标,也许我可以提供帮助。

Its working as intended.
You select "default-click". It gains focus.
You select Dialog. Frame and "default-click" loses focus.
You select Text area. Now, things becomes interesting. Dialog loses focus. Frame gets focus. "default-click" also gains focus, because he had it when Frame was active. And then "default-click" loses focus, textarea gains focus (because you clicked on text area).

Try combine focusGained and focusLost events.

Or tell me more what are you trying to accomplish, maybe I can help.

樱娆 2024-11-15 04:34:23

我找到解决方法的方法是检查 focusGained 方法,当前组件是否在自己的 JDialog 中,以及前一个组件是否是 JFrame 中的 JTextField。如果是这样,那么我会记住获得的焦点组件,将焦点更改为另一个组件,当我收到 focusLost 时,我会请求将其返回到之前记住的组件。

某种黑客,但它有效......如果有任何更好的想法,我感兴趣......

The way I found the workaround was to check in the focusGained-method if the current component is in an own JDialog and if previous component was a JTextField in a JFrame. If so, then I memorize the gained focused component, change the focus to another component and when I receive a focusLost I request it back to the previously memorized component.

Some kind of hack, but it works.... If there are any better ideas, I am interested in ...

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