How to disable a container and its children in Swing

Fuzzy76 :

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

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

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).

VonC :

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

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.

Compound components

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)

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) a property on the compound: "don't touch my children" and
b) a property on the uncontained dependents: "don't touch me"

Binding to enabled

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.

Trying to tackle that would require to have

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 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.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Disable contextmenu for a specific container and its children

dragula - how to disable dragging in its parent container

How to disable the delivery of mouse events to the widget but not its children in Qt?

How to set the container width calculating the width of if its children

How to specify the dimensions of a container as the total dimensions of its children in React Native?

Make flex container width grow as its children

Flex container not getting full width of its children

CSS Grid container not taking the width of its children

How to make a container's size fits its `position: absolute;` children's size

Java Swing - How to disable a JPanel?

flex row should be as wide as its children in an overflow-x container

Calculating <article> Height Based on the Number of its Container children()

Using a flex container, how can I give a child image its full width while right aligning all of the flex children?

How to disable/enable all children on LinearLayout in Android

How to disable NodeTree (parents and children) in primeng?

Jetpack Compose: how to disable gesture detection on children

how to disable one button but not all in Java Swing?

How to disable javax.swing.JButton in java?

How to temporarily disable event listeners in Swing?

Swing shows empty tooltips, how to disable this?

Give container ownership of its children, but have children store a reference to their parent using smart pointers

How to duplicate a node and its children in Karate

How a const widget works with its children?

jQuery - How to hide an element and its children?

How to flip a JFrame and its children upside down?

How to remove a div without removing its children?

How to solely upgrade an element and all its children

MSSQL: How to join only if its children not exist

How to remove text in an element but show its children