user1918908
user1918908

Reputation:

How to get the decendant components of a custom JDialog?

For some reasons I have to get the components in a custom JDilog. I tried to do that like this:

 private class RepoListDialog extends JDialog {
    public RepoListDialog(JFrame jf, String message){
    //do something
    JButton btConfirm = new JButton("Confirm");
    btConfirm.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){         
    for(Component c : RepoListDialog.this.getComponents()){
    //do something}
    }
    });
    this.add(btConfirm);
    //do something
    }
    }

But it does not work. I want to know how can I get the components which added in this Dialog by myself? I know it can be done by use rootPane. But I want to know that is there any other ways?

Any help will be highly apreciated.

Upvotes: 0

Views: 1518

Answers (1)

camickr
camickr

Reputation: 324147

The components you add are added to the content pane of a JDialog (JFrame, JWindow), not the root pane.

So you can try

dialog.getContentPane().getComponents();

Also, this does not do recursion, so it will only get component directly added to the content pane.

If you want all the components added to the content pane and its children then you can use Swing Utils.

Upvotes: 4

Related Questions