Wednesday, February 25, 2009

Working with Netbeans 6.5's Default Status Bar

I'm just curious about what's function of some default code in constructor body that come with netbeans 6.5 release. It show when you create new Java Desktop Application Project, then netbeans would give you default template which has default 3 java class.

this are step by step what I'm doing:
1. when dialog box about new project appear I choose 'Java' in categories box then
java desktop application in 'Projects' box.
2. when I push next button I choose Basic Application on 'choose application shell'
box. This is a message which show on the right box:

A basic application skeleton containing everything needed to start with a general desktop application, such as the following:
- main frame with a menu bar and status bar
- About box
- main Application class
- implementation of status bar messages and progress indication
- resource management mechanisms to better handle loading of icons and strings

3. click finish button and new project wolud be created. Now you will be given 3 java's class, that named with your project name in the beginning of each class name. Usually, it 's look like on below:
If your project name is 'DesktopApplication1' then netbeans would generate 3 class as,
- DesktopApplication1.java
- DesktopApplication1AboutBox.java
- DesktopApplication1View.java
Anyway, you can always change a name for each class, using refactor tool that come with netbeans.

Lets see the source for 'DesktopApplication1View.java' class. I grab part of source on the 'Default Constructor Body', it would be like this:

// 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() {
public void actionPerformed(ActionEvent e) {
statusMessageLabel.setText("");
}
});
messageTimer.setRepeats(false);
int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
for (int i = 0; i <>
busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
}
busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
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() {
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);
}
}
});

Get example project's source here then load it with netbeans IDE. It doesn't work well if you use netbeans release older than 6.5. On this project, I only create one button element that named as 'Process'. You can view it through to design mode for 'DekstopApplication1View.java' class.

What is would gonna show me from this project?
if you push 'Process' button, it will do a looping process that counting forward to 1000000000. when the process is still working just take a look at the right side from status bar. It will show you what happen. Don't forget to build it first, before you 'run' this project.

How it works?
if we see the source from constructor body that mean all 'action task' would be connected and monitored through task monitor and then showed to status bar. Status bar will show you both of progress bar and animation icon when there is a process.

What was happen?
Just add one method and one inner class into 'DekstopApplication1View.java' class to make this happen. first add an action method named 'processButton' which return a task.

@Action
public org.jdesktop.application.Task processButton(){
org.jdesktop.application.Task task=null;
task = new ProcessButtonTask(this.getApplication());
return task;
}

don't forget to add "@action" label first!
switch to design mode, right click on the 'process' button and Set Action...
In the Action label column, choose "processButton", and also enable Background Task option.
second, add inner class named as "ProcessButtonTask" which extends to Task class

private class ProcessButtonTask extends org.jdesktop.application.Task{

public ProcessButtonTask(org.jdesktop.application.Application app) {
super(app);
}

@Override
protected Object doInBackground()throws Exception{
for (double i=1;i<=100000000;){
i++;
}
return null;
}
}

third, clean and build the project.
fourth, run it!
hope this helpful :-)


No comments:

Post a Comment