Lock all buttons in a panel

Discussion in 'Java' started by Si, Apr 7, 2004.

  1. Si

    Si Guest

    Is there any way to "disable" a panel, or lock all buttons in a panel
    without any long drawn out code?
    Si, Apr 7, 2004
    #1
    1. Advertising

  2. "Si" <> wrote in message news:<c50mn0$2n0cmr$-berlin.de>...
    > Is there any way to "disable" a panel, or lock all buttons in a panel
    > without any long drawn out code?


    I suppose you could call getComponents() on the panel, and then loop
    through the returned array to disable them all. Of course, if you have
    sub-containers with other controls, then you'll have to do this
    recursively.

    AFAIK, there's no method to simply disable an entire panel of
    controls.
    Karl von Laudermann, Apr 7, 2004
    #2
    1. Advertising

  3. On Wed, 7 Apr 2004 11:55:12 +0100, Si wrote:

    > Is there any way to "disable" a panel,
    > or lock all buttons in a panel
    > without any long drawn out code?


    <untested>
    Panel.setEnabled(false);
    </untested>

    And in future, please do not cross-post
    to three groups. F'ups set to c.l.j.g.

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Apr 7, 2004
    #3
  4. Si

    Filip Larsen Guest

    "Si" wrote

    > Is there any way to "disable" a panel, or lock all buttons in a panel
    > without any long drawn out code?


    How about something like this general code:

    import java.awt.*;

    public static void setChildrenEnabled(Container container, boolean
    enabled) {
    Component[] children = container.getComponents();
    for (int i = 0; i < children.length; i++) {
    children.setEnabled(enabled);
    if (children instanceof Container) {
    setChildrenEnabled(children,enabled);
    }
    }
    }

    Add appropriate tests if you only want certain components to be
    disabled. If you use custom derived panels you can extend the setEnabled
    method to call setChildrenEnabled directly instead of the recursive call
    used above.

    It is a bit drawn out, but you only have to write it once.


    Regards,
    --
    Filip Larsen
    Filip Larsen, Apr 7, 2004
    #4
  5. Andrew Thompson, Apr 7, 2004
    #5
  6. Is this "long drawn out code"??


    /**
    * On all subcomponents contained in component which are instances
    of the classFilter class
    * setEnabled(enabled) is called.
    * Only subComoponents extending JComponent are "traversed"
    * @param component topComponent to traverse
    * @param classFilter components of which class to set enabled
    * @param enabled
    */

    static void setEnableSubComponent(JComponent component, Class
    classFilter, boolean enabled)
    {
    if (classFilter.isAssignableFrom(component.getClass()))
    {
    component.setEnabled(enabled);
    return; // assuming there will not be interesting subcomponents
    }
    for (int i =0; i<component.getComponentCount();i++)
    {
    Component subComponent = component.getComponent(i);
    if (subComponent instanceof JComponent)
    {
    setEnableSubComponent((JComponent) subComponent, classFilter,
    enabled);
    }

    }
    }

    Invoke with

    setEnableSubComponent(yourPanel, JButton.class, false);

    The class filter might be replaced with other filters.

    Stephan

    __________________________
    Stephan Wehner
    Editor, Traffic Life: Passionate Tales and Exit Strategies
    www.trafficlife.com



    "Si" <> wrote in message news:<c50mn0$2n0cmr$-berlin.de>...
    > Is there any way to "disable" a panel, or lock all buttons in a panel
    > without any long drawn out code?
    Stephan Wehner, Apr 7, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harry
    Replies:
    3
    Views:
    530
    Harry Singh
    Nov 10, 2003
  2. Fuzzyman
    Replies:
    3
    Views:
    480
    Andrew MacIntyre
    Dec 5, 2003
  3. Robert Brewer
    Replies:
    0
    Views:
    479
    Robert Brewer
    Dec 5, 2003
  4. k3xji
    Replies:
    7
    Views:
    788
    Gabriel Genellina
    Dec 30, 2008
  5. nano2k

    Application.Lock()/UnLock() or lock(Application)

    nano2k, Jul 23, 2007, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    268
    nano2k
    Aug 9, 2007
Loading...

Share This Page