我正在为idea构建一个插件。一般来说,我的插件包含一个工具窗口。toolwindow包含jfxpanel,其中包含从url加载其内容的javafx.scene.web.webview。
我面临的问题是,当我第一次调试并选择webview toolwindow标记时,javafx webview可以正常显示,但当我切换到其他标记,如“termainal”,然后切换回webview时,javafx webview无法正常显示,出现如下异常:
2021-06-09 13:00:41,176 [ 31967] ERROR - ellij.util.text.DateFormatUtil - IntelliJ IDEA 2019.3 Build #IC-193.5233.102
2021-06-09 13:00:41,176 [ 31967] ERROR - ellij.util.text.DateFormatUtil - JDK: 11.0.4; VM: OpenJDK 64-Bit Server VM; Vendor: JetBrains s.r.o
2021-06-09 13:00:41,176 [ 31967] ERROR - ellij.util.text.DateFormatUtil - OS: Mac OS X
2021-06-09 13:00:41,180 [ 31971] ERROR - ellij.util.text.DateFormatUtil - Last Action:
2021-06-09 13:00:49,549 [ 40340] ERROR - terminal.emulator.JediEmulator - Error processing OSC 1;..ts/PluginOpen
2021-06-09 13:00:49,563 [ 40354] ERROR - terminal.emulator.JediEmulator - Unhandled Control sequence
parsed :ESC[?2004h
bytes read :ESC[
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at javafx.graphics/com.sun.glass.ui.Screen.initScreens(Screen.java:412)
at javafx.graphics/com.sun.glass.ui.Screen.notifySettingsChanged(Screen.java:381)
public class CJDesignerToolWindow implements ToolWindowFactory {
private final MyBrowser myBrowser = new MyBrowser();
@Override
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
JComponent component = toolWindow.getComponent();
component.getParent().add(myBrowser);
load();
}
public void load() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
myBrowser.setVisible(true);
myBrowser.loadURL("www.baidu.com");
}
});
}
}
这是webview定义
package com.fss.webview;
import javax.swing.*;
import com.fss.biz.handler.JavaClassInfoParseHandler;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.embed.swing.JFXPanel;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import netscape.javascript.JSObject;
import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import java.net.URL;
import static javafx.concurrent.Worker.State.FAILED;
/**
* @author fangshusen
*/
public class MyBrowser extends JPanel {
private final JFXPanel webView = new JFXPanel();
private WebEngine engine;
private final JPanel nestedBrowser = new JPanel(new BorderLayout());
private final JLabel lblStatus = new JLabel();
private final JButton btnGo = new JButton("Go");
private final JTextField txtURL = new JTextField();
private final JProgressBar progressBar = new JProgressBar();
JavaClassInfoParseHandler javaSourceHandler = new JavaClassInfoParseHandler();
public MyBrowser() {
super();
initComponents();
}
private void initComponents() {
createScene();
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loadURL(txtURL.getText());
}
};
btnGo.addActionListener(al);
txtURL.addActionListener(al);
progressBar.setPreferredSize(new Dimension(150, 18));
progressBar.setStringPainted(true);
JPanel topBar = new JPanel(new BorderLayout(5, 0));
topBar.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
topBar.add(txtURL, BorderLayout.CENTER);
topBar.add(btnGo, BorderLayout.EAST);
JPanel statusBar = new JPanel(new BorderLayout(5, 0));
statusBar.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
statusBar.add(lblStatus, BorderLayout.CENTER);
statusBar.add(progressBar, BorderLayout.EAST);
nestedBrowser.add(topBar, BorderLayout.NORTH);
nestedBrowser.add(webView, BorderLayout.CENTER);
nestedBrowser.add(statusBar, BorderLayout.SOUTH);
//getContentPane().add(panel);
this.add(nestedBrowser);
setPreferredSize(new Dimension(1024, 1024));
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//pack();
}
private void createScene() {
Platform.runLater(new Runnable() {
@Override
public void run() {
WebView view = new WebView();
engine = view.getEngine();
engine.titleProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, final String newValue) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//MyBrowser.this.setTitle(newValue);
}
});
}
});
engine.setOnStatusChanged(new EventHandler<WebEvent<String>>() {
@Override
public void handle(final WebEvent<String> event) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
lblStatus.setText(event.getData());
}
});
}
});
//注册Java方法处理器
engine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
@Override
public void changed(ObservableValue<? extends Worker.State> observable, Worker.State oldValue, Worker.State newValue) {
if (newValue == Worker.State.SUCCEEDED) {
JSObject win = (JSObject) engine.executeScript("window");
win.setMember("apps", javaSourceHandler);//设置变量
}
}
});
engine.locationProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> ov, String oldValue, final String newValue) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
txtURL.setText(newValue);
}
});
}
});
engine.getLoadWorker().workDoneProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, final Number newValue) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
progressBar.setValue(newValue.intValue());
}
});
}
});
engine.getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
public void changed(ObservableValue<? extends Throwable> o, Throwable old, final Throwable value) {
if (engine.getLoadWorker().getState() == FAILED) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JOptionPane.showMessageDialog(
nestedBrowser,
(value != null) ?
engine.getLocation() + "\n" + value.getMessage() :
engine.getLocation() + "\nUnexpected error.",
"Loading error...",
JOptionPane.ERROR_MESSAGE);
}
});
}
}
});
webView.setScene(new Scene(view));
webView.setPreferredSize(new Dimension(1024, 1024));
}
});
}
public void loadURL(final String url) {
Platform.runLater(new Runnable() {
@Override
public void run() {
String tmp = toURL(url);
if (tmp == null) {
tmp = toURL("http://" + url);
}
engine.load(tmp);
}
});
}
private static String toURL(String str) {
try {
return new URL(str).toExternalForm();
} catch (MalformedURLException exception) {
return null;
}
}
public static void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MyBrowser browser = new MyBrowser();
browser.setVisible(true);
browser.loadURL("http://google.com");
}
});
}
}
由于不允许设置断点,因此无法通过设置断点获得更多信息,我想知道是什么原因导致此问题,以及如何修复它?
先谢谢你,方树森
暂无答案!
目前还没有任何答案,快来回答吧!