langping86

stay hungry,stay foolish.

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

使用NetBeans 新建一个Swing Application Program,会自动生成如下的代码:

TaskMonitor:

TaskMonitor taskMonitor = new TaskMonitor ( getApplication ().getContext () );
        taskMonitor.addPropertyChangeListener ( new java.beans.PropertyChangeListener ()
        {

            public void propertyChange ( java.beans.PropertyChangeEvent evt )
            {
                String propertyName = evt.getPropertyName ();
                if ( "started".equals ( propertyName ) )
                {
                    if ( !busyIconTimer.isRunning () )
                    {
                        statusAnimationLabel.setIcon ( busyIcons[0] );
                        busyIconIndex = 0;
                        busyIconTimer.start ();
                    }
                    progressBar.setVisible ( true );
                    progressBar.setIndeterminate ( true );
                }
                else if ( "done".equals ( propertyName ) )
                {
                    busyIconTimer.stop ();
                    statusAnimationLabel.setIcon ( idleIcon );
                    progressBar.setVisible ( false );
                    progressBar.setValue ( 0 );
                }
                else if ( "message".equals ( propertyName ) )
                {
                    String text = ( String ) ( evt.getNewValue () );
                    statusMessageLabel.setText ( ( text == null ) ? "" : text );
                    messageTimer.restart ();
                }
                else if ( "progress".equals ( propertyName ) )
                {
                    int value = ( Integer ) ( evt.getNewValue () );
                    progressBar.setVisible ( true );
                    progressBar.setIndeterminate ( false );
                    progressBar.setValue ( value );
                }
            }
        } );

 新建自定义Task,用于和TaskMonitor关联使用,实例代码:

@Action
    public Task doCount ()
    {
        DoCountTask task = new DoCountTask ( getApplication () );
//        ApplicationContext C = getApplication().getContext();     
//        TaskMonitor M = C.getTaskMonitor();     
//        TaskService S = C.getTaskService();       
//        M.setForegroundTask(task); 
        return task;
    }

    private class DoCountTask extends org.jdesktop.application.Task<Object, Void>
    {

        DoCountTask ( org.jdesktop.application.Application app )
        {
            // Runs on the EDT.  Copy GUI state that
            // doInBackground() depends on from parameters
            // to DoCountTask fields, here.
            super ( app );
        }

        @Override
        protected Object doInBackground ()
        {
            // Your Task's code here.  This method runs
            // on a background thread, so don't reference
            // the Swing GUI from here.
            countMenuItem.setEnabled ( false );
            statusMessageLabel.setText ( "Counting ..." );
            for ( int i = 0; i <= 100; i++ )
            {
                countLabel.setText ( "... " + i + " ..." );
                try
                {
                    Thread.sleep ( 50 );
                }
                catch ( InterruptedException e )
                {
                    countLabel.setText ( e.toString () );
                    break;
                }
            }
            statusMessageLabel.setText ( "OK!" );
            countMenuItem.setEnabled ( true );
            return null;  // return your result
        }

        @Override
        protected void succeeded ( Object result )
        {
            // Runs on the EDT.  Update the GUI based on
            // the result computed by doInBackground().
        }
    }

 将上述的@Action注解的方法,绑定到某个控件即可触发事件,并实现DoCountTask和TaskMonitor关联并起作用。

 

 

posted on 2011-12-20 22:28  masb  阅读(1003)  评论(0编辑  收藏  举报