JFileChooser can't return
Web Browser, Flash Player, HTML editor, Media player for Swing and SWT
Brought to you by:
chrriis
my code :
public class WebBrowser extends JPanel {
private static final String LS = System.getProperty("line.separator"); public WebBrowser() { super(new BorderLayout()); JPanel webBrowserPanel = new JPanel(new BorderLayout()); webBrowserPanel.setBorder(BorderFactory.createTitledBorder("tttt")); JWebBrowser webBrowser = new JWebBrowser(); webBrowser.setBarsVisible(false); // final JTextArea functionCallsTextArea = new JTextArea(7, 0); webBrowser.registerFunction(new WebBrowserFunction("callJavaReadLineFile") { @Override public Object invoke(JWebBrowser webBrowser, Object... args) { // JFileDialog fileDialog = new JFileDialog(); // fileDialog.show(null); JFileChooser filechooser = new JFileChooser();//创建文件选择器 filechooser.setCurrentDirectory(new File("."));//设置当前目录 filechooser.showOpenDialog(WebBrowser.this); return null; } }); webBrowser.registerFunction(new WebBrowserFunction("callJavaSaveLineFile") { @Override public Object invoke(JWebBrowser webBrowser, Object... args) { String data = (String) args[0]; String fileName; /* JFileDialog fileDialog = new JFileDialog(); fileDialog.setDialogType(JFileDialog.DialogType.SAVE_DIALOG_TYPE); fileDialog.setExtensionFilters(new String[] {"*.*", "*.mp3;*.avi", "*.txt;*.doc"}, new String[] {"All files", "Multimedia file (*.mp3, *.avi)", "Text document (*.txt, *.doc)"}, 1); fileDialog.setConfirmedOverwrite(true); // fileDialog.sh fileDialog.show(null);*/ // JDialog dlg = new JDialog() ; // dlg.setVisible(true) ; /* JFileChooser filechooser = new JFileChooser();//创建文件选择器 filechooser.setCurrentDirectory(new File("."));//设置当前目录 filechooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//设置选择模式,既可以选择文件又可以选择文件夹 String extj[] = {"jpeg", "jpg"}; FileFilter filter = new FileNameExtensionFilter("JPEG Image", extj); filechooser.setFileFilter(filter);//设置文件后缀过滤器 String extt[] = {"tiff", "tif"}; filter = new FileNameExtensionFilter("TIFF Image", extt); filechooser.setFileFilter(filter); int retval = filechooser.showSaveDialog(webBrowser);//显示“保存文件”对话框 if (retval == JFileChooser.APPROVE_OPTION) { fileName = filechooser.getSelectedFile().getName(); System.out.println("File to save " + fileName); }*/ return ""; } }); webBrowser.registerFunction(new WebBrowserFunction("readDataFile") { @Override public Object invoke(JWebBrowser webBrowser, Object... args) { return null; } }); webBrowser.setHTMLContent(getContents()); webBrowserPanel.add(webBrowser, BorderLayout.CENTER); webBrowserPanel.setPreferredSize(new Dimension(800, 600)); add(webBrowserPanel, BorderLayout.NORTH); } private String getContents() { String scontent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> " + LS + "<html xmlns=\"http://www.w3.org/1999/xhtml\"> " + LS + "<head> " + LS + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" /> " + LS + "<title></title> " + LS + "<style type=\"text/css\"> " + LS + "#content{ width:100%; " + LS + "height:100%; " + LS + "margin:0 auto; " + LS + "padding:10px 10px 10px 10px; " + LS + "text-align:center; " + LS + "background:#fff;} " + LS + "#left{ width:40%; float:left; " + LS + "text-align:left; " + LS + "padding-left:20px;} " + LS + ".right{ width:50%; float:left; } " + LS + " html,body{margin:0;padding:0;} " + LS + " .iw_poi_title {color:#CC5522;font-size:14px;font-weight:bold;overflow:hidden;padding-right:13px;white-space:nowrap} " + LS + " .iw_poi_content {font:12px arial,sans-serif;overflow:visible;padding-top:4px;white-space:-moz-pre-wrap;word-wrap:break-word} " + LS + ".kuan{ height:35px;} " + LS + "</style> " + LS + "<script type=\"text/javascript\" src=\"http://api.map.baidu.com/api?key=&v=1.2&services=true\"></script> " + LS + "</head> " + LS + "<body> " + LS + " <div id=\"content\"> " + LS + " <div class=\"right\" style=\"width:697px;height:550px;border:#ccc solid 1px;\" id=\"dituContent\"></div> " + LS + " <div id=\"left\"> " + LS + " <input type=\"button\" value=\"完成\" onclick=\"saveLineFile();\" /> " + LS + " <div class=\"kuan\"> " + LS + " <input name=\"\" type=\"button\" value=\"读取\" onclick=\"readLineFile();\" /> " + LS + " </div> " + LS + "<script type=\"text/javascript\"> " + LS + " //创建和初始化地图函数: " + LS + " function initMap(){ " + LS + " createMap();//创建地图 " + LS + " setMapEvent();//设置地图事件 " + LS + " addMapControl();//向地图添加控件 " + LS + " } " + LS + " //创建地图函数: " + LS + " function createMap(){ " + LS + " var map = new BMap.Map(\"dituContent\"); " + LS + " //在百度地图容器中创建一个地图 " + LS + " var point = new BMap.Point(113.30765,23.120049);" + LS + " //定义一个中心点坐标 " + LS + " map.centerAndZoom(point,12);" + LS + " //设定地图的中心点和坐标并将地图显示在地图容器中 " + LS + " window.map = map;" + LS + " //将map变量存储在全局 " + LS + " } " + LS + " //地图事件设置函数: " + LS + " function setMapEvent(){ " + LS + " map.enableDragging();" + LS + " //启用地图拖拽事件,默认启用(可不写) " + LS + " map.enableScrollWheelZoom(); " + LS + " //启用地图滚轮放大缩小 " + LS + " map.enableDoubleClickZoom(); " + LS + " //启用鼠标双击放大,默认启用(可不写) " + LS + " map.enableKeyboard();" + LS + " //启用键盘上下左右键移动地图 " + LS + " } " + LS + " //地图控件添加函数: " + LS + " function addMapControl(){ " + LS + " //向地图中添加缩放控件 " + LS + " var ctrl_nav = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_LEFT,type:BMAP_NAVIGATION_CONTROL_LARGE}); " + LS + " map.addControl(ctrl_nav); " + LS + " //向地图中添加比例尺控件 " + LS + " var ctrl_sca = new BMap.ScaleControl({anchor:BMAP_ANCHOR_BOTTOM_LEFT}); " + LS + " map.addControl(ctrl_sca); " + LS + " } " + LS + " initMap(); " + LS + " var arrayPoint = new Array(); " + LS + " var i=0; " + LS + " var polyline; " + LS + " map.addEventListener(\"click\", function(e){ " + LS + " var point = new BMap.Point(e.point.lng, e.point.lat); " + LS + " var marker = new BMap.Marker(point); " + LS + " marker.addEventListener(\"click\", function(){ " + LS + " i=1; " + LS + " var p = marker.getPosition(); " + LS + " map.removeOverlay(marker); " + LS + " removePoint(p); " + LS + " map.removeOverlay(polyline); " + LS + " //删掉折线覆盖物 " + LS + " polyline = new BMap.Polyline(arrayPoint, {strokeColor:\"blue\", strokeWeight:3, strokeOpacity:0.5}); " + LS + " map.addOverlay(polyline); " + LS + " //添加折线覆盖物 " + LS + " }); " + LS + " if(i==0){ " + LS + " arrayPoint.unshift(point); " + LS + " map.addOverlay(marker); " + LS + " // 将标注添加到地图中 " + LS + " map.removeOverlay(polyline); " + LS + " //删掉折线覆盖物 " + LS + " polyline = new BMap.Polyline(arrayPoint, {strokeColor:\"blue\", strokeWeight:3, strokeOpacity:0.5}); " + LS + " map.addOverlay(polyline); " + LS + " //添加折线覆盖物 " + LS + " } " + LS + " i=0; " + LS + " }); " + LS + " //经常用的是通过遍历,重构数组,利用数组下表删除数组 " + LS + " Array.prototype.remove=function(dx){ " + LS + " if(isNaN(dx)||dx>this.length){ " + LS + " return false; " + LS + " } " + LS + " for(var t=0,n=0;t<this.length;t++) " + LS + " { " + LS + " if(this[t]!=this[dx]) " + LS + " { " + LS + " this[n++]=this[t]; " + LS + " } " + LS + " } " + LS + " this.length-=1 " + LS + " } " + LS + " function removePoint(p1){ " + LS + " var h = \"\"; " + LS + " for(var n=0; n<arrayPoint.length; n++){ " + LS + " h = p1.lng+\"--\"+p1.lat+\"|||\"+arrayPoint[n].lng+\"...\"+arrayPoint[n].lat + h; " + LS + " if(arrayPoint[n].lat==p1.lat && arrayPoint[n].lng==p1.lng){ " + LS + " arrayPoint.remove(n); " + LS + " } " + LS + " } " + LS + " } " + LS + " //完成选点 " + LS + " function saveLineFile(){ " + LS + " var value = \"\"; " + " for(var n=0; n < arrayPoint.length; n++){ " + LS + " if(n != arrayPoint.length -1 ) " + LS + " value += arrayPoint[n].lng + ',' +arrayPoint[n].lat + ',';" + LS + " else " + LS + " value += arrayPoint[n].lng + ',' +arrayPoint[n].lat ;" + " } " + LS + " var result = callJavaSaveLineFile(value) ; " + LS + " } " + LS + " function readLineFile(){ " + LS + " callJavaReadLineFile(); " + LS + " } " + LS + "</script> " + LS + "</div> " + LS + "</body> " + LS + "</html> "; return scontent; } /* * Standard main method to try that test as a standalone application. */ public static void main(String[] args) { UIUtils.setPreferredLookAndFeel(); NativeInterface.open(); SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame("test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new WebBrowser(), BorderLayout.CENTER); frame.setSize(800, 600); frame.setLocationByPlatform(true); frame.setVisible(true); } }); NativeInterface.runEventPump(); }
}
Hi,
I tried the code, and indeed it freezes the application. The problem is probably that the UI thread is blocked by the web browser and the modal dialog cannot function properly. Such callbacks are supposed to run code but should not have any UI actions. They probably also have limitation with the methods they can call on the web browser while processing the function.
I think you should open the dialog asynchronously in a "SwingUtilities.invokeLater" call, and once the dialog closes, you execute Javascript code to notify the user's selection. It is not as simple as just returning the result to the caller, but it should solve the problem.
Please let me know if that answer is satisfying.
-Christopher