Monisha
Monisha

Reputation: 163

Toggle setVisibility for swt widget

I have a TitleAreaDialog with few buttons and a ProgressBar, upon selecting any of the buttons certain operation will be performed, so I would be needing the ProgressBar to indicate the user that some operation is in-progress. Since the time is unknown I am creating the progress bar globally, with type as SWT.INDETERMINATE and setVisibility(false) (so that the progress will be displayed only upon triggering the button listener) and whenever the button is pressed following is performed,

  1. progressBar.setVisible(true)
  2. perform the operation (progressBar should be active ie., should show as loading)
  3. progressBar.setVisible(false)

Is this the correct way? because i didn't get the output desired. I need the progress bar to be available/visible only when the operation is being performed and not throughout the dialog life.

Upvotes: 0

Views: 410

Answers (1)

Julia Kurde
Julia Kurde

Reputation: 53

When your button operation runs in the UI thread, the progressBar can't be updated. You could use a Job to do the operation in background:

public class MyLayout {
    public static void main (String [] args) {
        final Display display = new Display ();
        Shell shell = new Shell (display);
        RowLayout rowLayout = new RowLayout ();
        shell.setLayout (rowLayout);

        Button button = new Button (shell, SWT.PUSH);
        button.setText ("button0");

        final ProgressBar progressbar = new ProgressBar (shell, SWT.NONE);
        progressbar.setSelection (50);
        progressbar.setVisible(false);

        button.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event event) {
                progressbar.setVisible(true);
                // make a background operation
                Job job = new Job("Button...") {

                    @Override
                    protected IStatus run(IProgressMonitor monitor) {
                        // do the button operation...
                        return Status.OK_STATUS;
                    }
                };
                job.addJobChangeListener(new JobChangeAdapter() {

                    @Override
                    public void done(IJobChangeEvent jobEvent) {
                        // update progressbar in UI thread 
                        display.syncExec(new Runnable() {

                            @Override
                            public void run() {
                                progressbar.setVisible(false);
                            }
                        });
                    }

                });
                job.schedule();
            }
        });

        shell.pack ();
        shell.open ();

        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ())
                display.sleep ();
        }
        display.dispose ();
    }
}

But the Job itself also provides a monitor to show the user that an operation is in progress.

Upvotes: 1

Related Questions