2010-06-25 35 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