Results 1 to 6 of 6
Thread: Disabling a JPanel
- 11-28-2010, 09:17 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
Disabling a JPanel
So my problem is something like that:
I have a class that extends JPanel. After an event occurs I want to disable this panel.
My code is something like that
You get the idea I think :-)Java Code:public class GamePanel extends JPanel { ... enableTree ( this, false ) private void enableTree(Container root, boolean enable) { System.out.println ( "in" ); System.out.println ( "" + root ); Component[] com = root.getComponents(); for (int a = 0; a < com.length; a++) { System.out.println ( a ); com[a].setEnabled(enable); } } }
If it is any help, I should tell you that I use the method enableTree in another class too and it works just fine. Thing is that this class is like
I 've put some prints inside the method and the conclusion is that it doesn't get inside the for loop ( it has no components as to say? :P )Java Code:public class Foo { JPanel fooPanel; .... }
Thanks anyway :)
Cheers
-
This won't work if you have components inside of components (nesting of components) as will occur in most moderate to complex GUI's. To solve this, you need to create a method to disable components recursively. Hang on a sec and I'll show you...
edit:
something like this (which I placed in another recent post):
Java Code:public static void setEnabledAll(Container container, boolean enabled) { Component[] components = container.getComponents(); if (components.length > 0) { for (Component component : components) { component.setEnabled(enabled); if (component instanceof Container) { // has to be a container to contain components setEnabledAll((Container)component, enabled); // the recursive call } } } }
- 11-28-2010, 09:21 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
OK thanks for the fast reply :-)
I am waiting
- 11-28-2010, 09:39 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
OK. Solved. Thanks for your time ;-)
-
You're welcome. Oh, and welcome to the Java-Forums.org!
- 11-28-2010, 09:40 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Disabling Buttons
By superpala in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-26-2010, 10:00 PM -
Disabling Bluetooth passkey
By lambi in forum Sun Java Wireless ToolkitReplies: 0Last Post: 12-29-2009, 01:33 PM -
Disabling a button at EOF
By dbashby in forum New To JavaReplies: 1Last Post: 03-10-2009, 02:37 PM -
Disabling part of a method
By juru in forum Advanced JavaReplies: 10Last Post: 10-27-2008, 01:41 AM -
disabling JButtons after win in TicTacToe
By noisepoet in forum New To JavaReplies: 1Last Post: 05-18-2007, 11:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks