我想知道在创建桌面应用程序工作时实际创建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);
}
}
});
我明白这可能与TaskMonitor
有关但我无法理解。 :(
答案 0 :(得分: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;
}
}
答案 1 :(得分:3)
这是Java中那些不合理的复杂事情之一。但这是可行的。 NetBeans代码希望您将可执行代码放在Task中,附加到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() {
}
}