for(Component component : ProjectPanel.getComponents()) {I haven't found a good way around this yet, as the following addition to the loop fails as well:
component.setEnabled(false);
}
//use reflection to get the class typeDebugging reveals that the the JTextArea isn't seen as a component here (things like the scroll bar and viewport are, though). getComponents() doesn't loop over children, so maybe this does work two or three levels deeper, but that's getting a bit ridiculous.
if(component.getClass().equals(JScrollPane.class)) {
JScrollPane scrollPane = (JScrollPane)component;
for(Component subComponent : scrollPane.getComponents()) {
subComponent.setEnabled(false);
}
}
Until I figure out a better way, I'm setting the values explicitly using the following method:
textArea.setEnabled(false);The "setBackground" method is needed here (at least on Windows XP using the "Classic" theme), as the background isn't automatically set to the disabled color when setEnabled is set to "true". Most other components automatically handle this for you. This also means when you re-enable it, you need to set it back to the default enabled color:
textArea.setBackground(UIManager.getColor("TextArea.disabledBackground"));
textArea.setBackground(UIManager.getColor("TextArea.background"));BTW, I found a good article on using UIManager here that explains how to use and manipulate UI settings.
In any case, I found an official Java bug report from 1998 concerning what appears to be the exact enabled/disabled problem I ran into - the bug's status is "6-Fix Understood, request for enhancement". Though the suggested fix actually didn't work for me...
1 comment:
You could just traverse through the whole tree of a Container with a recursive method. That way the JTextArea (or any other component) can be embedded in a Container at arbitrary depth.
An example that gets you all the components of a Container into a List you provide:
public void getAllComponents(Container c, List l) {
if (c.getComponentCount() == 0) return;
Component[] components = c.getComponents();
for (int i = 0; i < components.length; i++) {
Container element = (Container)components[i];
getAllComponents(element, l);
l.add(element);
}
}
p.s.: can't use blockquote tags? o_O
Post a Comment