Disabling JPanel children

Suppose I have a hierarchy like this:

JPanel panel1; JCheckBox cb1; JCheckBox cb2; JRadioButton rb1; JRadioButton rb2; ... 

I have a condition where I want separate groups of controls inside the panel to be enabled / disabled. It works great. (for example, turn on cb1 and cb2 when one condition is true, turn them off when it is false.)

I would like to disable and enable the entire panel again. If I call panel1.setEnabled(false) , this does not work, it disables the panel, but does not affect its children.

If I list the children of the panel and call setEnabled(false) for each of them, this will work, but then I will have to keep the child state on when the panel is turned on again.

Is there an easier way?

+4
source share
4 answers

You can place a glass panel over the panel to capture events.

http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html

+2
source

If I list the children of the panel and call setEnabled (false) for each of them, this will work, but then I will need to keep the child activated state when I turn on the panel again.

The disconnected panel has a solution for this approach, as well as an approach to glass glass with a β€œcontainer”.

+2
source

JXLayer is a great way to achieve this. It can be used for any Swing component.

+1
source
 //una forma de recorrer todos los elementos dentro de un jpanel Component[] components = jPanelX.getComponents(); for (int i = 0; i < components.length; i++) { if(components[i].getClass().getName().toString().equals("javax.swing.JTextField")){ components[i].setEnabled(false); } } 
0
source

Source: https://habr.com/ru/post/1332772/


All Articles