Starten des Programms mit dem Progressbar

tanechka

Bekanntes Mitglied
Hallo zusammen,

ich habe folgendes Problem...beim Starten des Programms führe ich einige Konfigurationen durch. Dafür habe ich ein GUI mit dem Progressbar, das dem Benutzer den Fortschritt anzeigt:
Code:
package main_gui.gui;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import main.StartThread;



public class StartProgram {

    private static Display display;
    protected Shell shell;



    public static void main(String args[]) {
        display = Display.getDefault();

        Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
            public void run() {
                try {
                    StartProgram window = new StartProgram();
                    window.open();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    protected void createContents() {
        shell = new Shell(display, SWT.TITLE | SWT.PRIMARY_MODAL);
        shell.setSize(491, 197);
        Label status = new Label(shell, SWT.NULL);
        status.setAlignment(SWT.CENTER);
        status.setBounds(38, 77, 399, 71);
        /** take the primary monitor */
        Monitor primary = display.getPrimaryMonitor();
        /** get the size of the screen */
        Rectangle bounds = primary.getBounds();
        /** get the size of the window */
        Rectangle rect = shell.getBounds();
        /** calculate the centre */
        int x = bounds.x + (bounds.width - rect.width) / 2;
        int y = bounds.y + (bounds.height - rect.height) / 2;
        /** set the new location */
        shell.setLocation(x, y);

        ProgressBar progressBar = new ProgressBar(shell, SWT.NONE);
        progressBar.setMaximum(0);
        progressBar.setBounds(38, 49, 399, 17);

        new StartThread(shell, display, progressBar, status).start();
    }


    public void open() {
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
}

StartThread ist bei mir der Thread, der den Fortschritt des Progressbars steuert und Konfigurationen ausführt.

Code:
package main;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import main_gui.gui.MainGui;
import processlistener.ProcessListener.Processes;

public class StartThread extends Thread {
 private Shell shell;
 private Display display;
 private ProgressBar progressBar;
 private Label labelInfo;
 
 public StartThread(Shell shell, Display display, ProgressBar progressBar,
   Label labelInfo) {
  this.shell = shell;
  this.display = display;
  this.progressBar = progressBar;
  this.labelInfo = labelInfo;
 }

    @Override
    public void run() {
        if (display.isDisposed()) {
            return;
        }
        try {
        System.out.println("Konfiguration");
          Thread.sleep(500);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
 /**
  *
  * @param statusText
  */
 private void setFehler(String fehler) {
  display.asyncExec(new Runnable() {
   @Override
   public void run() {
    labelInfo.setText(fehler);
   }
  });
 }
 /**
  *
  */
 private void closeProgressGUI() {
  display.asyncExec(new Runnable() {
   @Override
   public void run() {
    shell.close();
   }
  });
 }
 /**
  *
  */
 private void startMainGUI() {
  display.asyncExec(new Runnable() {
   @Override
   public void run() {
    MainGui main = new MainGui();
    main.open();
//    shell.dispose();
   }
  });
 }
}

Mein Progressbar funktioniert gut, der Statustext wird auch angezeigt.
Wenn der Progressbar auf 100 % ist, möchte ich die GUI mit dem Progressbar schließen und eine MainGui starten. Mein Problem ist das zu implementieren, habt ihr vielleicht Vorschläge?

Vielen Dank
 
Ich habe an so eine Lösung gedacht. So eine Klasse zu erstellen:
Code:
public class ThreadTest {
public static void main(String[] args) {   

    StartThread mythread = new StartThread();

    MainThread mainGui=new MainThread();
    mythread.run();
    mainGui.run();
  }
}
In der Main wurde erstmal der Progressbar-Thread aufgerufen und wenn das fertig ist, würde der MainThread-Aufruf passieren. Meine Lösung funktioniert leider nicht.
Ich bitte um Hilfe!
 
Warum machst du nicht einfach so, dass der erste Thread mit dem Progressbar durchlaufen lassen, dann die Progressbar von Shell (oder auch alle Controls) entfernen und dann die UI vom MainThread in die Shell hinzufügen?
 

Zurück
Oben