How to make a modal JDialog execute code immediately upon being shown

微笑、不失礼 提交于 2020-01-02 07:41:41

问题


Ok, I have a list of objects. I need to show a Modal JDialog and then pass it this list of objects and have it act on them. The problem is that when I call .show() it hijacks the EDT. The ideal situation would be to be able to pass the list in to the constructor and then when the dialog is shown, execute the function in question. In C# I'd use the Loaded event for this, but how to do it a JDialog escapes me.

Thoughts?


回答1:


JDialog dialog = new JDialog(...);
...
dialog.addComponentListener(new ComponentAdapter()
{
    public void componentShown(ComponentEvent e)
    {
        System.out.println("Time to do something");
    }
});
dialog.setVisible( true );



回答2:


JDialog dialog = new JDialog(...);
dialog.addWindowListener(new WindowAdaper() {
    @Override
    public void windowOpened(WindowEvent e) {
        super.windowOpened(e);
        // do something
    }
});

You get the idea.



来源:https://stackoverflow.com/questions/4542580/how-to-make-a-modal-jdialog-execute-code-immediately-upon-being-shown

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!