swt Browser 如何打开新url链接地址
上一篇文章【java swing 内置浏览器打开网页显示flash图表-swt Browser应用】讲解的如何在java中打开网站,但是,在实际开发中右遇到了一个问题就是如何用程序改变已经打开的网页地址,下面把自己解决问题的方法讲解一下。
首先我在程序中直接调用 Browser对象的setUrl方法,代码如下:
browser.setUrl("http://www.y400800.cn");
程序会抛出如下异常:
Exception occurred during event dispatching:
org.eclipse.swt.SWTException: Invalid thread access
at org.eclipse.swt.SWT.error(SWT.java:3884)
at org.eclipse.swt.SWT.error(SWT.java:3799)
at org.eclipse.swt.SWT.error(SWT.java:3770)
at org.eclipse.swt.widgets.Widget.error(Widget.java:463)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:355)
at org.eclipse.swt.browser.Browser.checkWidget(Browser.java:164)
at org.eclipse.swt.browser.Browser.setUrl(Browser.java:973)
at zhinengkaiguanjiankong.comm.SWTPane.StartThread(SWTPane.java:42)
at zhinengkaiguanjiankong.RealTimeSwitchDetailChart.jbtn_searchActionPerformed(RealTimeSwitchDetailChart.java:292)
at zhinengkaiguanjiankong.RealTimeSwitchDetailChart.access$200(RealTimeSwitchDetailChart.java:32)
at zhinengkaiguanjiankong.RealTimeSwitchDetailChart$2.actionPerformed(RealTimeSwitchDetailChart.java:186)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6267)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:178)
at java.awt.Dialog$1.run(Dialog.java:1046)
at java.awt.Dialog$3.run(Dialog.java:1098)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Dialog.show(Dialog.java:1096)
at java.awt.Component.show(Component.java:1563)
at java.awt.Component.setVisible(Component.java:1515)
at java.awt.Window.setVisible(Window.java:842)
at java.awt.Dialog.setVisible(Dialog.java:986)
at zhinengkaiguanjiankong.RealTimeSwitchDetailChart$4.run(RealTimeSwitchDetailChart.java:315)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
该解决方案被否定,以失败告终。
然后又在网上baidu,google了一下,发现问题如下:
在非SWT线程的线程里想要修改SWT界面,都要通过上面类似的办法来做,不然就会报错的,解决方法如下:
Display.getDefault().syncExec(new Runnable() {
public void run() {
browser.setUrl("http://www.my400800.cn");
}
});
最主要是红色字体的地方代码,大家注意一下。