also ich habe eine swing gui in der ich eine SWTBrowserPanel klasse einbinde und nun von aussen also z.b aus der main die methode seturl des SWTBrowser aufrufen möchte.. sofern ich dies jedoch mache,.. wird der browser garnicht mehr fertig geladen und friert einfach ein.. warte ich jedoch ein paar sekunden und versuche dann die seturl zu starten, funktioniert alles
SWTBrowserPanel.java
Main.java
hoffe mir kann einer helfen //micha
SWTBrowserPanel.java
Code:
package com.jsur.utils;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
// TODO: Auto-generated Javadoc
/**
* 16.02.2008 19:25:41 (SCHILLEM, JRE 1.6.0_04)
*/
@SuppressWarnings("serial")
public class SWTBrowserPanel extends JPanel implements ComponentListener {
private Canvas canvas1;
private Shell shell1;
private Browser browser;
/**
* Instantiates a new SWTBrowserPanel.
*/
public SWTBrowserPanel() {
super(new BorderLayout());
initSwtAwtGUI();
addComponentListener(this);
setBorder(BorderFactory.createEmptyBorder(0, 0, 7, 0));
}
/**
* Inits the gui.
*/
private void initGUI() {
try {
canvas1 = new Canvas();
// Make it flexible
JScrollPane scrollPane1 = new JScrollPane(canvas1, JScrollPane.VERTICAL_SCROLLBAR_NEVER,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollPane1, BorderLayout.CENTER);
shell1 = SWT_AWT.new_Shell(Display.getDefault(), canvas1);
FillLayout shell1Layout = new FillLayout();
shell1.setLayout(shell1Layout);
browser = new Browser(shell1, SWT.NULL);
browser.setLayoutData(new GridData(GridData.FILL_BOTH));
setUrl("http://google.de");
} catch (Exception e) {
e.printStackTrace();
}
}
public void setUrl(final String u) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
if (browser != null)
browser.setUrl(u);
}
});
}
// $protect>>$
// ===== start of SWT_AWT special handler code =============
/**
* Inits the swt awt gui.
*/
public void initSwtAwtGUI() {
new DisplayThread().start();
}
/**
* This class makes sure that the SWT controls will be created and behave
* correctly.
*/
private class DisplayThread extends Thread {
/*
* (non-Javadoc)
*
* @see java.lang.Thread#run()
*/
public void run() {
Display.getDefault().syncExec(new Runnable() {
public void run() {
// make sure the GUI is created inside the SWT display thread
// otherwise you will get invalid-thread-access errors, and make
// sure it is visible before calling the SWT_AWT.new_Shell
// method, otherwise a "No handles" error will be thrown.
setVisible(true);
initGUI();
}
});
// "wiggling" the size is one way to make sure that the SWT controls
// are displayed correctly
java.awt.Dimension sz = getSize();
int w = sz.width;
int h = sz.height;
canvas1.setSize(w - 5, h - 12);
validate();
swtEventLoop();
}
/**
* Listen for and dispatch SWT events.
*/
private void swtEventLoop() {
Display display = Display.getDefault();
while (true) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
// ===== end of SWT_AWT special handler code =============
// $protect<<$
/*
* (non-Javadoc)
*
* @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
*/
@Override
public void componentHidden(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
*
* @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
*/
@Override
public void componentMoved(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
*
* @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
*/
@Override
public void componentResized(ComponentEvent arg0) {
// TODO Auto-generated method stub
if (canvas1 != null) {
Dimension sz = getSize();
int w = sz.width;
int h = sz.height;
canvas1.setSize(w - 5, h - 12);
validate();
}
}
/*
* (non-Javadoc)
*
* @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
*/
@Override
public void componentShown(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
}
Main.java
Code:
package com.jsur;
import javax.swing.SwingUtilities;
import com.jsur.gui.BasicWindow;
import com.jsur.utils.SWTBrowserPanel;
import com.jsur.utils.Singleton;
/**
* @author unknown-scm
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
BasicWindow inst = Singleton.getInstance(BasicWindow.class);
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
new Thread() {
public void run() {
while (true){
try {
// Thread.sleep(5000); damit gehts, jedoch keine schöne lösung
Singleton.getInstance(SWTBrowserPanel.class).setUrl("web.de");
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
}
hoffe mir kann einer helfen //micha