Timer in java using Eclipse

前端 未结 3 912
小蘑菇
小蘑菇 2021-01-21 15:37

I\'m trying to do a little program in Java using Eclipse, and I\'m a little bit lost.

Could anybody explain me (in a \"for dummies way\") what do I have to do for repain

相关标签:
3条回答
  • 2021-01-21 15:57

    Why not use the timer functionality that is built into the SWT Display class?

    private void activateTimer(final Display display)
    {
        display.timerExec(
            1000,
            new Runnable() {
                public void run() {
                    whatever.redraw();
                    // If you want it to repeat:
                    display.timerExec(1000, this);
                }
            });
    }
    
    0 讨论(0)
  • you have to split that to the separete methods, better would be using javax.swing.Action instead of ActionListener

    private void activateTimer(){
        myTimer = new Timer(1000, myAction);
        myTimer.start();
    }
    
    private Action myAction = new AbstractAction() {
    
        private static final long serialVersionUID = 1L;
    
        @Override
        public void actionPerformed(ActionEvent e) {
              whatever.redraw();
        }
    };
    
    0 讨论(0)
  • 2021-01-21 16:08

    This page may be useful.

    If you use SWT, do it in SWT way :)

    EDIT:

    The problem is widget should be updated by eclipse's thread. Try this code.

    Job job = new Job("My Job") {
    @Override
    protected IStatus run(IProgressMonitor monitor) {
        Display.getDefault().asyncExec(new Runnable() {
            @Override
            public void run() {
                while(true)
                {
                    Thread.sleep(1000);
                    whatever.redraw();
                }
            }
        });
        return Status.OK_STATUS;
    }
    };
    job.schedule();
    
    0 讨论(0)
提交回复
热议问题