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

  • 相关阅读:
    礼物的最大价值
    复杂链表的复制
    全排列(回溯法)
    删除排序数组中的重复项
    三角形最小路径和(经典dp)
    链表中倒数第k个节点
    造成segment fault,产生core dump的可能原因
    wmpnetwk.exe怎么禁启动
    GSM/GPRS/EDGE/WCDMA/HSDPA/HSUPA--辨析
    OSI七层参考模型每一层都有哪些协议
  • 原文地址:https://www.cnblogs.com/masb/p/2295219.html
Copyright © 2011-2022 走看看