如何在Swing中禁用容器及其子代 [英] How to disable a container and its children in Swing

查看:93
本文介绍了如何在Swing中禁用容器及其子代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到一种在Swing中禁用容器及其子代的方法。 Swing真的缺少这个基本功能吗?

I cannot figure out a way to disable a container AND its children in Swing. Is Swing really missing this basic feature?

如果我对容器执行setEnabled(false),它的子代仍然处于启用状态。

If I do setEnabled(false) on a container, its children are still enabled.

我的GUI结构非常复杂,并且无法对容器下的所有元素进行转换。容器顶部的GlassPane也不是(容器不是整个窗口)。

My GUI structure is pretty complex, and doing a traversion of all elements below the container is not an option. Neither is a GlassPane on top of the container (the container is not the entire window).

推荐答案

添加到 mmyers的答案,禁用儿童并非易事(请参阅此主题

To add to mmyers's answer, disabling children is not an easy task (see this thread)


问题接近于在一般情况下无法解决。这就是为什么它不是核心Swing的一部分。

The problem is near-to unsolvable in the general case. That's why it is not part of core Swing.

从技术上讲,禁用和存储旧状态后跟启用 - 恢复 - 旧 - 国家可能看起来有吸引在特殊情况下,它甚至可能是一件好事。但是有两个问题(至少可能有更多)。

Technically, the disable-and-store-old-state followed by a enable-and-restore-to-old-state might look attractive. It even might be a nice-to-have in special cases. But there are (at least, probably a bunch more) two issues with that.

复合组件

递归必须停在复合组件(或单个实体)上。然后组件负责保持依赖状态。没有通用的方法来检测这样的组件 - 例如JComboBox,JXDatePicker(与相关的问题

The recursion must stop on a "compound component" (or "single entity"). Then the component is responsible for keeping dependent's state. There's no general way to detect such a component - examples are JComboBox, JXDatePicker (which as related issue)

为了使事情变得更加复杂,依赖者不需要在复合组件的层次结构下,fi JXTable负责处理ColumnControl(和头部)的启用状态。

To make things even more complicated, dependents don't need to be under the hierarchy of the "compound component", f.i. JXTable takes care of the ColumnControl's (and header's) enabled state.

尝试解决这两个问题需要

Trying to tackle both would require to have

a)该化合物上的财产:不要触摸我的孩子和b $ bb非受限家属的财产:别碰我

a) a property on the compound: "don't touch my children" and
b) a property on the uncontained dependents: "don't touch me"

绑定到已启用

如果已启用状态已绑定到,则启用并更新到旧可能会中断应用程序状态一个(演示或其他)模型属性,并且该属性在此期间发生了变化 - 现在旧状态无效。

enable-and-update-to-old might break application state if the enabled status is bound to a (presentation or other) model property and that property changed in-the-meantime - now the old-state is invalid.

尝试解决这个问题需要

c)真实的存储旧的启用应对视图关注属性

d)将表示模型属性绑定到启用并且存储旧的启用

c) a "real" stored-old-enabled-due-to-view-concerns property
d) bind the presentation model property to both the enabled and the stored-old-enabled

JXRadioGroup有一个问题的变体:在禁用时 - 组本身或通用控制器 - 跟踪旧的启用每一个b utton。按钮的启用由Action控制 - 如果有Action。因此,启用的控制器需要恢复到启用旧功能或启用操作。在组的禁用(as-group)期间,如果Action的启用在存储时为false并且更改为true,则会出现问题。
另一个如果添加了动作。

JXRadioGroup has a variant of that problem: On disabling - the group itself or the general controller - keeps track of the old-enabled of every button. Button's enabled is controlled by the Action - if there is an Action. So the enabled controller needs to restore to old-enabled or to action's enabled. During group's disabled (as-group) a problem looms if the Action's enabled was false on storing and changed to true. Another if actions are added.

现在想象一下重载时状态转换的复杂性a) - d)

Now imagine the complexity of state transitions when overloading a)-- d)

这篇关于如何在Swing中禁用容器及其子代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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