More Swing Things... Long Running Processes/Button control/Progress bar/Change listener etc

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

More Swing Things... Long Running Processes/Button control/Progress bar/Change listener etc

Dave Woodman

The previous example of Swing programming is all well and good as long as the actions happen QUICKLY. Any delay will hang the GUI.

 

Swing has a solution to this - the SwingWorker which farms any long-running activities to a background thread, from which it can receive updates during execution.

 

The example below consists of three files – created in the same way as the previous example (an exercise for the reader). The code within can be replaced by the code here. Once again, this serves only to illustrate the important points, and not as an example of best practice.

 

The files are:-

 

·         LongRunner.java – this setups up some buttons, a text field and a progress bar. Created in WindowBuilder and amended in the same manner as before

·         barBridge.nrx – the bridge class for LongRunner.java and also a property change listener

·         nrxSwingWorker.nrx – this file contains the worker definition for the thread

 

barBridge.nrx

 

/* A "bridge" for the WindowBuilder example */

 

options replace

 

package org.netrexx

 

import javax.swing

import java.awt

import java.beans

 

class barBridge

 

       properties private

              frame = JFrame

              bar = JProgressBar

              box = JTextField

              strt = JButton

              cncl = JButton

             

              sw = nrxSwingWorker

             

       method barBridge(args = String[])

              sw = nrxSwingWorker()

             

       method addFrame(f = JFrame)

              frame = f

             

       method addBar(b = JProgressBar) -- Could use the indirect property support for these...

              bar = b

              listener = pcl(bar)

              sw.addPCL(listener)

              bar.setStringPainted(1)

             

       method addBox(b = JTextField)

              box = b

              sw.addBox(box);

             

       method addStart(b = JButton)

              strt = b

              strt.setEnabled(1)

             

       method addCancel(b = Jbutton)

              cncl = b

              cncl.setEnabled(0)

             

       method killIt

              sw.cancel(1)

              cncl.setEnabled(0)

             

       method Bye

              killit

              frame.dispose

             

       method doIt

              strt.setEnabled(0)

              cncl.setEnabled(1)

              box.setText("Running...")

              sw.execute()

             

             

             

class pcl implements PropertyChangeListener

 

       properties private

              bar = JProgressBar

             

       method pcl(b = JProgressBar)

              bar = b

 

       method PropertyChange(event = PropertyChangeEvent)

      

              if event.getPropertyName = "progress" then

              do

                     val =  event.getNewValue()

                     bar.setValue(val.toString)

              end

 

 

nrxSwingWorker.nrx

 

/* A wrapper for the java SwingWorker avoiding generics */

options replace

 

import java.beans

 

package org.netrexx

 

class nrxSwingWorker extends SwingWorker

 

       properties private

              box = JTextField

 

       method nrxSwingWorker()

             

       method addBox(b = JTextField)

              box = b

              box.setText("Push That Button!")

 

       method addPCL(l = PropertyChangeListener)

              addPropertyChangeListener(l)

 

       method done

              box.setText("All done")   

 

       method doInBackground() returns Object

              -- val = Object

              loop i = 1 to 100

                     thread.sleep(100)

                     this.setProgress(i)

              end

              return null

             

       -- method process(List = Object[])

              /* handle intermediate results sent via publish (below)

                     method publish(val = Object[])

              */

             

 

LongRunner.java

 

package org.netrexx;

 

import java.awt.EventQueue;

 

import javax.swing.JFrame;

import java.awt.GridBagLayout;

import javax.swing.JButton;

import java.awt.GridBagConstraints;

import javax.swing.JProgressBar;

import java.awt.Insets;

import javax.swing.JTextField;

import javax.swing.event.ChangeListener;

import javax.swing.event.ChangeEvent;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

 

public class LongRunner {

 

       private JFrame frame;

       private JTextField textField;

       private static barBridge nrBit;

 

       /**

       * Launch the application.

       */

       public static void main(String[] args) {

              nrBit = new barBridge(args);

              EventQueue.invokeLater(new Runnable() {

                     public void run() {

                           try {

                                  LongRunner window = new LongRunner();

                                  window.frame.setVisible(true);

                           } catch (Exception e) {

                                  e.printStackTrace();

                           }

                     }

              });

       }

 

       /**

       * Create the application.

       */

       public LongRunner() {

              initialize();

              nrBit.addFrame(frame);

       }

 

       /**

       * Initialize the contents of the frame.

       */

       private void initialize() {

              frame = new JFrame();

              frame.setBounds(100, 100, 450, 300);

              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              GridBagLayout gridBagLayout = new GridBagLayout();

              gridBagLayout.columnWidths = new int[]{0, 390, 0, 0};

              gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};

              gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};

              gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};

              frame.getContentPane().setLayout(gridBagLayout);

             

              JButton btnNewButton = new JButton("Start");

              btnNewButton.addActionListener(new ActionListener() {

                     public void actionPerformed(ActionEvent arg0) {

                           nrBit.doIt();

                     }

              });

              GridBagConstraints gbc_btnNewButton = new GridBagConstraints();

              gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);

              gbc_btnNewButton.gridx = 1;

              gbc_btnNewButton.gridy = 1;

              frame.getContentPane().add(btnNewButton, gbc_btnNewButton);

              nrBit.addStart(btnNewButton);

             

              JButton btnNewButton_1 = new JButton("Cancel");

              btnNewButton_1.addActionListener(new ActionListener() {

                     public void actionPerformed(ActionEvent e) {

                           nrBit.killIt();

                     }

              });

              GridBagConstraints gbc_btnNewButton_1 = new GridBagConstraints();

              gbc_btnNewButton_1.insets = new Insets(0, 0, 5, 5);

              gbc_btnNewButton_1.gridx = 1;

              gbc_btnNewButton_1.gridy = 2;

              frame.getContentPane().add(btnNewButton_1, gbc_btnNewButton_1);

              nrBit.addCancel(btnNewButton_1);

             

              JProgressBar progressBar = new JProgressBar();

              GridBagConstraints gbc_progressBar = new GridBagConstraints();

              gbc_progressBar.fill = GridBagConstraints.BOTH;

              gbc_progressBar.insets = new Insets(0, 0, 5, 5);

              gbc_progressBar.gridx = 1;

              gbc_progressBar.gridy = 3;

              frame.getContentPane().add(progressBar, gbc_progressBar);

              nrBit.addBar(progressBar);

             

              textField = new JTextField();

              GridBagConstraints gbc_textField = new GridBagConstraints();

              gbc_textField.insets = new Insets(0, 0, 5, 5);

              gbc_textField.fill = GridBagConstraints.HORIZONTAL;

              gbc_textField.gridx = 1;

              gbc_textField.gridy = 5;

              frame.getContentPane().add(textField, gbc_textField);

              textField.setColumns(10);

              nrBit.addBox(textField);

             

              JButton btnNewButton_2 = new JButton("Exit");

              btnNewButton_2.addActionListener(new ActionListener() {

                     public void actionPerformed(ActionEvent e) {

                           nrBit.Bye();

                     }

              });

              GridBagConstraints gbc_btnNewButton_2 = new GridBagConstraints();

              gbc_btnNewButton_2.insets = new Insets(0, 0, 0, 5);

              gbc_btnNewButton_2.gridx = 1;

              gbc_btnNewButton_2.gridy = 7;

              frame.getContentPane().add(btnNewButton_2, gbc_btnNewButton_2);

       }

 

}

 

 

 

 

 

 


_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/