Java阻塞焦点从JComponent [英] Java blocking focus from JComponent

查看:119
本文介绍了Java阻塞焦点从JComponent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的应用程序做了一个gui。 JFrame有2个JPanels,panel1& panel2。 panel1是一个JPanel,自定义绘画每5ms重新绘制一次。



panel2是我第一次尝试CardLayout实现:它包含JPanels subPanel1& subPanel2。 subPanel1包含一个JComboBox并被添加到panel2: panel2.add(subPanel1);



code> .setLayout(new CardLayout()); 命令,我添加3个新的JPanels到它,与适当的itemListener和所有。当然我也添加它: panel2.add(subPanel2);



现在问题: Java。我知道方法 setFocusable(boolean) requestFocus()。但我不能让他们以任何逻辑的方式行事。



首先,它们的根问题:当组合框获得焦点时,我不能将它全部聚焦(尝试用光标在任何地方单击)。



以下是我进行的实验:



1)没有任何代码在整个应用程序中说话combobox开始



2)如果我设置 panel1.setFocusable(true); (在其构造函数中)它将以焦点开始



3)如果我设置 panel1.setFocusable 并且也请求焦点,它不会得到它。 (只有正常工作的东西)



4)如果我设置panel2,subPanel1或subPanel2单独或任何组合无法聚焦,他们仍然可以接收焦点(组合框,也就是说,这是唯一能够注册焦点的组件)。



5)如果我设置combobox unfocusable,我仍然能够在卡之间滚动CardLayout与框'itemListener,但焦点不坚持它。事实上panel1仍然注册键盘输入



所以我真的很困惑整个焦点的事情。也许这不是我想象的吗?我想做的是完全阻止所有与panel2的交互,直到一个标志(每5毫秒评估)是真的。我是否正确,假设不像JPanels,JComboBox自动有一个mousebuttonListener点击时获得焦点?如果没有,那么如何完全禁用JComboBox和当前卡显示的所有组件?
一个不可聚焦组件中的组件是否仍然可以聚焦的正常行为?

解决方案

这听起来像你真正想要的要使用的是 .setEnabled(false)



如果您需要将面板中的所有组件设置为禁用,使用这样的方法:(可能不是 JComponents 的最佳方法,但是如果需要可以很容易地修改,但是这样做)

  public static void setContainerAndChildrenEnabled(Container c,boolean b)
{
Component [] allComps = c.getComponents
for(Component com:allComps)
{
com.setEnabled(b);
if(com instanceof Container)
setContainerAndChildrenEnabled((Container)com,b);
}
}

然后使用您要设置的面板和true或false以启用/禁用。这也将递归调用中的每个组件 $ c>



从文件两点要注意:



  1. 注意:禁用轻量级组件不会阻止它接收MouseEvents。


  2. 这个容器从接收任何输入事件。但是禁用轻量级容器只会影响此容器。



http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#isLightweight%28%29 =nofollow> isLightweight()


I've made a gui for my application. The JFrame has 2 JPanels, panel1 & panel2. panel1 is just that, a JPanel with a custom painting that repaints itself every 5 ms.

panel2 is my first attempt at a CardLayout implementation: it contains JPanels subPanel1 & subPanel2. subPanel1 contains a JComboBox and is added to panel2: panel2.add(subPanel1);.

subPanel2 has the .setLayout(new CardLayout()); command, and I add 3 new JPanels into it, with appropiate itemListener and all. Of course I also add it: panel2.add(subPanel2);

Now to the problem: focusing components in Java. I have knowledge of methods setFocusable(boolean) and requestFocus(). But I can't make them behave in any logical way.

First, the root problem of them all: When the combobox gets the focus, I can't unfocus it at all (tried clicking everywhere with cursor).

Following are experiments I've conducted:

1) without any code speaking to focus throughout the application the combobox starts with the focus, no matter which order panel1 and panel2 are added to the JFrame.

2) if I set panel1.setFocusable(true); (in its constructor) it will start with the focus

3) if I set panel1.setFocusable(false); and also request focus to it, it doesn't get it. (only thing that works as expected)

4) if I set panel2, subPanel1, or subPanel2 unfocusable individually or in any combination, they can still receive focus (the combobox, that is, which is the only component able to register focus).

5) if I set the combobox unfocusable, I'm still able to scroll between the cards in the CardLayout with the box' itemListener, but the focus doesn't stick to it. In fact panel1 still registers keyboard-inputs

So really I am very confused about the whole 'focus' thing. Maybe it is not what I assume it is? What I am trying to do is entirely block all interaction with panel2 untill a flag (which is evaluated every 5 ms) is true. Am I correct to assume that unlike JPanels, the JComboBox automatically has a mousebuttonListener to gain focus when clicked? if no, then how do I completely disable the JComboBox and all components the current card is displaying? Is it normal behavior that components within an unfocusable component are still focusable?

解决方案

It sounds like what you really want to use is .setEnabled(false)

If you need to set all the components in a panel to be disabled then you can use a method like this to do it: (probably not the best method for JComponents but can be easily modified if required, but this does work)

public static void setContainerAndChildrenEnabled(Container c, boolean b)
{
    Component[] allComps = c.getComponents();
    for(Component com : allComps)
    {
        com.setEnabled(b);
        if(com instanceof Container)
            setContainerAndChildrenEnabled((Container) com, b);
    }
}

Then call it with the panel you want to set for and true or false to enable/disable. This will also recursively call setEnabled() for every Component within a Container

From the docs two points to note:

  1. Note: Disabling a lightweight component does not prevent it from receiving MouseEvents.

  2. Note: Disabling a heavyweight container prevents all components in this container from receiving any input events. But disabling a lightweight container affects only this container.

See isLightweight()

这篇关于Java阻塞焦点从JComponent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