Frage zu Eclipse Plugin

Status
Nicht offen für weitere Antworten.
G

Guest

Gast
Hi!

Ich versuche grad mein erstes Eclipse Plugin zu schreiben. Es soll einfach die aktuelle Zeit angeben. Bisher habe ich folgendes:

package com.example.helloworld.views;

Code:
import java.util.Date;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;


   public class HelloWorldView extends ViewPart {
      static Label label;

      public HelloWorldView() {
      }

      public void createPartControl(Composite parent) {
         label = new Label(parent, SWT.WRAP);
         label.setText("Es ist jetzt: " + new Date().toLocaleString());
      }

      public void setFocus() {
      }

   }

Ich brauche jetzt noch einen Thread, der die Zeit die im Label angezeigt wird immer aktualisiert. Meine bisherigen Versuche sind aber alle gescheitert, ich habe immer eine Fehlermeldung bekommen von wegen illegaler Threadzugriff. Wie krieg ich immer die aktuelle zeit ins Label?

Grüsse, Chris
 

WieselAc

Top Contributor
am einfachsten startest du intern einen neuen thread, der deje sekunde das datum/ den label text neu setzt und dan einen repaint aufruft.

edit:

probier mal folgendes aus (ist alles dings ungetestet und aus dem Bauch heraus)

Code:
import java.util.Date;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;


   public class HelloWorldView extends ViewPart  implements Runnable {
      static Label label;

      public HelloWorldView() {
      }

      public void createPartControl(Composite parent) {
         label = new Label(parent, SWT.WRAP);
         label.setText("Es ist jetzt: " + new Date().toLocaleString());
         new Thread(this).start();
      }

	public void run() {
		while (true) {
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
			}

                        label.setText("Es ist jetzt: " + new Date().toLocaleString());

			repaint();
		}
	}

      public void setFocus() {
      }

   }
 

byte

Top Contributor
Du kannst in SWT nicht ohne weiteres von einem nebenläufigen Thread die Widgets verändern. Du musst das z.b. über Display.asyncExec() machen, sonst fliegt eine Exception:


Code:
Display.asyncExec(new Runnable() {
  public void run() {
    label.setText("Es ist jetzt: " + new Date().toLocaleString());
  }
});


edit - @WieselAc: Genau so geht das eben in SWT bzw. RCP nicht. Und auch in Swing sollte man das so nicht machen, weil viele Komponenten nicht thread-safe sind.
 

WieselAc

Top Contributor
Ups dann hab ich wieder was über SWT gelernt und zieh ich meine Aussag zurück.


Das die Vorgehensweise auch in Swing nicht gerade die Beste ist, war mir bewusst, aber so auf die schnelle und für den Hausgebrauch tipp ich leider doch häufig so.


*Notiz an mich: Sauberer proggen*
 

byte

Top Contributor
Aus dem Grund fliegt in SWT auch grundsätzlich eine Exception. Da wird Faulheit bestraft. ;)
 
G

Guest

Gast
Hi!

Danke!! Das aktualisieren funktioniert jetzt wunderbar!

Nur ein problem habe ich noch: wenn ich Eclipse beende, friert es komplett ein und ich kanns nur noch "abschießen". Ist bei dem thread noch was falsch?

