Results 1 to 6 of 6
  1. #1
    Esteban is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default 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

    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);  
    	}
     }
    }
    You get the idea I think :-)

    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

    Java Code:
     public class Foo
    {
      JPanel fooPanel;
      ....
    }
    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 )

    Thanks anyway :)

    Cheers

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

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

  3. #3
    Esteban is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    OK thanks for the fast reply :-)

    I am waiting

  4. #4
    Esteban is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    OK. Solved. Thanks for your time ;-)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    Esteban is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Disabling Buttons
    By superpala in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-26-2010, 11:00 PM
  2. Disabling Bluetooth passkey
    By lambi in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 12-29-2009, 02:33 PM
  3. Disabling a button at EOF
    By dbashby in forum New To Java
    Replies: 1
    Last Post: 03-10-2009, 03:37 PM
  4. Disabling part of a method
    By juru in forum Advanced Java
    Replies: 10
    Last Post: 10-27-2008, 02:41 AM
  5. disabling JButtons after win in TicTacToe
    By noisepoet in forum New To Java
    Replies: 1
    Last Post: 05-19-2007, 12:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •