2010-06-25 75 views
3

我想知道一种方法,当你创建一个桌面应用程序的工作时,实际上使netbeans提供的状态栏,但我真的不知道如何。使用Netbean的桌面应用程序的状态栏

我包括下面的代码,所以每个人都可以理解我的意思和在netbeans中的位置。

// status bar initialization - message timeout, idle icon and busy animation, etc 
    ResourceMap resourceMap = getResourceMap(); 
    int messageTimeout = resourceMap.getInteger(
      "StatusBar.messageTimeout"); 
    messageTimer = new Timer(messageTimeout, 
      new ActionListener() 
    { 

     @Override 
     public void actionPerformed(ActionEvent e) 
     { 
      statusMessageLabel.setText(""); 
     } 
    }); 
    messageTimer.setRepeats(false); 
    int busyAnimationRate = resourceMap.getInteger(
      "StatusBar.busyAnimationRate"); 
    for (int i = 0; i < busyIcons.length; i++) 
    { 
     busyIcons[i] = resourceMap.getIcon(
       "StatusBar.busyIcons[" + i + "]"); 
    } 
    busyIconTimer = new Timer(busyAnimationRate, 
      new ActionListener() 
    { 

     @Override 
     public void actionPerformed(ActionEvent e) 
     { 
      busyIconIndex = (busyIconIndex + 1) % busyIcons.length; 
      statusAnimationLabel.setIcon(busyIcons[busyIconIndex]); 
     } 
    }); 
    idleIcon = resourceMap.getIcon("StatusBar.idleIcon"); 
    statusAnimationLabel.setIcon(idleIcon); 
    progressBar.setVisible(false); 

    // connecting action tasks to status bar via TaskMonitor 
    TaskMonitor taskMonitor = new TaskMonitor(
      getApplication().getContext()); 
    taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() 
    { 

     @Override 
     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); 
      } 
     } 
    }); 

我知道ir可能与TaskMonitor有关,但我无法得到它。 :(

+0

我看到NetBeans的statusPanel实现一次(正如您所指的那样),但似乎无法从NetBeans内部加载模板。你能告诉我加载这个模板吗?谢谢! – sudo 2012-12-28 07:56:19

回答

3

这是那些不合理的复杂的东西在Java中做一个,但它是可行的NetBeans的代码,希望你把你的可执行代码的任务,附着在TaskMonitor。 ,然后它会起作用,下面是我工作的例子:

@Action 
public void myTaskButtonAction() { // this action is called from a menu item or a button 
    startMyTaskAction(); 
} 
@Action 
public Task startMyTaskAction() { // this sets up the Task and TaskMonitor 
    StartMyTask task = new StartMyTask(org.jdesktop.application.Application.getInstance()); 

    ApplicationContext C = getApplication().getContext(); 
    TaskMonitor M = C.getTaskMonitor(); 
    TaskService S = C.getTaskService(); 
    S.execute(task); 
    M.setForegroundTask(task); 

    return task; 
} 

private class StartMyTask extends Task<Void, Void> { // this is the Task 
    StartMyTask(org.jdesktop.application.Application app) { 
     super(app); 
    } 

    @Override 
    protected Void doInBackground() { 
     try { 
      // specific code for your task 
      // this code shows progress bar with status message for a few seconds 
      setMessage("starting up");// status message 
      for(int progress=0; progress<100; progress += (int)(Math.random()*10)) { 
       setProgress(progress); // progress bar (0-100) 
       setMessage("prog: "+progress); // status message 
       try { 
        Thread.sleep((long)500); // sleep 500ms 
       } catch (InterruptedException ignore) { 
       } 
      } 
      setMessage("done");// status message 
     } 
     catch(java.lang.Exception e) { 
      //specific code for exceptions 
     } 

     return null; 
    } 

    protected void succeeded() { 
    } 
} 
+0

太棒了! :) Thnx! – kxk 2011-05-22 17:53:25

4

如果你只是想以更新状态栏中的文本,使用:

StatusDisplayer.getDefault().setStatusText("Hello World!"); 

如果你想(在你的例子一样)使用自定义的组件,你需要创建一个新的类它会自动注册自己:

@ServiceProvider(service=StatusLineElementProvider.class, position=18) 
public class TestStatusLineElementProvider implements StatusLineElementProvider { 
    private JLabel statusMessageLabel = new JLabel("Hello World!"); 

    @Override 
    public Component getStatusLineElement() { 
     return statusMessageLabel; 
    } 
} 
+0

这是一个更好的答案。 – user1052080 2013-10-19 16:27:45