Code:
import java.util.Date; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Label; 
import org.eclipse.ui.part.ViewPart; 

   public class HelloWorldView extends ViewPart  { 
      static Label label; 

      public HelloWorldView() { 
      } 

      public void createPartControl(Composite parent) { 
         label = new Label(parent, SWT.WRAP); 
         label.setText("Es ist jetzt: " + new Date().toLocaleString()); 

         Display d = parent.getDisplay();
         d.asyncExec(new Runnable() { 
              public void run() { 
                    while(!parent.isDisposed()) {
       	        label.setText("Es ist jetzt: " + new Date().toLocaleString());
                    }
      	  } 
      	});
      } 

      public void setFocus() { 
      } 

   }
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
hdi Frage zu Eclipse Plugin Development Plattformprogrammierung 12
S Frage zum Produktexport von Eclipse RCP Plattformprogrammierung 13
G Frage zu Eclipse Adapter Mechanismus Plattformprogrammierung 2
H Frage zur Verwendung des RCP Plattformprogrammierung 16
L RCP Frage zur CNF und ProjektExplorer? Plattformprogrammierung 3
R Frage zum RCP Plugin-Mechanismus. Plattformprogrammierung 2
Kr0e Allgemeine NetBeans Platform Module Frage Plattformprogrammierung 13
G Frage zu IEditorInput Plattformprogrammierung 21
hdi Frage zu Target Platform Plattformprogrammierung 15
N Noch eine Frage zu GEF Plattformprogrammierung 5
E OSGi Eclipse Plug-in programmierung: java.lang.NullPointerException: Cannot enter synchronized block because "profile" is null Plattformprogrammierung 4
R Eclipse Bundles Plattformprogrammierung 2
Robertop RCP Command in bereits bestehendes Eclipse-Menü einbauen Plattformprogrammierung 4
T Java Projekt läuft nur in Eclipse Plattformprogrammierung 6
M Eclipse Plugin PreferencePage BooleanFieldEditor Plattformprogrammierung 1
F OSGi Plugin unter Eclipse arbeitet anders als wenn veröffentlicht Plattformprogrammierung 2
D RCP P2-Repository für Eclipse-Plugins Plattformprogrammierung 0
M Debuging in Eclipse Annotations Processor Plattformprogrammierung 0
D Nach Sprachpaket Installation kann eclipse nicht mehr gestartet werden Plattformprogrammierung 4
L0MiN Wie kann ich ein Klassendiagramm aus Eclipse heraus erstellen? Plattformprogrammierung 5
M Eclipse Probleme beim Ausführen eines Programms Plattformprogrammierung 3
feinperligekohlensaeure Eclipse Workspace gemeinsam Nutzen -> keine .project Datei Plattformprogrammierung 1
F Eclipse Build Path auf benötigte Projekte Plattformprogrammierung 4
B Daten von Eclipse extern sichern Plattformprogrammierung 2
B Eclipse zeigt Fehler nach Java-Update auf 1.8.0_40 Plattformprogrammierung 3
VfL_Freak [Eclipse] Fehleranzeige im Reiter "Problems" Plattformprogrammierung 1
S Errors in workspace bei eclipse Plattformprogrammierung 3
T eclipse bietet keine META-INF an Plattformprogrammierung 1
N Eclipse wo starten Plattformprogrammierung 6
A Eclipse schließt einfach ohne Fehlermeldung Plattformprogrammierung 4
W Eclipse vergißt beim Exportieren Resources-Ordner Plattformprogrammierung 11
A RCP Eclipse e4 und Injection-Contexts Plattformprogrammierung 0
K Eclipse fährt nicht mehr hoch. Metadatendatei schuld? Plattformprogrammierung 7
C [Eclipse RCP E4]InjectionException: no actual value was found for the argument "MDirtyable" Plattformprogrammierung 8
T Eclipse Eigenschaften hinzufügen Plattformprogrammierung 2
M RCP [Eclipse RCP 3.4] ViewTab Kontextmenu "Alle schließen" Plattformprogrammierung 0
D Problem mit eclipse Plattformprogrammierung 2
S Verschieben des Verzeichnis .eclipse aus Userprofile Plattformprogrammierung 3
A RCP Kann kein Eclipse 4 Projekt anlegen Plattformprogrammierung 0
S RCP Exportiertes RCP Produkt lädt Datei nicht, bei Start aus Eclipse wird Datei jedoch gefunden Plattformprogrammierung 6
G Eine Library in Eclipse zur Verfügung stellen Plattformprogrammierung 14
Gregorrr Eclipse RCP Product Build-Nummer + Jenkins Plattformprogrammierung 6
R Einfaches Eclipse-Plugin-Beispiel funktioniert nicht Plattformprogrammierung 5
H OSGi OSGi + Logback + slf4j - Eclipse Run Configuration Plattformprogrammierung 7
C plugin development environment eclipse Plattformprogrammierung 4
R RCP Commands nutzen / org.eclipse.ui.file.save Plattformprogrammierung 7
O Installiere externe Methode "containsNone" in Eclipse Plattformprogrammierung 2
M Keybinding in Eclipse Plugin Plattformprogrammierung 3
TheWhiteShadow RCP Konstrukt für Editor in Eclipse gesucht. Plattformprogrammierung 3
B Eclipse Probleme mit build Plattformprogrammierung 8
H Keine Hilfe/API mehr in Eclipse Juno? Plattformprogrammierung 4
B Neue SuppressWarning in Eclipse Juno Plattformprogrammierung 6
B Eclipse Property Page Plattformprogrammierung 6
B Eclipse PreferencePage Plattformprogrammierung 3
B Eclipse Plugin Einstellungsleiste Plattformprogrammierung 2
B Birt-Previewer über Eclipse Plattformprogrammierung 14
J Eclipse Plug-In für UML-Diagramme Problem Plattformprogrammierung 4
O Eclipse Plugin - Einfachstes Problem, das es gibt. Plattformprogrammierung 2
R Eclipse RCP Tabellen-View (Anfängerfrage) Plattformprogrammierung 3
C Eclipse: Notification beim umbennen von Methoden? Plattformprogrammierung 4
schalentier Eclipse Sourcecode Plattformprogrammierung 4
A Eclipse undo/redo button reagiert nicht auf Änderungen in der OperationHistory Plattformprogrammierung 5
P Eclipse M2_REPO (NON MODIFIABLE) Plattformprogrammierung 8
B Eclipse: Contextmenu id erhalten? Plattformprogrammierung 9
H Datei in eclipse wird nicht erkannt Plattformprogrammierung 3
O Neues Tastenkürzel für Eclipse Editorfenster registrieren Plattformprogrammierung 2
3 Eclipse Editor Plugin selektiert Projekt nicht Plattformprogrammierung 10
Madlip RCP eclipse.ui.bindings (Key-Problematik) Plattformprogrammierung 2
S Eclipse vs. JavaMail vs. Ubuntu vs. Windows Plattformprogrammierung 3
M eclipse führt applikationen nicht mehr aus Plattformprogrammierung 6
S Eclipse: Auf CTRL-C/CTRL-V im Package Explorer? Plattformprogrammierung 4
M Eclipse und Hilfen Plattformprogrammierung 5
G Eclipse buggt rum!!! Plattformprogrammierung 4
B Eclipse RCP und Java 7 Plattformprogrammierung 7
G RCP Eclipse Editor + Guice Plattformprogrammierung 8
T RCP Eclipse RCP: Wo/wann im Plugin ist workbench initialisiert? Plattformprogrammierung 14
G Eclipse Plug-in: wie refreshe ich eine combobox? Plattformprogrammierung 4
M Eclipse Plug-in: Wie eine Grafik einfügen? Plattformprogrammierung 20
M RCP [Eclipse RCP] Feature Export und "Fehler"... Plattformprogrammierung 3
P Eclipse speichert keine Änderungen Plattformprogrammierung 5
M Eclipse - Dokumentation nicht über das Internet benutzen Plattformprogrammierung 3
C Eclipse Plugin NoClassDefFoundError Plattformprogrammierung 4
E org.eclipse.ui.dialogs.ListSelectionDialog erzeugt ClassNotFoundException zur Laufzeit Plattformprogrammierung 4
M Eclipse Plugin Entwicklung - NoClassDefFoundError Plattformprogrammierung 10
C RCP Verständnisfrage Eclipse/Equinox Plattformprogrammierung 4
D eclipse-RCP von der Konsole baut nicht Plattformprogrammierung 3
L RCP Eclipse Popupmenu im PackageExplorer erweitern? Plattformprogrammierung 7
C Eclipse Plugin Entwicklung: Editor für Flussdiagramme Plattformprogrammierung 6
S (OSGI - EQUINOX) Welche Eclipse IDE? Plattformprogrammierung 3
C Pdf in jar datei einbinden mit eclipse Plattformprogrammierung 23
G neuer branch in SVN mit Eclipse Plattformprogrammierung 6
F RCP eclipse rcp ohne eclipse? Plattformprogrammierung 15
dzim RCP Eclipse Provisioning Plattformprogrammierung 3
G RCP Abhängigkeiten von Eclipse Plugins Plattformprogrammierung 9
K Eclipse RCP, EMF, Hibernate Problem Plattformprogrammierung 7
B RCP eigenes Eclipse Plugin wird nicht aufgelöst Plattformprogrammierung 7
T Eclipse Helios XMLUnit importieren Plattformprogrammierung 6
R Verwendung pdfbox in Maven-Projekt in Eclipse Plattformprogrammierung 3
lumo RCP wechseln der Font von Eclipse per code Plattformprogrammierung 10
lumo RCP Eclipse internationalisierung Plattformprogrammierung 5

Ähnliche Java Themen

Neue Themen


Oben