Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Swing AWT and SWT Browser(Embedding SWT Browser in AWT Application)
Swing AWT and SWT Browser [message #518364] Wed, 03 March 2010 21:42 Go to next message
PJ is currently offline PJFriend
Messages: 1
Registered: March 2010
Junior Member
Hello,
I have a large Swing (AWT) application that I would liek to launch a SWT Browser from. I am able to create a small test Application where I can create a swt browser that will be inside a JFrame just fine. What I would like to be able to do would be to launch this Jframe from my exisitng Swing AWT Application as if it is another window, have the user interact with the browser and then close the browser and return to the Swing AWT application.

Here is my test code so far:

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


public class MySWTBrowserTest implements ActionListener {
public JButton addCodeButton;
public JButton launchBrowserButton;
public JTextField inputCode;
public JFrame frame;


public MySWTBrowserTest() {
frame = new JFrame("Main Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new FlowLayout());

inputCode = new JTextField(15);
inputCode.setText("999");
addCodeButton = new JButton("Add Code");
addCodeButton.addActionListener(this);
addCodeButton.setActionCommand("addcode");

launchBrowserButton = new JButton("Launch Browser");
launchBrowserButton.addActionListener(this);
launchBrowserButton.setActionCommand("launchbrowser");


mainPanel.add(inputCode);
mainPanel.add(addCodeButton);
mainPanel.add(launchBrowserButton);

frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("addcode")) {

}
else if (e.getActionCommand().equals("launchbrowser")) {
createAndShowBrowser();
}

}

public void createAndShowBrowser() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Canvas canvas = new Canvas();
f.setSize(850,650);
f.getContentPane().add(canvas);

f.setVisible(true);


Display display = new Display();
Shell shell = SWT_AWT.new_Shell(display, canvas);
shell.setSize(800,600);
Browser browser = new Browser(shell, SWT.NONE);
browser.setLayoutData( new GridData(GridData.FILL_BOTH));
browser.setSize(800,600);
browser.setUrl("http://www.google.com");
shell.open();
while(!shell.isDisposed()) {
if(!display.readAndDispatch())
display.sleep();
}
display.dispose();
}


public static void main(String args[]) {
new MySWTBrowserTest();
}
}


The Browser is able to be launched just fine but then it is not possible to interact with the Main Window that launched the browser and neither window is able to be closed.

My hope was to be able to send some data to the browser to populate an initial screen of the browser then be able to have the browser call back into Java to return the results of what the user did in the browser.

Any help would be greatly appreciated!
Re: Swing AWT and SWT Browser [message #519766 is a reply to message #518364] Tue, 09 March 2010 21:23 Go to previous messageGo to next message
Silenio Quarti is currently offline Silenio QuartiFriend
Messages: 31
Registered: July 2009
Member
The problem in your sample is that you are stoping the AWT event loop by running the SWT event loop from the action listener. Eu need to use Display.asyncExec() and EventQueue.invokeLater() to avoid this problem.

Something like this:

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class MySWTBrowserTest implements ActionListener {
public JButton addCodeButton;
public JButton launchBrowserButton;
public JTextField inputCode;
public JFrame frame;
static Display display;

static boolean exit;

public MySWTBrowserTest() {
frame = new JFrame("Main Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new FlowLayout());

inputCode = new JTextField(15);
inputCode.setText("999");
addCodeButton = new JButton("Add Code");
addCodeButton.addActionListener(this);
addCodeButton.setActionCommand("addcode");

launchBrowserButton = new JButton("Launch Browser");
launchBrowserButton.addActionListener(this);
launchBrowserButton.setActionCommand("launchbrowser");

mainPanel.add(inputCode);
mainPanel.add(addCodeButton);
mainPanel.add(launchBrowserButton);

frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("addcode")) {

} else if (e.getActionCommand().equals("launchbrowser")) {
createAndShowBrowser();
}

}

public void createAndShowBrowser() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Canvas canvas = new Canvas();
f.setSize(850, 650);
f.getContentPane().add(canvas);
f.setVisible(true);
display.asyncExec(new Runnable() {
public void run() {
Shell shell = SWT_AWT.new_Shell(display, canvas);
shell.setSize(800, 600);
Browser browser = new Browser(shell, SWT.NONE);
browser.setLayoutData(new GridData(GridData.FILL_BOTH));
browser.setSize(800, 600);
browser.setUrl("http://www.google.com");
shell.open();
}
});
}

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

EventQueue.invokeLater(new Runnable() {
public void run() {
new MySWTBrowserTest();
}
});

while (!exit) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Re: Swing AWT and SWT Browser [message #522146 is a reply to message #519766] Sat, 20 March 2010 13:50 Go to previous messageGo to next message
Grzegorz is currently offline GrzegorzFriend
Messages: 6
Registered: March 2010
Junior Member
Hey,
I don't know why, but both of examples above doesn't work on my OS. I mean - it compiles and launch properly, but after clicking button 'Launch browser', new empty window is being opened.

Do you think that it is a bug? Below is information about my OS:
Linux Ubuntu with kernel 2.6.28-11
swt-3.5.2-gtk-linux-x86
java version "1.6.0_16"

Re: Swing AWT and SWT Browser [message #522681 is a reply to message #522146] Tue, 23 March 2010 10:26 Go to previous message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Thanks for pointing this out, it works on win32 and Cocoa, but not on Linux.
I've logged https://bugs.eclipse.org/bugs/show_bug.cgi?id=306827 .

Grant


"Grzegorz" <gregory.d3@gmail.com> wrote in message
news:ho2js1$u2p$1@build.eclipse.org...
> Hey,
> I don't know why, but both of examples above doesn't work on my OS. I
mean - it compiles and launch properly, but after clicking button 'Launch
browser', new empty window is being opened.
>
> Do you think that it is a bug? Below is information about my OS:
> Linux Ubuntu with kernel 2.6.28-11
> swt-3.5.2-gtk-linux-x86
> java version "1.6.0_16"
>
>
Previous Topic:OLE Understanding Excel Automation
Next Topic:Key Binding problem
Goto Forum:
  


Current Time: Mon Dec 16 04:44:20 GMT 2019

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

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

Back to the top