zoukankan      html  css  js  c++  java
  • NetBeans中TaskMonitor的使用

    使用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关联并起作用。

  • 相关阅读:
    局部 与 整体 修正 逐渐逼近
    en-zh(社会问题)social problems
    单调性 [1 + 1 / (n)]^n
    en-zh(科学技术)science and technology
    mysql函数之截取字符串
    看数据库的文件大小 MySQL Binlog日志的生成和清理规则
    Brouwer不动点
    布尔巴基学派
    量子杨-Baxter方程新解系的一般量子偶构造_爱学术 https://www.ixueshu.com/document/f3385115a33571aa318947a18e7f9386.html
    COMSOL
  • 原文地址:https://www.cnblogs.com/masb/p/2295219.html
Copyright © 2011-2022 走看看