Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » The Exception when the code that swing call the code that swt
The Exception when the code that swing call the code that swt [message #461840] Mon, 03 October 2005 02:50 Go to next message
Eclipse UserFriend
Originally posted by: guanxiaofeng_27.126.com

i have a main class be writed by swing,in a button click event,call a swt
window,the code as follow:

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwtText sw = new SwtText();
sw.open();
}
}

swt window code as follow:

public class SwtText{

protected Shell shell;

public void open() {

final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}

protected void createContents() {
shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
}

}

the Exception as follow:

org.eclipse.swt.SWTException: Invalid thread access
at org.eclipse.swt.SWT.error(SWT.java:2691)
at org.eclipse.swt.SWT.error(SWT.java:2616)
at org.eclipse.swt.SWT.error(SWT.java:2587)
at org.eclipse.swt.widgets.Widget.error(Widget.java:381)
at org.eclipse.swt.widgets.Shell.<init>(Shell.java:245)
at org.eclipse.swt.widgets.Shell.<init>(Shell.java:236)
at org.eclipse.swt.widgets.Shell.<init>(Shell.java:189)
at SwtText.createContents(SwtText.java:38)
at SwtText.open(SwtText.java:28)
at SwtText.run(SwtText.java:48)
at java.lang.Thread.run(Thread.java:534)
Re: The Exception when the code that swing call the code that swt [message #461842 is a reply to message #461840] Mon, 03 October 2005 06:13 Go to previous messageGo to next message
Haris Peco is currently offline Haris PecoFriend
Messages: 1072
Registered: July 2009
Senior Member
You have to call swing in swing ui (it is ok in button.addA..Listener() and
swt in swt ui thread.
This is example :
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Display display = PlatformUI.getWorkbench().getDisplay();
display.syncExec(new Runnable() {
public void run () {
SwtText sw = new SwtText();
sw.open();
}
}
}
}

Peco
xiaofeng wrote:

> i have a main class be writed by swing,in a button click event,call a swt
> window,the code as follow:
>
> button.addActionListener(new ActionListener() {
> public void actionPerformed(ActionEvent e) {
> SwtText sw = new SwtText();
> sw.open();
> }
> }
>
> swt window code as follow:
>
> public class SwtText{
>
> protected Shell shell;
>
> public void open() {
>
> final Display display = Display.getDefault();
> createContents();
> shell.open();
> shell.layout();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> }
>
> protected void createContents() {
> shell = new Shell();
> shell.setSize(500, 375);
> shell.setText("SWT Application");
> }
>
> }
>
> the Exception as follow:
>
> org.eclipse.swt.SWTException: Invalid thread access
> at org.eclipse.swt.SWT.error(SWT.java:2691)
> at org.eclipse.swt.SWT.error(SWT.java:2616)
> at org.eclipse.swt.SWT.error(SWT.java:2587)
> at org.eclipse.swt.widgets.Widget.error(Widget.java:381)
> at org.eclipse.swt.widgets.Shell.<init>(Shell.java:245)
> at org.eclipse.swt.widgets.Shell.<init>(Shell.java:236)
> at org.eclipse.swt.widgets.Shell.<init>(Shell.java:189)
> at SwtText.createContents(SwtText.java:38)
> at SwtText.open(SwtText.java:28)
> at SwtText.run(SwtText.java:48)
> at java.lang.Thread.run(Thread.java:534)
Re: The Exception when the code that swing call the code that swt [message #461846 is a reply to message #461840] Mon, 03 October 2005 06:00 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: guanxiaofeng_27.126.com

the code that you worte have exception as follow:

java.lang.IllegalStateException: Workbench has not been created yet.
at org.eclipse.ui.PlatformUI.getWorkbench(PlatformUI.java:92)
at Jpro$1.actionPerformed(Jpro.java:48)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButto n.java:1786)
at
javax.swing.AbstractButton$ForwardActionEvents.actionPerform ed(AbstractButton.java:1839)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultBu ttonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel .java:258)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Bas icButtonListener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5100)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container. java:3483)
at java.awt.LightweightDispatcher.processMouseEvent(Container.j ava:3198)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java: 3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventD ispatchThread.java:201)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDis patchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread. java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread. java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:10 0)
Re: The Exception when the code that swing call the code that swt [message #461854 is a reply to message #461846] Mon, 03 October 2005 13:20 Go to previous messageGo to next message
Haris Peco is currently offline Haris PecoFriend
Messages: 1072
Registered: July 2009
Senior Member
This code is for eclipse rcp
If you have standalone swt try this

button.addActionListener(new ActionListener() {
             public void actionPerformed(Acti onEvent e) {
       
                 display.syncExec(new Runn able() {
   public void run () {
                SwtText sw = new SwtText ();
                sw.open();
   }
              }
 }
}

display variable is your Display created before (maybe you have another
name)

xiaofeng wrote:

> the code that you worte have exception as follow:
>
> java.lang.IllegalStateException: Workbench has not been created yet.
> at org.eclipse.ui.PlatformUI.getWorkbench(PlatformUI.java:92)
> at Jpro$1.actionPerformed(Jpro.java:48)
> at
> javax.swing.AbstractButton.fireActionPerformed(AbstractButto n.java:1786)
> at
>
javax.swing.AbstractButton$ForwardActionEvents.actionPerform ed(AbstractButton.java:1839)
> at
>
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultBu ttonModel.java:420)
> at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel .java:258)
> at
>
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Bas icButtonListener.java:245)
> at java.awt.Component.processMouseEvent(Component.java:5100)
> at java.awt.Component.processEvent(Component.java:4897)
> at java.awt.Container.processEvent(Container.java:1569)
> at java.awt.Component.dispatchEventImpl(Component.java:3615)
> at java.awt.Container.dispatchEventImpl(Container.java:1627)
> at java.awt.Component.dispatchEvent(Component.java:3477)
> at java.awt.LightweightDispatcher.retargetMouseEvent(Container. java:3483)
> at java.awt.LightweightDispatcher.processMouseEvent(Container.j ava:3198)
> at java.awt.LightweightDispatcher.dispatchEvent(Container.java: 3128)
> at java.awt.Container.dispatchEventImpl(Container.java:1613)
> at java.awt.Window.dispatchEventImpl(Window.java:1606)
> at java.awt.Component.dispatchEvent(Component.java:3477)
> at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
> at
>
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventD ispatchThread.java:201)
> at
>
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDis patchThread.java:151)
> at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread. java:145)
> at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread. java:137)
> at java.awt.EventDispatchThread.run(EventDispatchThread.java:10 0)
Re: The Exception when the code that swing call the code that swt [message #461857 is a reply to message #461854] Mon, 03 October 2005 15:21 Go to previous message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

You should use asyncExec and not syncExec. That is because the Button's
action listener is running on the AWT UI thread, and then you try to run
something on a different thread (the SWT UI thread). If during the
running of thw SwtText something was required from the AWT ui (such as
loose focus!) the AWT thread is already suspended waiting on the SWT
thread, and then the SWT thread will be suspended waiting on the AWT
thread. You would have a deadlock.

snpe wrote:
> This code is for eclipse rcp
> If you have standalone swt try this
>
> button.addActionListener(new ActionListener() {
> public void actionPerformed(ActionEvent e) {
>
> display.syncExec(new Runnable() {
> public void run () {
> SwtText sw = new SwtText();
> sw.open();
> }
> }
> }
> }

--
Thanks,
Rich Kulp
Previous Topic:getting an Object out of a combo instead of a string
Next Topic:Context sensitive help with the Problems list?
Goto Forum:
  


Current Time: Wed Sep 23 10:12:31 GMT 2020

Powered by FUDForum. Page generated in 0.01991 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top