JScrollPane 无法聚焦?

发布于 2024-12-01 04:33:56 字数 1523 浏览 3 评论 0原文

我在将焦点集中到 JScrollPane 时遇到问题。我试图在创建窗口时执行此操作,以便我可以尝试正在处理的键绑定。一切都以正确的尺寸显示在它应该在的地方。我得到的看起来像这样:

import java.awt.*;
import java.swing.*;

public class MyInterface extends JFrame {
   public MyInterface() {}

   public static void main(String[] args) {
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }

   private static void createAndShowGUI() {
      // define custom table model
      JTable table = new JTable(model);
      MyScrollPane scrollPane = new MyScrollPane(table);
      MyInterface frame = new MyInterface();
      scrollPane.setPreferredSize(new Dimension(512, 512));
      frame.getContentPane().add(scrollPane);
      frame.pack();
      frame.setVisible();
      scrollPane.requestFocus();
   }
}

public class MyScrollPane extends JScrollPane implements KeyListener {
   public MyScrollPane(Component view) {
      super(view, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
   }

   // key event processing
}

我尝试向框架添加一个窗口侦听器,该侦听器会在窗口打开完成后请求滚动窗格的焦点,但它没有帮助。有什么想法吗?

编辑:我想我应该提到scrollPane.isFocusable()和scrollPane.isRequestFocusEnabled都返回true,所以我应该能够给予它焦点。

编辑:看来我无法将焦点集中在任何东西(框架、表格等)上,所以这里还有一些其他问题。无论我尝试什么,这都显示为空:

Component compFocusOwner =   KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
System.out.println("focus owner: " + compFocusOwner);

I'm having issues giving focus to a JScrollPane. I'm trying to do this when the window is created so that I can try out the key bindings I'm working on. Everything shows up where it's supposed to be and at the correct size. What I've got looks like this:

import java.awt.*;
import java.swing.*;

public class MyInterface extends JFrame {
   public MyInterface() {}

   public static void main(String[] args) {
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }

   private static void createAndShowGUI() {
      // define custom table model
      JTable table = new JTable(model);
      MyScrollPane scrollPane = new MyScrollPane(table);
      MyInterface frame = new MyInterface();
      scrollPane.setPreferredSize(new Dimension(512, 512));
      frame.getContentPane().add(scrollPane);
      frame.pack();
      frame.setVisible();
      scrollPane.requestFocus();
   }
}

public class MyScrollPane extends JScrollPane implements KeyListener {
   public MyScrollPane(Component view) {
      super(view, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
   }

   // key event processing
}

I tried adding a window listener to the frame which would request focus for scrollPane after the window was finished opening, but it didn't help. Any ideas?

EDIT: Thought I should mention that scrollPane.isFocusable() and scrollPane.isRequestFocusEnabled both return true, so I should be able to give it focus.

EDIT: It seems I'm unable to give focus to anything (frame, table, etc.), so there's some other problem here. No matter what I try, this displays null:

Component compFocusOwner =   KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
System.out.println("focus owner: " + compFocusOwner);

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

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

发布评论

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

评论(4

俯瞰星空 2024-12-08 04:33:56

我认为不可能将焦点设置为 JScrolPane,但将焦点设置为 JComponent,将其放入 JScrolPane 中不会有问题

    Runnable doRun = new Runnable() {

        @Override
        public void run() {
            myComponent.requestFocus();//requestFocusInWindow
            myComponent.grabFocus();
        }
    };
    SwingUtilities.invokeLater(doRun);

I think that not possible to set the Focus to the JScrolPane, but set Focus to the JComponent, which is place into JScrolPane couldn't be problem

    Runnable doRun = new Runnable() {

        @Override
        public void run() {
            myComponent.requestFocus();//requestFocusInWindow
            myComponent.grabFocus();
        }
    };
    SwingUtilities.invokeLater(doRun);
ま柒月 2024-12-08 04:33:56

为什么需要这个?我认为您应该将焦点放在 JScrollPane 内的组件上。如何检测焦点不存在?滚动窗格本身没有可见部分(可能只有 1 个像素帧可见)。

Why do you need this? I think you should set focus on the component inside the JScrollPane. How do you detect that the focus isn't there? Scrollpane itself has no visible part (may be just 1 pixel frame is visible).

今天小雨转甜 2024-12-08 04:33:56

如何使用键绑定中所述:如何使用键绑定工作,复合组件通常使用WHEN_ANCESTOR_OF_FOCUSED_COMPONENT映射。您甚至可以将绑定添加到顶级组件的根窗格,如 < em>按键绑定

As noted in How to Use Key Bindings: How Key Bindings Work, composite components typically use the WHEN_ANCESTOR_OF_FOCUSED_COMPONENT map. You can even add bindings to the top-level component's root pane, as shown in Key Bindings.

情绪 2024-12-08 04:33:56

我不知道问题出在哪里;由于缺乏更好的想法,我删除并重新输入了所有内容,现在它可以工作了。如果任何阅读本文的人愿意的话,可以将焦点集中在 JScrollPane 上。我认为这是可能的,因为默认情况下 .isFocusable() 和 isRequestFocusEnabled() 都设置为 true。但是,是的,我不知道;至少我得到了一些很好的按键绑定链接。感谢您的帮助!

I've no clue what the problem was; for lack of a better idea I deleted and retyped everything, and now it works. It is possible to focus a JScrollPane, in case anyone reading this wishes to. I assumed it was possible, since by default both .isFocusable() and isRequestFocusEnabled() are set to true. But yeah, I have no idea; at the very least I got a few good links for key binding. Thanks for the help!

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