【问题标题】:Using Netbean's Desktop Application's status bar使用 Netbeans 桌面应用程序状态栏
【发布时间】:2010-06-25 21:46:24
【问题描述】:

我想知道一种方法,让您在创建桌面应用程序时实际使用 netbeans 提供的状态栏,但我不太明白如何操作。

我将下面的代码包含在内,以便每个人都能理解我的意思以及在 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 有关,但我无法理解。 :(

【问题讨论】:

  • 我曾经看到过 NetBeans 的 statusPanel 的实现(就像您所指的那样),但我似乎无法从 NetBeans 中加载模板。你能告诉我加载这个模板吗?谢谢!

标签: java netbeans desktop-application statusbar


【解决方案1】:

如果您只想更新状态栏中的文本,请使用:

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;
    }
}

【讨论】:

  • 这是一个更好的答案。
【解决方案2】:

这是在 Java 中做的那些非常复杂的事情之一。但这是可行的。 NetBeans 代码要求您将可执行代码放入任务中,附加到任务监视器,然后它就会工作。这是对我有用的示例:

@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() {
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    相关资源
    最近更新 更多