Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » SWT+Swing hang application
SWT+Swing hang application [message #467108] Wed, 25 January 2006 10:53 Go to next message
Eclipse UserFriend
Originally posted by: nicolas._no.spam_legrain.fr

Hi,
I have a problem using SWT and Swing in an Eclipse RCP application.
We would like to use only swing components in SWT Shell so I create a
method that take a Shell and put a JPanel in.

public void swingInComposite(Shell shell, JPanel swingPanel) {
Composite c = new Composite(shell,SWT.NONE);
FillLayout cLayout = new FillLayout(SWT.HORIZONTAL);
c.setLayout(cLayout);
c.setSize(563, 265);
//Composite
Composite compositeEmbedded = new Composite(c, SWT.EMBEDDED);
FillLayout compositeEmbeddedLayout = new FillLayout(SWT.HORIZONTAL);
compositeEmbedded.setLayout(compositeEmbeddedLayout);
//Frame AWT => heavy weight component to avoid focus problems
Frame frameAWT = SWT_AWT.new_Frame(compositeEmbedded);
compositeEmbedded.setData(frameAWT);
//Panel AWT
Panel panelAWT = new Panel();
BorderLayout panelAWTLayout = new BorderLayout();
panelAWT.setLayout(panelAWTLayout);
frameAWT.add(panelAWT);
//Adding the swing panel
panelAWT.add(swingPanel, BorderLayout.CENTER);
}

If in a such Shell (shell+jpanel) I open a JOptionPane.showConfirmDialog
(...) on the shellClose event my application hang.
If I open the same JOptionPane.showConfirmDialog(...) on an empty Shell,
that's OK. I can make a choice in the JOptionPane and return to the shell.
I don't understand my problem.

Thanks for your help.

Here is a code snippet :
To test the 2 version (hang and don't hang), comment uncomment the call
to swingInComposite(Shell s, JPanel p) in initAndShowUI().
/*********************************************************** *************************************/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;

public class TestShellEvent {
public Shell shell;
public AbstractController controller;

public TestShellEvent() {}

public static void main(String[] args) {
org.eclipse.swt.widgets.Display display = org.eclipse.swt.widgets.Display
.getDefault();
TestShellEvent test = new TestShellEvent();
test.initAndShowUI();
while (!test.shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

//
public void initAndShowUI() {
shell = new Shell();
shell.setSize(600, 600);
shell.setText("Test");

this.controller = new Controller();
shell.addShellListener(new MyShellAdapter());

//comment this for the version that don't hang (empty shell)
swingInComposite(shell,new SampleSwingPanel());

shell.setLayout(new FillLayout());
shell.layout();
shell.setVisible(true);
}

/**
* Embed a JPanel into an SWT Shell
* @param shell
* @param swingPanel
*/
public void swingInComposite(Shell shell, JPanel swingPanel) {
Composite c = new Composite(shell,SWT.NONE);
FillLayout cLayout = new FillLayout(SWT.HORIZONTAL);
c.setLayout(cLayout);
c.setSize(563, 265);
//Composite
Composite compositeEmbedded = new Composite(c, SWT.EMBEDDED);
FillLayout compositeEmbeddedLayout = new FillLayout(SWT.HORIZONTAL);
compositeEmbedded.setLayout(compositeEmbeddedLayout);
//Frame AWT => heavy weight component to avoid focus problems
Frame frameAWT = SWT_AWT.new_Frame(compositeEmbedded);
compositeEmbedded.setData(frameAWT);
//Panel AWT
Panel panelAWT = new Panel();
BorderLayout panelAWTLayout = new BorderLayout();
panelAWT.setLayout(panelAWTLayout);
frameAWT.add(panelAWT);
//Adding the swing panel
panelAWT.add(swingPanel, BorderLayout.CENTER);
}

class MyShellAdapter extends ShellAdapter {
public void shellClosed(ShellEvent e) {
//super.shellClosed(e);
try {
e.doit = controller.onClose();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

}

class SampleSwingPanel extends JPanel{
public SampleSwingPanel() {
this.setLayout(new BorderLayout());
this.add(new JTextField("test"), BorderLayout.NORTH);
}
}

abstract class AbstractController {
abstract public boolean onClose() throws Exception;
}

class Controller extends AbstractController {
public boolean onClose() throws Exception {
if(JOptionPane.showConfirmDialog(null,
"Quit without saving ?",
"Attention", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION)
return true;
else
return false;
}
}
/*********************************************************** *************************************/

I think my "swingInComposite()" method also cause some focus problems
between Swing and SWT.
Re: SWT+Swing hang application [message #467115 is a reply to message #467108] Wed, 25 January 2006 11:34 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: nicolas._no.spam_legrain.fr

The application seems to hang only with this in the "swingInComposite" method :

public void swingInComposite(Shell shell, JPanel swingPanel) {
Composite compositeEmbedded = new Composite(shell, SWT.EMBEDDED);
FillLayout compositeEmbeddedLayout = new FillLayout(SWT.HORIZONTAL);
compositeEmbedded.setLayout(compositeEmbeddedLayout);
//Frame AWT => heavy weight component to avoid focus problems
Frame frameAWT = SWT_AWT.new_Frame(compositeEmbedded);
}

nm a écrit :
> Hi,
> I have a problem using SWT and Swing in an Eclipse RCP application.
> We would like to use only swing components in SWT Shell so I create a
> method that take a Shell and put a JPanel in.
>
> public void swingInComposite(Shell shell, JPanel swingPanel) {
> Composite c = new Composite(shell,SWT.NONE);
> FillLayout cLayout = new FillLayout(SWT.HORIZONTAL);
> c.setLayout(cLayout);
> c.setSize(563, 265);
> //Composite
> Composite compositeEmbedded = new Composite(c, SWT.EMBEDDED);
> FillLayout compositeEmbeddedLayout = new FillLayout(SWT.HORIZONTAL);
> compositeEmbedded.setLayout(compositeEmbeddedLayout);
> //Frame AWT => heavy weight component to avoid focus problems
> Frame frameAWT = SWT_AWT.new_Frame(compositeEmbedded);
> compositeEmbedded.setData(frameAWT);
> //Panel AWT
> Panel panelAWT = new Panel();
> BorderLayout panelAWTLayout = new BorderLayout();
> panelAWT.setLayout(panelAWTLayout);
> frameAWT.add(panelAWT);
> //Adding the swing panel
> panelAWT.add(swingPanel, BorderLayout.CENTER);
> }
>
> If in a such Shell (shell+jpanel) I open a JOptionPane.showConfirmDialog
> (...) on the shellClose event my application hang.
> If I open the same JOptionPane.showConfirmDialog(...) on an empty Shell,
> that's OK. I can make a choice in the JOptionPane and return to the shell.
> I don't understand my problem.
>
> Thanks for your help.
>
> Here is a code snippet :
> To test the 2 version (hang and don't hang), comment uncomment the call
> to swingInComposite(Shell s, JPanel p) in initAndShowUI().
> /*********************************************************** *************************************/
>
> import java.awt.BorderLayout;
> import java.awt.Color;
> import java.awt.Frame;
> import java.awt.Panel;
>
> import javax.swing.JOptionPane;
> import javax.swing.JPanel;
> import javax.swing.JTextField;
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.awt.SWT_AWT;
> import org.eclipse.swt.events.ShellAdapter;
> import org.eclipse.swt.events.ShellEvent;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Shell;
>
> public class TestShellEvent {
> public Shell shell;
> public AbstractController controller;
>
> public TestShellEvent() {}
>
> public static void main(String[] args) {
> org.eclipse.swt.widgets.Display display =
> org.eclipse.swt.widgets.Display
> .getDefault();
> TestShellEvent test = new TestShellEvent();
> test.initAndShowUI();
> while (!test.shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
>
> //
> public void initAndShowUI() {
> shell = new Shell();
> shell.setSize(600, 600);
> shell.setText("Test");
>
> this.controller = new Controller();
> shell.addShellListener(new MyShellAdapter());
>
> //comment this for the version that don't hang (empty shell)
> swingInComposite(shell,new SampleSwingPanel());
>
> shell.setLayout(new FillLayout());
> shell.layout();
> shell.setVisible(true);
> }
>
> /**
> * Embed a JPanel into an SWT Shell
> * @param shell
> * @param swingPanel
> */
> public void swingInComposite(Shell shell, JPanel swingPanel) {
> Composite c = new Composite(shell,SWT.NONE);
> FillLayout cLayout = new FillLayout(SWT.HORIZONTAL);
> c.setLayout(cLayout);
> c.setSize(563, 265);
> //Composite
> Composite compositeEmbedded = new Composite(c, SWT.EMBEDDED);
> FillLayout compositeEmbeddedLayout = new
> FillLayout(SWT.HORIZONTAL);
> compositeEmbedded.setLayout(compositeEmbeddedLayout);
> //Frame AWT => heavy weight component to avoid focus problems
> Frame frameAWT = SWT_AWT.new_Frame(compositeEmbedded);
> compositeEmbedded.setData(frameAWT);
> //Panel AWT
> Panel panelAWT = new Panel();
> BorderLayout panelAWTLayout = new BorderLayout();
> panelAWT.setLayout(panelAWTLayout);
> frameAWT.add(panelAWT);
> //Adding the swing panel
> panelAWT.add(swingPanel, BorderLayout.CENTER);
> }
>
> class MyShellAdapter extends ShellAdapter {
> public void shellClosed(ShellEvent e) {
> //super.shellClosed(e);
> try {
> e.doit = controller.onClose();
> } catch (Exception ex) {
> ex.printStackTrace();
> }
> }
> }
>
> }
>
> class SampleSwingPanel extends JPanel{
> public SampleSwingPanel() {
> this.setLayout(new BorderLayout());
> this.add(new JTextField("test"), BorderLayout.NORTH);
> }
> }
>
> abstract class AbstractController {
> abstract public boolean onClose() throws Exception;
> }
>
> class Controller extends AbstractController {
> public boolean onClose() throws Exception {
> if(JOptionPane.showConfirmDialog(null,
> "Quit without saving ?",
> "Attention", JOptionPane.YES_NO_OPTION,
> JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION)
> return true;
> else
> return false;
> }
> }
> /*********************************************************** *************************************/
>
>
> I think my "swingInComposite()" method also cause some focus problems
> between Swing and SWT.
Re: SWT+Swing hang application [message #467121 is a reply to message #467108] Wed, 25 January 2006 15:54 Go to previous messageGo to next message
Haris Peco is currently offline Haris PecoFriend
Messages: 1072
Registered: July 2009
Senior Member
do you try add AWT frame like Snippet135
nm wrote:

> Hi,
> I have a problem using SWT and Swing in an Eclipse RCP application.
> We would like to use only swing components in SWT Shell so I create a
> method that take a Shell and put a JPanel in.
>
> public void swingInComposite(Shell shell, JPanel swingPanel) {
> Composite c = new Composite(shell,SWT.NONE);
> FillLayout cLayout = new FillLayout(SWT.HORIZONTAL);
> c.setLayout(cLayout);
> c.setSize(563, 265);
> //Composite
> Composite compositeEmbedded = new Composite(c, SWT.EMBEDDED);
> FillLayout compositeEmbeddedLayout = new FillLayout(SWT.HORIZONTAL);
> compositeEmbedded.setLayout(compositeEmbeddedLayout);
> //Frame AWT => heavy weight component to avoid focus problems
> Frame frameAWT = SWT_AWT.new_Frame(compositeEmbedded);
> compositeEmbedded.setData(frameAWT);
> //Panel AWT
> Panel panelAWT = new Panel();
> BorderLayout panelAWTLayout = new BorderLayout();
> panelAWT.setLayout(panelAWTLayout);
> frameAWT.add(panelAWT);
> //Adding the swing panel
> panelAWT.add(swingPanel, BorderLayout.CENTER);
> }
>
> If in a such Shell (shell+jpanel) I open a JOptionPane.showConfirmDialog
> (...) on the shellClose event my application hang.
> If I open the same JOptionPane.showConfirmDialog(...) on an empty Shell,
> that's OK. I can make a choice in the JOptionPane and return to the shell.
> I don't understand my problem.
>
> Thanks for your help.
>
> Here is a code snippet :
> To test the 2 version (hang and don't hang), comment uncomment the call
> to swingInComposite(Shell s, JPanel p) in initAndShowUI().
> /*********************************************************** *************************************/
> import java.awt.BorderLayout;
> import java.awt.Color;
> import java.awt.Frame;
> import java.awt.Panel;
>
> import javax.swing.JOptionPane;
> import javax.swing.JPanel;
> import javax.swing.JTextField;
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.awt.SWT_AWT;
> import org.eclipse.swt.events.ShellAdapter;
> import org.eclipse.swt.events.ShellEvent;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Shell;
>
> public class TestShellEvent {
> public Shell shell;
> public AbstractController controller;
>
> public TestShellEvent() {}
>
> public static void main(String[] args) {
> org.eclipse.swt.widgets.Display display = org.eclipse.swt.widgets.Display
> .getDefault();
> TestShellEvent test = new TestShellEvent();
> test.initAndShowUI();
> while (!test.shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
>
> //
> public void initAndShowUI() {
> shell = new Shell();
> shell.setSize(600, 600);
> shell.setText("Test");
>
> this.controller = new Controller();
> shell.addShellListener(new MyShellAdapter());
>
> //comment this for the version that don't hang (empty shell)
> swingInComposite(shell,new SampleSwingPanel());
>
> shell.setLayout(new FillLayout());
> shell.layout();
> shell.setVisible(true);
> }
>
> /**
> * Embed a JPanel into an SWT Shell
> * @param shell
> * @param swingPanel
> */
> public void swingInComposite(Shell shell, JPanel swingPanel) {
> Composite c = new Composite(shell,SWT.NONE);
> FillLayout cLayout = new FillLayout(SWT.HORIZONTAL);
> c.setLayout(cLayout);
> c.setSize(563, 265);
> //Composite
> Composite compositeEmbedded = new Composite(c, SWT.EMBEDDED);
> FillLayout compositeEmbeddedLayout = new FillLayout(SWT.HORIZONTAL);
> compositeEmbedded.setLayout(compositeEmbeddedLayout);
> //Frame AWT => heavy weight component to avoid focus problems
> Frame frameAWT = SWT_AWT.new_Frame(compositeEmbedded);
> compositeEmbedded.setData(frameAWT);
> //Panel AWT
> Panel panelAWT = new Panel();
> BorderLayout panelAWTLayout = new BorderLayout();
> panelAWT.setLayout(panelAWTLayout);
> frameAWT.add(panelAWT);
> //Adding the swing panel
> panelAWT.add(swingPanel, BorderLayout.CENTER);
> }
>
> class MyShellAdapter extends ShellAdapter {
> public void shellClosed(ShellEvent e) {
> //super.shellClosed(e);
> try {
> e.doit = controller.onClose();
> } catch (Exception ex) {
> ex.printStackTrace();
> }
> }
> }
>
> }
>
> class SampleSwingPanel extends JPanel{
> public SampleSwingPanel() {
> this.setLayout(new BorderLayout());
> this.add(new JTextField("test"), BorderLayout.NORTH);
> }
> }
>
> abstract class AbstractController {
> abstract public boolean onClose() throws Exception;
> }
>
> class Controller extends AbstractController {
> public boolean onClose() throws Exception {
> if(JOptionPane.showConfirmDialog(null,
> "Quit without saving ?",
> "Attention", JOptionPane.YES_NO_OPTION,
> JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION)
> return true;
> else
> return false;
> }
> }
> /*********************************************************** *************************************/
>
> I think my "swingInComposite()" method also cause some focus problems
> between Swing and SWT.
Re: SWT+Swing hang application [message #467133 is a reply to message #467121] Wed, 25 January 2006 16:38 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: nicolas._no.spam_legrain.fr

Thanks for your answer.
After looking the snippet 135, I haven't seen major difference between this code
and my code. They use SWT_AWT.new_Frame() to obtain a Frame then directly add
AWT component or add a Panel before if they want to add a Swing component.
If in "swingInComposite()" there is only
public void swingInComposite(Shell shell, JPanel swingPanel) {
Frame frameAWT = SWT_AWT.new_Frame(shell);
}
the application hangs when I open the JOptionPane (in the shellClosed event).
I've tried with other JDialog and the application only hangs if the JDialog
is modal (like the JOptionPane).
I think it is a threading issue.

Haris Peco a écrit :
> do you try add AWT frame like Snippet135
> nm wrote:
>
>> Hi,
>> I have a problem using SWT and Swing in an Eclipse RCP application.
>> We would like to use only swing components in SWT Shell so I create a
>> method that take a Shell and put a JPanel in.
>>
>> public void swingInComposite(Shell shell, JPanel swingPanel) {
>> Composite c = new Composite(shell,SWT.NONE);
>> FillLayout cLayout = new FillLayout(SWT.HORIZONTAL);
>> c.setLayout(cLayout);
>> c.setSize(563, 265);
>> //Composite
>> Composite compositeEmbedded = new Composite(c, SWT.EMBEDDED);
>> FillLayout compositeEmbeddedLayout = new FillLayout(SWT.HORIZONTAL);
>> compositeEmbedded.setLayout(compositeEmbeddedLayout);
>> //Frame AWT => heavy weight component to avoid focus problems
>> Frame frameAWT = SWT_AWT.new_Frame(compositeEmbedded);
>> compositeEmbedded.setData(frameAWT);
>> //Panel AWT
>> Panel panelAWT = new Panel();
>> BorderLayout panelAWTLayout = new BorderLayout();
>> panelAWT.setLayout(panelAWTLayout);
>> frameAWT.add(panelAWT);
>> //Adding the swing panel
>> panelAWT.add(swingPanel, BorderLayout.CENTER);
>> }
>>
>> If in a such Shell (shell+jpanel) I open a JOptionPane.showConfirmDialog
>> (...) on the shellClose event my application hang.
>> If I open the same JOptionPane.showConfirmDialog(...) on an empty Shell,
>> that's OK. I can make a choice in the JOptionPane and return to the shell.
>> I don't understand my problem.
>>
>> Thanks for your help.
>>
>> Here is a code snippet :
>> To test the 2 version (hang and don't hang), comment uncomment the call
>> to swingInComposite(Shell s, JPanel p) in initAndShowUI().
>> /*********************************************************** *************************************/
>> import java.awt.BorderLayout;
>> import java.awt.Color;
>> import java.awt.Frame;
>> import java.awt.Panel;
>>
>> import javax.swing.JOptionPane;
>> import javax.swing.JPanel;
>> import javax.swing.JTextField;
>>
>> import org.eclipse.swt.SWT;
>> import org.eclipse.swt.awt.SWT_AWT;
>> import org.eclipse.swt.events.ShellAdapter;
>> import org.eclipse.swt.events.ShellEvent;
>> import org.eclipse.swt.layout.FillLayout;
>> import org.eclipse.swt.widgets.Composite;
>> import org.eclipse.swt.widgets.Shell;
>>
>> public class TestShellEvent {
>> public Shell shell;
>> public AbstractController controller;
>>
>> public TestShellEvent() {}
>>
>> public static void main(String[] args) {
>> org.eclipse.swt.widgets.Display display = org.eclipse.swt.widgets.Display
>> .getDefault();
>> TestShellEvent test = new TestShellEvent();
>> test.initAndShowUI();
>> while (!test.shell.isDisposed()) {
>> if (!display.readAndDispatch())
>> display.sleep();
>> }
>> display.dispose();
>> }
>>
>> //
>> public void initAndShowUI() {
>> shell = new Shell();
>> shell.setSize(600, 600);
>> shell.setText("Test");
>>
>> this.controller = new Controller();
>> shell.addShellListener(new MyShellAdapter());
>>
>> //comment this for the version that don't hang (empty shell)
>> swingInComposite(shell,new SampleSwingPanel());
>>
>> shell.setLayout(new FillLayout());
>> shell.layout();
>> shell.setVisible(true);
>> }
>>
>> /**
>> * Embed a JPanel into an SWT Shell
>> * @param shell
>> * @param swingPanel
>> */
>> public void swingInComposite(Shell shell, JPanel swingPanel) {
>> Composite c = new Composite(shell,SWT.NONE);
>> FillLayout cLayout = new FillLayout(SWT.HORIZONTAL);
>> c.setLayout(cLayout);
>> c.setSize(563, 265);
>> //Composite
>> Composite compositeEmbedded = new Composite(c, SWT.EMBEDDED);
>> FillLayout compositeEmbeddedLayout = new FillLayout(SWT.HORIZONTAL);
>> compositeEmbedded.setLayout(compositeEmbeddedLayout);
>> //Frame AWT => heavy weight component to avoid focus problems
>> Frame frameAWT = SWT_AWT.new_Frame(compositeEmbedded);
>> compositeEmbedded.setData(frameAWT);
>> //Panel AWT
>> Panel panelAWT = new Panel();
>> BorderLayout panelAWTLayout = new BorderLayout();
>> panelAWT.setLayout(panelAWTLayout);
>> frameAWT.add(panelAWT);
>> //Adding the swing panel
>> panelAWT.add(swingPanel, BorderLayout.CENTER);
>> }
>>
>> class MyShellAdapter extends ShellAdapter {
>> public void shellClosed(ShellEvent e) {
>> //super.shellClosed(e);
>> try {
>> e.doit = controller.onClose();
>> } catch (Exception ex) {
>> ex.printStackTrace();
>> }
>> }
>> }
>>
>> }
>>
>> class SampleSwingPanel extends JPanel{
>> public SampleSwingPanel() {
>> this.setLayout(new BorderLayout());
>> this.add(new JTextField("test"), BorderLayout.NORTH);
>> }
>> }
>>
>> abstract class AbstractController {
>> abstract public boolean onClose() throws Exception;
>> }
>>
>> class Controller extends AbstractController {
>> public boolean onClose() throws Exception {
>> if(JOptionPane.showConfirmDialog(null,
>> "Quit without saving ?",
>> "Attention", JOptionPane.YES_NO_OPTION,
>> JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION)
>> return true;
>> else
>> return false;
>> }
>> }
>> /*********************************************************** *************************************/
>>
>> I think my "swingInComposite()" method also cause some focus problems
>> between Swing and SWT.
>
Re: SWT+Swing hang application [message #467139 is a reply to message #467133] Wed, 25 January 2006 18:34 Go to previous messageGo to next message
Haris Peco is currently offline Haris PecoFriend
Messages: 1072
Registered: July 2009
Senior Member
what mean 'application hang'
you have NPE or what ?
nm wrote:

> Thanks for your answer.
> After looking the snippet 135, I haven't seen major difference between
> this code and my code. They use SWT_AWT.new_Frame() to obtain a Frame then
> directly add AWT component or add a Panel before if they want to add a
> Swing component. If in "swingInComposite()" there is only
> public void swingInComposite(Shell shell, JPanel swingPanel) {
> Frame frameAWT = SWT_AWT.new_Frame(shell);
> }
> the application hangs when I open the JOptionPane (in the shellClosed
> event). I've tried with other JDialog and the application only hangs if
> the JDialog is modal (like the JOptionPane).
> I think it is a threading issue.
>
> Haris Peco a écrit :
>> do you try add AWT frame like Snippet135
>> nm wrote:
>>
>>> Hi,
>>> I have a problem using SWT and Swing in an Eclipse RCP application.
>>> We would like to use only swing components in SWT Shell so I create a
>>> method that take a Shell and put a JPanel in.
>>>
>>> public void swingInComposite(Shell shell, JPanel swingPanel) {
>>> Composite c = new Composite(shell,SWT.NONE);
>>> FillLayout cLayout = new FillLayout(SWT.HORIZONTAL);
>>> c.setLayout(cLayout);
>>> c.setSize(563, 265);
>>> //Composite
>>> Composite compositeEmbedded = new Composite(c, SWT.EMBEDDED);
>>> FillLayout compositeEmbeddedLayout = new FillLayout(SWT.HORIZONTAL);
>>> compositeEmbedded.setLayout(compositeEmbeddedLayout);
>>> //Frame AWT => heavy weight component to avoid focus problems
>>> Frame frameAWT = SWT_AWT.new_Frame(compositeEmbedded);
>>> compositeEmbedded.setData(frameAWT);
>>> //Panel AWT
>>> Panel panelAWT = new Panel();
>>> BorderLayout panelAWTLayout = new BorderLayout();
>>> panelAWT.setLayout(panelAWTLayout);
>>> frameAWT.add(panelAWT);
>>> //Adding the swing panel
>>> panelAWT.add(swingPanel, BorderLayout.CENTER);
>>> }
>>>
>>> If in a such Shell (shell+jpanel) I open a JOptionPane.showConfirmDialog
>>> (...) on the shellClose event my application hang.
>>> If I open the same JOptionPane.showConfirmDialog(...) on an empty Shell,
>>> that's OK. I can make a choice in the JOptionPane and return to the
>>> shell. I don't understand my problem.
>>>
>>> Thanks for your help.
>>>
>>> Here is a code snippet :
>>> To test the 2 version (hang and don't hang), comment uncomment the call
>>> to swingInComposite(Shell s, JPanel p) in initAndShowUI().
>>> /*********************************************************** *************************************/
>>> import java.awt.BorderLayout;
>>> import java.awt.Color;
>>> import java.awt.Frame;
>>> import java.awt.Panel;
>>>
>>> import javax.swing.JOptionPane;
>>> import javax.swing.JPanel;
>>> import javax.swing.JTextField;
>>>
>>> import org.eclipse.swt.SWT;
>>> import org.eclipse.swt.awt.SWT_AWT;
>>> import org.eclipse.swt.events.ShellAdapter;
>>> import org.eclipse.swt.events.ShellEvent;
>>> import org.eclipse.swt.layout.FillLayout;
>>> import org.eclipse.swt.widgets.Composite;
>>> import org.eclipse.swt.widgets.Shell;
>>>
>>> public class TestShellEvent {
>>> public Shell shell;
>>> public AbstractController controller;
>>>
>>> public TestShellEvent() {}
>>>
>>> public static void main(String[] args) {
>>> org.eclipse.swt.widgets.Display display =
>>> org.eclipse.swt.widgets.Display .getDefault();
>>> TestShellEvent test = new TestShellEvent();
>>> test.initAndShowUI();
>>> while (!test.shell.isDisposed()) {
>>> if (!display.readAndDispatch())
>>> display.sleep();
>>> }
>>> display.dispose();
>>> }
>>>
>>> //
>>> public void initAndShowUI() {
>>> shell = new Shell();
>>> shell.setSize(600, 600);
>>> shell.setText("Test");
>>>
>>> this.controller = new Controller();
>>> shell.addShellListener(new MyShellAdapter());
>>>
>>> //comment this for the version that don't hang (empty shell)
>>> swingInComposite(shell,new SampleSwingPanel());
>>>
>>> shell.setLayout(new FillLayout());
>>> shell.layout();
>>> shell.setVisible(true);
>>> }
>>>
>>> /**
>>> * Embed a JPanel into an SWT Shell
>>> * @param shell
>>> * @param swingPanel
>>> */
>>> public void swingInComposite(Shell shell, JPanel swingPanel) {
>>> Composite c = new Composite(shell,SWT.NONE);
>>> FillLayout cLayout = new FillLayout(SWT.HORIZONTAL);
>>> c.setLayout(cLayout);
>>> c.setSize(563, 265);
>>> //Composite
>>> Composite compositeEmbedded = new Composite(c, SWT.EMBEDDED);
>>> FillLayout compositeEmbeddedLayout = new FillLayout(SWT.HORIZONTAL);
>>> compositeEmbedded.setLayout(compositeEmbeddedLayout);
>>> //Frame AWT => heavy weight component to avoid focus problems
>>> Frame frameAWT = SWT_AWT.new_Frame(compositeEmbedded);
>>> compositeEmbedded.setData(frameAWT);
>>> //Panel AWT
>>> Panel panelAWT = new Panel();
>>> BorderLayout panelAWTLayout = new BorderLayout();
>>> panelAWT.setLayout(panelAWTLayout);
>>> frameAWT.add(panelAWT);
>>> //Adding the swing panel
>>> panelAWT.add(swingPanel, BorderLayout.CENTER);
>>> }
>>>
>>> class MyShellAdapter extends ShellAdapter {
>>> public void shellClosed(ShellEvent e) {
>>> //super.shellClosed(e);
>>> try {
>>> e.doit = controller.onClose();
>>> } catch (Exception ex) {
>>> ex.printStackTrace();
>>> }
>>> }
>>> }
>>>
>>> }
>>>
>>> class SampleSwingPanel extends JPanel{
>>> public SampleSwingPanel() {
>>> this.setLayout(new BorderLayout());
>>> this.add(new JTextField("test"), BorderLayout.NORTH);
>>> }
>>> }
>>>
>>> abstract class AbstractController {
>>> abstract public boolean onClose() throws Exception;
>>> }
>>>
>>> class Controller extends AbstractController {
>>> public boolean onClose() throws Exception {
>>> if(JOptionPane.showConfirmDialog(null,
>>> "Quit without saving ?",
>>> "Attention", JOptionPane.YES_NO_OPTION,
>>> JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION)
>>> return true;
>>> else
>>> return false;
>>> }
>>> }
>>> /*********************************************************** *************************************/
>>>
>>> I think my "swingInComposite()" method also cause some focus problems
>>> between Swing and SWT.
>>
Re: SWT+Swing hang application [message #467143 is a reply to message #467139] Wed, 25 January 2006 18:05 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: nicolas._no.spam_legrain.fr

The GUI freeze and there is no error in the console.
I only can move the windows of my application (very slowly) and reduce/retorre
them on the task bar.
After the problem, the only way to close the program is Ctrl+Alt+Suppr

Haris Peco a écrit :
> what mean 'application hang'
> you have NPE or what ?
> nm wrote:
>
>> Thanks for your answer.
>> After looking the snippet 135, I haven't seen major difference between
>> this code and my code. They use SWT_AWT.new_Frame() to obtain a Frame then
>> directly add AWT component or add a Panel before if they want to add a
>> Swing component. If in "swingInComposite()" there is only
>> public void swingInComposite(Shell shell, JPanel swingPanel) {
>> Frame frameAWT = SWT_AWT.new_Frame(shell);
>> }
>> the application hangs when I open the JOptionPane (in the shellClosed
>> event). I've tried with other JDialog and the application only hangs if
>> the JDialog is modal (like the JOptionPane).
>> I think it is a threading issue.
>>
>> Haris Peco a écrit :
>>> do you try add AWT frame like Snippet135
>>> nm wrote:
>>>
>>>> Hi,
>>>> I have a problem using SWT and Swing in an Eclipse RCP application.
>>>> We would like to use only swing components in SWT Shell so I create a
>>>> method that take a Shell and put a JPanel in.
>>>>
>>>> public void swingInComposite(Shell shell, JPanel swingPanel) {
>>>> Composite c = new Composite(shell,SWT.NONE);
>>>> FillLayout cLayout = new FillLayout(SWT.HORIZONTAL);
>>>> c.setLayout(cLayout);
>>>> c.setSize(563, 265);
>>>> //Composite
>>>> Composite compositeEmbedded = new Composite(c, SWT.EMBEDDED);
>>>> FillLayout compositeEmbeddedLayout = new FillLayout(SWT.HORIZONTAL);
>>>> compositeEmbedded.setLayout(compositeEmbeddedLayout);
>>>> //Frame AWT => heavy weight component to avoid focus problems
>>>> Frame frameAWT = SWT_AWT.new_Frame(compositeEmbedded);
>>>> compositeEmbedded.setData(frameAWT);
>>>> //Panel AWT
>>>> Panel panelAWT = new Panel();
>>>> BorderLayout panelAWTLayout = new BorderLayout();
>>>> panelAWT.setLayout(panelAWTLayout);
>>>> frameAWT.add(panelAWT);
>>>> //Adding the swing panel
>>>> panelAWT.add(swingPanel, BorderLayout.CENTER);
>>>> }
>>>>
>>>> If in a such Shell (shell+jpanel) I open a JOptionPane.showConfirmDialog
>>>> (...) on the shellClose event my application hang.
>>>> If I open the same JOptionPane.showConfirmDialog(...) on an empty Shell,
>>>> that's OK. I can make a choice in the JOptionPane and return to the
>>>> shell. I don't understand my problem.
>>>>
>>>> Thanks for your help.
>>>>
>>>> Here is a code snippet :
>>>> To test the 2 version (hang and don't hang), comment uncomment the call
>>>> to swingInComposite(Shell s, JPanel p) in initAndShowUI().
>>>> /*********************************************************** *************************************/
>>>> import java.awt.BorderLayout;
>>>> import java.awt.Color;
>>>> import java.awt.Frame;
>>>> import java.awt.Panel;
>>>>
>>>> import javax.swing.JOptionPane;
>>>> import javax.swing.JPanel;
>>>> import javax.swing.JTextField;
>>>>
>>>> import org.eclipse.swt.SWT;
>>>> import org.eclipse.swt.awt.SWT_AWT;
>>>> import org.eclipse.swt.events.ShellAdapter;
>>>> import org.eclipse.swt.events.ShellEvent;
>>>> import org.eclipse.swt.layout.FillLayout;
>>>> import org.eclipse.swt.widgets.Composite;
>>>> import org.eclipse.swt.widgets.Shell;
>>>>
>>>> public class TestShellEvent {
>>>> public Shell shell;
>>>> public AbstractController controller;
>>>>
>>>> public TestShellEvent() {}
>>>>
>>>> public static void main(String[] args) {
>>>> org.eclipse.swt.widgets.Display display =
>>>> org.eclipse.swt.widgets.Display .getDefault();
>>>> TestShellEvent test = new TestShellEvent();
>>>> test.initAndShowUI();
>>>> while (!test.shell.isDisposed()) {
>>>> if (!display.readAndDispatch())
>>>> display.sleep();
>>>> }
>>>> display.dispose();
>>>> }
>>>>
>>>> //
>>>> public void initAndShowUI() {
>>>> shell = new Shell();
>>>> shell.setSize(600, 600);
>>>> shell.setText("Test");
>>>>
>>>> this.controller = new Controller();
>>>> shell.addShellListener(new MyShellAdapter());
>>>>
>>>> //comment this for the version that don't hang (empty shell)
>>>> swingInComposite(shell,new SampleSwingPanel());
>>>>
>>>> shell.setLayout(new FillLayout());
>>>> shell.layout();
>>>> shell.setVisible(true);
>>>> }
>>>>
>>>> /**
>>>> * Embed a JPanel into an SWT Shell
>>>> * @param shell
>>>> * @param swingPanel
>>>> */
>>>> public void swingInComposite(Shell shell, JPanel swingPanel) {
>>>> Composite c = new Composite(shell,SWT.NONE);
>>>> FillLayout cLayout = new FillLayout(SWT.HORIZONTAL);
>>>> c.setLayout(cLayout);
>>>> c.setSize(563, 265);
>>>> //Composite
>>>> Composite compositeEmbedded = new Composite(c, SWT.EMBEDDED);
>>>> FillLayout compositeEmbeddedLayout = new FillLayout(SWT.HORIZONTAL);
>>>> compositeEmbedded.setLayout(compositeEmbeddedLayout);
>>>> //Frame AWT => heavy weight component to avoid focus problems
>>>> Frame frameAWT = SWT_AWT.new_Frame(compositeEmbedded);
>>>> compositeEmbedded.setData(frameAWT);
>>>> //Panel AWT
>>>> Panel panelAWT = new Panel();
>>>> BorderLayout panelAWTLayout = new BorderLayout();
>>>> panelAWT.setLayout(panelAWTLayout);
>>>> frameAWT.add(panelAWT);
>>>> //Adding the swing panel
>>>> panelAWT.add(swingPanel, BorderLayout.CENTER);
>>>> }
>>>>
>>>> class MyShellAdapter extends ShellAdapter {
>>>> public void shellClosed(ShellEvent e) {
>>>> //super.shellClosed(e);
>>>> try {
>>>> e.doit = controller.onClose();
>>>> } catch (Exception ex) {
>>>> ex.printStackTrace();
>>>> }
>>>> }
>>>> }
>>>>
>>>> }
>>>>
>>>> class SampleSwingPanel extends JPanel{
>>>> public SampleSwingPanel() {
>>>> this.setLayout(new BorderLayout());
>>>> this.add(new JTextField("test"), BorderLayout.NORTH);
>>>> }
>>>> }
>>>>
>>>> abstract class AbstractController {
>>>> abstract public boolean onClose() throws Exception;
>>>> }
>>>>
>>>> class Controller extends AbstractController {
>>>> public boolean onClose() throws Exception {
>>>> if(JOptionPane.showConfirmDialog(null,
>>>> "Quit without saving ?",
>>>> "Attention", JOptionPane.YES_NO_OPTION,
>>>> JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION)
>>>> return true;
>>>> else
>>>> return false;
>>>> }
>>>> }
>>>> /*********************************************************** *************************************/
>>>>
>>>> I think my "swingInComposite()" method also cause some focus problems
>>>> between Swing and SWT.
>
Re: SWT+Swing hang application [message #467147 is a reply to message #467133] Wed, 25 January 2006 18:50 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

That makes some sense. There are two threads running, one is the main
SWT display thread, and then the other thread is the AWT's display
thread. When you open a modal Swing dialog from the Shell close event,
you are blocking the SWT's display thread because the modal open happens
on the SWT's thread. But the dialog is processed on the AWT's display
thread. Since the dialog doesn't return until it is closed the SWT
thread cannot process any events and so it locks up.


--
Thanks,
Rich Kulp
Re: SWT+Swing hang application [message #467159 is a reply to message #467147] Wed, 25 January 2006 22:22 Go to previous messageGo to next message
Haris Peco is currently offline Haris PecoFriend
Messages: 1072
Registered: July 2009
Senior Member
Rich Kulp wrote:

> That makes some sense. There are two threads running, one is the main
> SWT display thread, and then the other thread is the AWT's display
> thread. When you open a modal Swing dialog from the Shell close event,
> you are blocking the SWT's display thread because the modal open happens
> on the SWT's thread. But the dialog is processed on the AWT's display
> thread. Since the dialog doesn't return until it is closed the SWT
> thread cannot process any events and so it locks up.
>
>
and what is solution - change swing with swt dialog or call dialog with
async ?
Re: SWT+Swing hang application [message #467163 is a reply to message #467159] Wed, 25 January 2006 22:12 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

You would do:

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
do your JOptions dialog
process returned result;
}
});

Note that this means that the SWT shell's close event will continue
processing and the shell will close before the JOptions dialog comes up.
You won't be able to stop it from closing. You can't stop the SWT
display queue to wait for the JOptions dialog.

If you absolutely must do that, then there is a trick. You just run the
SWT display thread yourself. (I think this will work).

public boolean onClose() throws Exception {
final Display display = Display.getCurrent();
final boolean[] runLoopAndContinue =
new boolean[] {true, true};
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
runLoopAndContinue[1] =
(JOptionPane.showConfirmDialog(null,
"Quit without saving ?",
"Attention", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION);
// Do syncExec so that display queue is emptied and entry
// [0] changed on SWT UI thread.
display.syncExec(new Runnable() {
public run() {
runLoopAndContinue[0] = false; // Tell onClose to stop
looping.
}
});
display.asyncExec(null); // Wake SWT UI thread up one more
time to make sure it terminates.
}
});

while(runLoopAndContinue[0]) {
try {
if (!display.readAndDispatch())
display.sleep();
} catch (Throwable e) {
// process exception if you like.
}
}
return runLoopAndContinue[1];
}


--
Thanks,
Rich Kulp
Re: SWT+Swing hang application [message #467200 is a reply to message #467163] Thu, 26 January 2006 10:13 Go to previous message
Eclipse UserFriend
Originally posted by: nicolas._no.spam_legrain.fr

Thank you for the informations, your trick is working.
I will try with it.

Rich Kulp a écrit :
> You would do:
>
> java.awt.EventQueue.invokeLater(new Runnable() {
> public void run() {
> do your JOptions dialog
> process returned result;
> }
> });
>
> Note that this means that the SWT shell's close event will continue
> processing and the shell will close before the JOptions dialog comes up.
> You won't be able to stop it from closing. You can't stop the SWT
> display queue to wait for the JOptions dialog.
>
> If you absolutely must do that, then there is a trick. You just run the
> SWT display thread yourself. (I think this will work).
>
> public boolean onClose() throws Exception {
> final Display display = Display.getCurrent();
> final boolean[] runLoopAndContinue =
> new boolean[] {true, true};
> java.awt.EventQueue.invokeLater(new Runnable() {
> public void run() {
> runLoopAndContinue[1] =
> (JOptionPane.showConfirmDialog(null,
> "Quit without saving ?",
> "Attention", JOptionPane.YES_NO_OPTION,
> JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION);
> // Do syncExec so that display queue is emptied and entry
> // [0] changed on SWT UI thread.
> display.syncExec(new Runnable() {
> public run() {
> runLoopAndContinue[0] = false; // Tell onClose to
> stop looping.
> }
> });
> display.asyncExec(null); // Wake SWT UI thread up one more
> time to make sure it terminates.
> }
> });
>
> while(runLoopAndContinue[0]) {
> try {
> if (!display.readAndDispatch())
> display.sleep();
> } catch (Throwable e) {
> // process exception if you like.
> }
> }
> return runLoopAndContinue[1];
> }
>
>
Previous Topic:ImageLoader save()
Next Topic:Frame being painted several times during resize
Goto Forum:
  


Current Time: Thu Mar 28 23:26:41 GMT 2024

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

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

Back to the top