Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

java - Simple Swing Delay

In a swing application, I have a popup jDialog which pops up with a jlabel that says "Hang on 5 seconds."

After 5 Seconds, the label should change to "Okay, now I'm done." And a button should appear allowing the user to click continue.

In the example action below (linked to a button which causes the popup), the popup appears as it should but it is blank instead of saying "Hang on 5 seconds." Then after 5 seconds everything updates and the labels are there and the button too. So what's going on? Is the thread sleeping before a repaint or something?

@Action
    public void popUp() {
        popUpDialog.setSize(300,200);
        popUpDialog.setLocationRelativeTo(null);
        popUpDialog.setVisible(true);

        popUpLabel.setVisible(true);
        popUpLabel.setText("Working, hang on a sec....");

        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }

        popUpLabel.setText("Okay Now I'm Done.");
        popUpBut.setVisible(true);
    }

EDIT: So I tried this, in an effort to use a swing timer in place of thread sleeping:

@Action
        public void popUp() {
            popUpDialog.setSize(300,200);
            popUpDialog.setLocationRelativeTo(null);
            popUpDialog.setVisible(true);

            popUpLabel.setVisible(true);
            popUpLabel.setText("Working, hang on a sec....");

            Timer timer = new Timer(speed, this);
            timer.setInitialDelay(pause);
            timer.start(); 

            popUpLabel.setText("Okay Now I'm Done.");
            popUpBut.setVisible(true);
        }

Obviously I'll need more code to finish the timer, but right off the bat I get a symbol not found, variable: timer error. What's that all about? Am I doing it wrong?

Edit 2: I changed the timer declaration and solved one problem but created another. Now I am getting the symbol not found error in regards to speed. I have never used a swing timer before and don't know how to use them. The java tutorial on the topic is convoluted and difficult to understand. Can any of you point me to a simple, clear example of a timer so I can learn from that and figure out what I need to do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If your code modifying your Swing component's state is on the EDT here (it should be), then no repainting of the label with the first text will take place even if you do call repaint(), because all other EDT requests queued before the last repaint need to complete before you reach that repaint, and your code here is one of those queued EDT events.

If you call repaint, it adds a repaint to the queue, it doesn't repaint right away. Your actions here will result in a 5 second wait, with the label before the next repaint having only the text you last set it to (as code queued on the EDT is executed fully before going to what next is queued).

Try using a Swing Timer, events fired from the Swing Timer are already executing on the EDT, so it's pretty much what you need, one event here where you set the text initially, and another event fired by the Swing Timer to change the text after 5 seconds.

Edit, example of Swing Timer firing once, after 5 seconds as requested by author:

    // set first jlabel text here

    ActionListener task = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("This is on the EDT after 5 seconds, " +
                "well depending on if I'm used with a Timer, and if " +
                "the right options are set to that Timer");
            // set second jlabel text here
        }
        };
    Timer timer = new Timer(5000 , task);
    timer.setRepeats(false);
    timer.start();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...