How to make a modal JDialog execute code immediate

2019-05-14 07:13发布

问题:

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.