Disabling JPanel Components

Dear ,
I have the following method that disable all the components in a JPanel;
It has a JLabel, JTextField, and a JButton.
When I call this method from the constructor of the parent class; It is only disabling the JLabel, and the JTextField.
If I extend this class from another class, and call this method; None of the components are disabled.
public void disablePanel()
        myPanel.setEnabled(false);
        allCom = myPanel.getComponents();
        for (int i = 0 ; i < allCom.length; i++)
            allCom.setEnabled(false);
Would any one, suggest what might the reason be?
Best Regards,
M. Tleis

Your code disables only the first-level children of myPanel .
If the JButton is not a first-level child but merely a grandchild (e.g. added to a JPanel that is itself added to the initial myPanel), your loop never iterates to the button.
I suggest you have a look at camickr's DisabledPanel : http://tips4java.wordpress.com/2009/08/02/disabled-panel/

Similar Messages

  • Disable All Components in GUI?

    Hi, I am writing a gui using java.awt/javax.swing
    I want to disable all components in my gui (ie. setting all components of my gui to the disabled state by using setEnabled(false) from the java.awt.Component class) except for the Exit Button of the title bar (so that one can still exit the program via the exit button).
    I try using JFrame.setEnabled(false); however, the close button is also disabled. I can go about disabling the panes, the menus, and the buttons separately but that would be too troublesome.
    Anyone have some pointers? Thanks!

    hi
    I think this block of code solve your problem.
              JFrame f=new JFrame();
              f.setSize(400,400);
              f.getContentPane().setLayout(new FlowLayout());
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              JButton b1=new JButton("Demo1");
              JButton b2=new JButton("Demo2");
              f.getContentPane().add(b1);
              f.getContentPane().add(b2);
              Component[] c=f.getContentPane().getComponents();
              System.out.println(c.length);
              for(int i=0;i<c.length;i++)
                   c.setEnabled(false);
              b1.setEnabled(true);
              f.setVisible(true);

  • SetVisible() for JPanel Components

    All,
    I have a Class extended from JPanel
    In one of its function I am adding components like... its just a sample
    somecomp1= new Box(BoxLayout.X_AXIS);
            somecomp2= new Box(BoxLayout.Y_AXIS);
            somecomp3= new Box(BoxLayout.Z_AXIS);
            add(somecomp1, BorderLayout.NORTH);
            add(somecomp2, BorderLayout.CENTER);
            add(somecomp3, BorderLayout.CENTER);For disabling the somecomp3 when I say
    getComponent(2).setVisible(false)
    it doesn't work :( but it do work when I remove one of the component from BorderLayout.CENTER and just try to do getComponent(1).setVisible(false) for the one left
    any idea
    is it so because I am putting two components at the same position?
    any other way to do it?
    thanks
    Vishal

    First, it's not "my way". When you have multiple components, exactly
    one of which should be displayed at a time, and they're collocated,
    the preferred solution is CardLayout. Not my preferred solution --
    the preferred solution.
    Second, I'll add whatever comments I like. I'm well within the
    bounds of the forum rules. Feel free to ignore me. That's your
    prerogative.
    Your hack might make sense in the specific bounds of your
    codebase, but it certainly isn't the correct way to approach the general
    problem. I'm sorry you don't want to hear that, but it's the case.
    You're abusing the way that GBL works to imitate CL. For starters,
    solutions that rely on hard-coding component sizes are brittle.
    Bottom line - you're telling somebody who can't
    figure out how to use BorderLayout that they
    can play games with GBL and setVisible().
    They didn't ask about it - you're just volunteering
    information that will confuse them or lead them
    down a wrong path in the future. It's not clear
    to me why you're doing so, but you probably
    shouldn't.

  • Disable all components AND subcomponents

    I have a bunch of JButtons, JText, JTree, etc. in a JPanel and I want to disable/grey-out all of them. But some of the JComponents are in a Box. They don't get disabled. The code I am using now:
         public void setEditable(boolean bool) {
              for (Component component: getComponents()) {
                   component.setEnabled(bool);
    So the above works for all the components that are not in the Box
    I ended up doing the below (checks if the component is a Box and if it is, iterate through that enabling/disableing JComponents):
         public void setEditable(boolean bool) {
              for (Component component: getComponents()) {
                   component.setEnabled(bool);
                   if (component.getClass().toString().equals("class javax.swing.Box")) {
                        for(Component subComponent: ((Box)component).getComponents()) {
                             subComponent.setEnabled(bool);
    So big question is, is there some better way of doing this, or someway which is more generic?

    Well you could extend Box and override the setEnabled method to call setEnabled of all it's components, and then use that as your Box instead.
    But I have a couple of suggestions regarding the code you posted.
    1. You should use instanceof operator to check whether the component is an instance of Box
    2. You don't handle the case when there's a Box within a Box.
    e.g.,
    public void setEditable(boolean editable) {
        for (Component c : getComponents()) {
            setEditable(c, editable);
    private void setEditable(Component c, boolean editable) {
        c.setEnabled(editable);
        if (c instanceof javax.swing.Box) {
            for (Component subC : ((javax.swing.Box)c).getComponents())
                setEditable(subC, editable);
    }

  • JPanels & Components not behaving properly.

    Hi, I'm having great difficulties trying to get my panels and their components to work with each other. I want a few simple things on a JFrame in the following manner.
    |JLabel | JList |
    |JLabel | JTextField |
    |JLabel | JTextArea |
    |--------------------------|
    |JButton | JButton| JButton|
    The problem is all the JLabel, JList, JTextField and JTextArea occupy the same amount of space, I only want 5 lines for JList, 1 line for JTextField and 20 lines for JTextArea. Does anyone have any clues as to what I'm doing wrong? I've attached my code below. Please take a look and see if you can help me out. I'm stumped! :( Thanks in advance for all your help!
    Juivette
    emailScreen( Vector emailVector ) {
    this.emailVector = emailVector;
    emailList = new JList( emailVector );
    emailList.setVisibleRowCount(5);
    initScreen();
    addListeners();
    pack();
    private void initScreen() {
    getContentPane().setLayout( new BorderLayout() );
    JPanel mainPanel = new JPanel( new BorderLayout() );
    JPanel leftPanel = new JPanel( new GridLayout(0,1) );
    JPanel rightPanel = new JPanel( new GridLayout(0,1) );
    JPanel buttonPanel = new JPanel( new GridLayout(0, 3) );
    emailScrollPane = new JScrollPane( emailList );
    emailToLabel = new JLabel( "Receipent(s): ", JLabel.RIGHT );
    ChangeColour.changeColour( emailToLabel );
    subjectLabel = new JLabel( "Subject: ", JLabel.RIGHT );
    ChangeColour.changeColour( subjectLabel );
    subjectText = new JTextField( 50 );
    messageLabel = new JLabel( "Message: ", JLabel.RIGHT );
    ChangeColour.changeColour( messageLabel );
    messageArea = new JTextArea( 20, 50 );
    leftPanel.add( emailToLabel );
    leftPanel.add( subjectLabel );
    leftPanel.add( messageLabel );
    rightPanel.add( emailScrollPane );
    rightPanel.add( subjectText );
    rightPanel.add( messageArea );
    SendButton = new JButton( "Send" );
    ClearButton = new JButton( "Clear" );
    CancelButton = new JButton( "Cancel" );
    buttonPanel.add( SendButton );
    buttonPanel.add( ClearButton );
    buttonPanel.add( CancelButton );
    mainPanel.add( leftPanel, "West" );
    mainPanel.add( rightPanel, "East" );
    mainPanel.add( buttonPanel, "South" );
    getContentPane().add( mainPanel, "Center" );

    Review the Java Swing Tutorial on Layout Managers: (the BoxLayout may be what you need)
    http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
    The entire tutorial can be downloaded free from:
    http://java.sun.com/docs/books/tutorial/

  • JPanel components too close together

    Hi!
    I have a JPanel with three components (JLabels) aligned vertically....but there are so close together...how can I put a space (like another line) between them?
    Thank you very much...
    Gen

    I don't even know how your code works. Normally you assign a LayoutManager to the panel first and then you add the components to the panel.
    leftPanel.setLayout( new BoxLayout( leftPanel, BoxLayout.Y_AXIS ) );
    leftPanel.add(typeLabel);
    leftPanel.add(colorLabel);
    leftPanel.add(sizeLabel);If you want additional spacing then you just add use:
    leftPanel.add( Box.createVerticalStrut(10) ); Its even easier to use the Box class:
    Box box = Box.createVerticalBox();
    box.add( ??? );
    box.add( Box.createVerticalStrut(10) );
    box.add( ??? );

  • 11G ADF BC: First time loading the page, how to disable some components?

    Hi,
    I have a fusion web application created using Jdev 11G ADF BC. I am working on a search/results page that has a panel split. In the left panel, there is a list of nevigation links. The data shows in the right panel.
    What I want to achieve here is to enable only one link on the left when the page is first loaded and it is a search/result page. Disable all the rest of it because other links are related to more info about the search results. When the page is first loaded, there is no data on the right, so that there is no more other data to look at. Ideally, all the rest of the links should be disabled and only the "Query" link should be active and selected.
    Is there a way to do this with 11G? Thank you very much for your help.
    Regards,
    Annie

    initially, what ever the components you want to disable set the disabled="true" in the jspx. Once you fetch the query results and in that action enable the componens like commandButton1.setDisabled(Boolean.FALSE);. that's how I am doing. You can dynamically enable/disable by having a boolean variable in the backing bean and set the disable property of component lke disabled="#{backingBeanScope.yourbean.blnAddBtn}".

  • How to disable modules/components in a cluster

    hi,experts, I installed obiee 11g.
    some components such as bi publisher is useless for my purpose.
    how to disable it for using less memory

    If BI publisher is listed as an application in the deployments environment you can stop it.
    In the console, click on the link deployment (left of the screen), find the BI publisher entry, check it and
    choose stop, force stop now.

  • Change JPanel Components

    Hi ya,
    I need some help with what seems like quite a simple problem ... on an action from lets say a JButton I need the contents of a JPanel to change.
    I thought this would work :
    this.getContectPane().add(newPanel);where the current object holds the main JPanel which I want to change. This does not seem to work even if I add the pack(); or setVisible();
    How do I set this view to a new JPanel Object??
    Thanks

    Well normally all you need to do is:
    validate() (for AWT components or revalidate() for
    Swing components
    repaint() is sometimes required.
    If you need further help then you need to create a
    [url
    http://homepage1.nifty.com/algafield/sscce.html]Short,
    Self Contained, Compilable and Executable, Example
    Program that demonstrates the incorrectbehaviour, because I can't guess exactly what you are
    doing based on the information provided.
    And don't forget to use the [url
    http://forum.java.sun.com/help.jspa?sec=formatting]Cod
    e Formatting Tags so the code retains its
    original formatting.
    Thanks for everyones help but I have resolved the problem here, simple little error.

  • Removing A JPanels Components As Well As Itself?

    i know that u can use removeAll() to remove all components from a JPanel, but how can i remove all components from a JPanel AS WELL as remove the JPanel itself?

    It gets removed as soon as the code is executed unless you have some other code that is blocking the GUI from repainting itself.
    If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
    Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags so the posted code retains its original formatting.

  • DIsabling hardware components

    I am using Arch on a laptop and when on battery, I would like to have an option to turn of particular hardware components, for example PCMCIA slot, WiFi card, Bluetooth, sound card, etc.
    I want to achieve the same effect like disabling a device in Device Manager in Windows, so it does not draw any power.
    How can I achieve this?

    Ok. Can you tell me what are the driver (or should these be modules?) names for pcmcia, express card, dvdrom, bluetooth and firewire?
    I think for the sound card it would have be be dedicated disable command, as unloading the driver could cause too much config damage.

  • Disabling of components.

    I have a panel containing many components in it...say like JTextField, JComboBox, JCheckBox...etc
    I need to disable all the components...Is there any way to disable all the components in the panel at one shot or shd i disable each component like below.
    " textfield.setEnabled(false)"
    Thanks

    Try this:
    Component[] components = myPanel.getComponents();
    for ( int i = 0 ; i < components.length ; i++){
           components.setEnabled(false);
    You'll have to adjust that to your situation, and if the panel has subcontainers they will have to be treated in the same manner.
    That should get you started.
    DB

  • Disable the components of UI

    I have one requirements, where i have three Team member say A(Super), B and C. This all team is resulted in a table. When i click on A, i should see/updates the details of A, B and C. But if i click on B, the user can see/updates details of him(B) but can only view details of A and C. Same applies for C.
    How should i handle this. Can it be achived using personalization?

    You should write logic in the controller for this, when you select B, disable the fields that details A and C, so that these are not updatable.
    Thanks
    Tapash

  • Problem with Checkbox that disables/enables other components.

    Hi
    As I show in the title, I have problems with a checkbox that enables/disables another components (three exactly).
    I have done the follow:
    1.- Set property "AutoSubmit" true in the SelectOneChoice
    2.- Set "Disabled" property true or false depending if the SelectOneChoice is selected or not
    (#...selectBooleanCheckbox.value?false:true})
    3.- Set the Partial triggers to the SelectOneChoice in the three components.
    CASE 1: The SelectOneChoice is unmarked. The three components appears disabled and when I mark the SelectOneChoice, this components appears enabled. The thing works ;)
    CASE 2: The SelectOneChoice is marked. The three components appears enabled and when I unmark the SelectOneChoice, the components continue enabled. They don't disable until I press the submit button.
    how can i make that the components appears disabled when i unmark the checkbox?
    i try with de ChangeValueListener, but it only works where the SelectOneChoice changes from unmarked to marked.
    thanks!

    I think the problem is, when the value is null the checkbox return with the selected state, b'coz u r
    returning the checkbox (as it is). so pl'z try with below code (ADDED).
    public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus,
    int row, int column){
    if(value != null){
    Boolean booleanValue = (Boolean)value;
    setSelected(booleanValue.booleanValue());
    else /// ADDED
    setSelected(false);/// ADDED
    return this;
    Nediaph.

  • Disabling components when page is loaded

    Hi all,
    I have a web page where I want to disable some components (a selectmany_menu) depending on the data that other components on the page are displaying (a selectone_menu).
    I have got a value changed listener working fine that disables my selectmany_menu depending on what is selected in the selectone_menu. The event listener checks the data and then uses the setAttribute method to disable the selectmany_menu if necessary.
    However, what do I do when the page is first loaded. I there a way that I can invoke the event listener (or something else) when the page is loaded so that the correct components will be enabled and disabled.
    Thanks
    Patrick

    Maybe I can state my question above in another way...
    I using a standard command_button to navigate from one page to another page.
    <h:command_button actionRef="xyzBean.goToPage" label="Go" commandName="submit" />
    public Action getGoToPage() {
    return new Action() {
    public String invoke() {
    //MANIPULATE COMPONENTS ON NEW PAGE
    return "success";
    The question is, how do I access the components for the page that I am going to rather than the page that I am coming from. I tried this:
    FacesContext context = FacesContext.getCurrentInstance();
    UISelectMany myMenuComponent= (UISelectMany) context.getTree().getRoot().findComponent("myMenuComponent");
    myMenuComponent.setAttribute("disabled", "true");
    However, this fails because the tree that I get access to is the tree I am coming from, raher than the one I am going to.
    Is there a way to access the new tree?
    Patrick

Maybe you are looking for

  • Ref Cursor and For Loop

    The query below will return values in the form of bu     seq     eligible 22     2345     Y 22     2345     N 22     1288     N 22     1458     Y 22     1458     N 22     1234     Y 22     1333     N What I am trying to accomplish is to loop through

  • SITE_2 The application '/' does not exist IIS error - SharePoint 2013

    During stopping the service Microsoft SharePoint Foundation Web Application on an App server I haven't been able to get it started. When I try to stop it it gets stucked in STOPPING and when I try to start it gets stuck in STARTING. The problem now i

  • Unable to start the IDC Content service

    Hi All, The Content server is not getting started and I am getting the below error: Failed to initialize the server. Unable to instantiate class 'collections.CollectionScriptExtensions' while loading ScriptExtensions. Unable to instantiate java class

  • How to get extra information in server response "250 OK extra info here "

    Hello all, I am using javamail to send e-mail messages via an SMTP server that responds in the following way when I finish sending my DATA: 250 OK <extra information here> How can I get the <extra information here> text using javamail? I am using jav

  • Synced AI library content not appearing in Draw's Libraries

    I've seen a couple versions of this question on here but neither have really been answered: The short of it is, I'm synced on the same CC account between my Adobe Draw app and Ilustrator CC, both latest releases, and I can successfully send to Illust