| View previous topic :: View next topic |
| Author |
Message |
comm64
Joined: 12 Jan 2009 Posts: 1
|
Posted: Mon Jan 12, 2009 3:23 pm Post subject: Swing+How to use generated NetBeans ProgressBar code |
|
|
Hi, I need an example to use this code and display a progressBar:
| Code: |
public class DesktopApplication1View extends FrameView {
public DesktopApplication1View(SingleFrameApplication app) {
super(app);
initComponents();
// 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.length; 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);
}
}
});
}
@Action
public void showAboutBox() {
if (aboutBox == null) {
JFrame mainFrame = DesktopApplication1.getApplication().getMainFrame();
aboutBox = new DesktopApplication1AboutBox(mainFrame);
aboutBox.setLocationRelativeTo(mainFrame);
}
DesktopApplication1.getApplication().show(aboutBox);
}
|
thanks
|
|
| Back to top |
|
 |
Rocco
Joined: 08 Jan 2009 Posts: 17 Location: Orta di Atella (CE, Italy)
|
Posted: Tue Jan 13, 2009 2:00 pm Post subject: |
|
|
Try in this way:
create an Action, set the class that contains the method, set the method name; we'll call the action "myAction";
set the Background Task checkbox to TRUE;
set text string and tooltip string (optional);
you'll get this code:
@Action
public Task myAction() {
return new MyActionTask(org.jdesktop.application.Application.getInstance(desktopapplication1.DesktopApplication1.class));
}
private class MyActionTask extends org.jdesktop.application.Task<Object, Void> {
MyActionTask(org.jdesktop.application.Application app) {
// Runs on the EDT. Copy GUI state that
// doInBackground() depends on from parameters
// to MyActionTask fields, here.
super(app);
}
@Override protected Object doInBackground() {
// Your Task's code here. This method runs
// on a background thread, so don't reference
// the Swing GUI from here.
return null; // return your result
}
@Override protected void succeeded(Object result) {
// Runs on the EDT. Update the GUI based on
// the result computed by doInBackground().
}
}
Now, in the doInBackground() method, use the setProgress() method to manage your progress bar.
This task hould be easy, but if you face some trouble please post again.
Bye Bye.
|
|
| Back to top |
|
 |
Rune Henning Johansen
Joined: 12 Mar 2009 Posts: 7
|
Posted: Thu Mar 12, 2009 1:51 pm Post subject: Automatic start? |
|
|
Thanks to Rocco for good advice!
But how can I get this started automatically when the program starts?
|
|
| Back to top |
|
 |
rdblaha1 Posted via mailing list.
|
|
| Back to top |
|
 |
Rune Henning Johansen
Joined: 12 Mar 2009 Posts: 7
|
Posted: Wed Mar 18, 2009 7:19 pm Post subject: |
|
|
Thanks for the answer! But it wasn't that I was looking for. Sorry!
I need a sort of 'Set Action ...' for a frame the first time.
Here I have tried to to describe the problem with the code: http://moradi.no/count/
|
|
| Back to top |
|
 |
Rocco
Joined: 08 Jan 2009 Posts: 17 Location: Orta di Atella (CE, Italy)
|
Posted: Wed Mar 25, 2009 11:04 am Post subject: |
|
|
I hope the demo herewith attached, supplied with source code as Netbeans 6.5 project, may help you.
I suggest you to go to menu Windows/Other/Application Actions, the select "startCount" action, then click on "View Source" button.
Notice that you can recall the method "startCount" from wherever you need inside the application, not only from a menu item.
| Description: |
|
 Download |
| Filename: |
CounterDemo.zip |
| Filesize: |
209.94 KB |
| Downloaded: |
2468 Time(s) |
|
|
| Back to top |
|
 |
Rune Henning Johansen
Joined: 12 Mar 2009 Posts: 7
|
Posted: Sun Mar 29, 2009 5:39 pm Post subject: |
|
|
Thanks for the demo! But I was not able to run startCount at the start and see the action.
(I also got a small error: http://moradi.no/count/code-02.html)
I have been able to make a program demonstrating what I want it to do: http://moradi.no/count/code-03.html
This is not the way it should be done, but it's the only way I'm able to do it!
|
|
| Back to top |
|
 |
Rocco
Joined: 08 Jan 2009 Posts: 17 Location: Orta di Atella (CE, Italy)
|
Posted: Mon Mar 30, 2009 6:47 am Post subject: |
|
|
try to modify your code as follows (in bold):
public SimpleCountView ( SingleFrameApplication app )
{
super ( app );
initComponents ();
/*
* Do a counting when the program starts.
*/
doCount (); //this doesn't work
new CountThread(); //this should work
}
|
|
| Back to top |
|
 |
Rune Henning Johansen
Joined: 12 Mar 2009 Posts: 7
|
Posted: Mon Mar 30, 2009 1:03 pm Post subject: |
|
|
Now I am a little confused.
A call on doCount (); does work in my program. The routine simply disables a menu, changes the status and makes a call on new CountThread().
So why it doesn't work with you, I simply don't understand. And I can't see why your suggestion would help. Sorry!
But I am most interested in how to modify your code so the program starts a counting at the start.
|
|
| Back to top |
|
 |
Rocco
Joined: 08 Jan 2009 Posts: 17 Location: Orta di Atella (CE, Italy)
|
Posted: Tue Mar 31, 2009 8:48 pm Post subject: |
|
|
You can modify my code adding:
new StartCountTask(getApplication()).execute();
or, if you prefer, use this:
jMenuItem1.doClick(); that performs the same task.
|
|
| Back to top |
|
 |
Rune Henning Johansen
Joined: 12 Mar 2009 Posts: 7
|
Posted: Wed Apr 01, 2009 6:23 am Post subject: |
|
|
Your first suggestion worked, but without the progressbar running.
The second suggestion worked perfect!
Problem solved!
Thanks!
PS: All the relevant code for my solution - http://moradi.no/count/code-04.html
|
|
| Back to top |
|
 |
daisywhite333
Joined: 20 Mar 2012 Posts: 5
|
|
| Back to top |
|
 |
|