0

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.

4

1 回答 1

4

您添加的组件将添加到 JDialog(JFrame、JWindow)的内容窗格,而不是根窗格。

所以你可以试试

dialog.getContentPane().getComponents();

此外,这不会进行递归,因此只会将组件直接添加到内容窗格中。

如果您希望将所有组件添加到内容窗格及其子项,则可以使用Swing Utils

于 2013-05-03T21:58:29.960 回答