How to disable all components in a panel(JPanel actually)

1125 단어
How to iterate all the components in a panel, and then operate on them? The following code can work as a reference or a clue of solving likewise problems.

public Container disableAllComponentsInPSIPPanel(Container container)
	{
		Container result = null;
		for(int i = 0; i < container.getComponentCount(); i++)
		{
			Component component = container.getComponent(i);
			if(component instanceof JFormattedTextField)
			{
				component.setEnabled(false);
			}
			if(component instanceof JCheckBox)
			{
				component.setEnabled(false);
			}
			if(component instanceof WideComboBox)
			{
				component.setEnabled(false);
				// ((WideComboBox)component).getSelectedItem();
			}
			if(component instanceof Container)
			{
				disableAllComponentsInPSIPPanel((Container) component);
			}
		}
		result = container;
		return result;
	}


When using this strip of code, you need just put in a JPanel instance. It's not so complex, right?

좋은 웹페이지 즐겨찾기