简体   繁体   中英

JFrame does not draw content, only shows white rectangle, when made visible inside ActionListener

I have an application, on the beginning the Jframe is displayed, user enters the URL of a webpage that has to be analysed, then user clicks OK.

When user clicks OK actionListener should do this: It should display a new Jframe with a text "please wait , analysing website, this may take a little" then it should start the analyzePage() method.

the problem is that after user clicks OK button, the new Jframe is displayed, but it is blank, only with white rectangle inside. Only after analyzePage() method is finished (which is after many seconds) Contents of Jframe are displayed.

    static class OKListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        new WaitFrame();
// mf is main frame (the first frame where url was entered and where OK was clicked)
        mf.setEnabled(false);
        address = mf.getURLtext();
        analyzePage();
    }
}

public class WaitFrame extends JFrame {

public WaitFrame() {
    super();
    JFrame wait = this;
    JLabel jl = new JLabel("Čakajte prosím, analyzujem stránku, môže to chvíľu trvať.");
    JPanel jp = new JPanel();
    jp.add(jl);
    wait.getContentPane().add(jp);
    wait.pack();
    wait.setVisible(true);
}
}

Run your JFrame within the AWT-Thread:

//...
java.awt.EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        JFrame waitFrame = new JFrame();
        //...
        waitFrame.pack();
        waitFrame.setVisible(true);
    }
});
//...
// Other things to do ...
// dispose Frame after all, if neccessry ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM