Handling components of a container

Asked

Viewed 300 times

0

I added a frame with several components to one container. In another class I add to jPanel the data from container. In this class, is there any way I can get access to the components of this container ?

Container ct3 = new Container();
ct3 = this.getContentPane();

The point is that I added all the components of the frame to the container, But when I show these components in another class, I wish some components weren’t shown. In the other class I have it:

painelPrincipalConfMainMenu.add(ConfUtilizador.ct3.getComponent(0));

Is there any way to do that ?

  • But then how do I dispose the elements of the first frame in the same way, but in the new frame ?

  • Because wrong suggestion, I will remove and publish a response that can help you

1 answer

1

Forehead like this:

Code:

    Component[] components = ConfUtilizador.ct3.getContentPane().getComponents();

    for (int i = 0; i < components.length; ++i) {
        if ((components[i] instanceof JButton)) {
            System.out.println(" um botao");
            JButton button = components[i];
            if(button.getttext().equals("ok"))
                   button.setVisivel(false);

        }else if ((components[i] instanceof JLabel)){
            System.out.println(" uma label");
            JLabel label= components[i];
            if(label.getttext().equals("nome"))
                   label.setVisivel(false);
        }else {
            System.out.println(" outro componente qualquer");
          }
    }
  • But this Component[] Components is done in the first frame constructor. And then how do I scroll through the other class to add them to the new frame?

  • I edited the code, I assume you have access to that container in the new frame, where you want to add the components

  • Is it something like that? : for(int i = 0; i < components.length ; i++){ if ((components[i] instanceof Jbutton)) { Jbutton button = (Jbutton) components[i]; if(button.gettext().equals("Cancelar") button.setVisible(false); paneelPrincipalConfMainMenu.add(components[i]); } }

  • Yes I think so, only the last painelPrincipalConfMainMenu.add(componentes[i]); you can use the button you created, but why? it doesn’t work?

  • painPrincipalConfMainMenu.add(components[i]); this is underlined in yellow and says 'Confusing identation' EDIT: The error was for something else that I already corrected. Does not error or add anything to the frame

  • 'cause use the button you made like this: painelPrincipalConfMainMenu.add(button);

  • components.length()=1 :S how is it possible if in the frame I have many components?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.