Ignore:
Timestamp:
11/26/13 11:43:54 (11 years ago)
Author:
ngw8
Message:

Fixed various bugs with the JFXBrowser widget, incl. fixing issue where it disappeared when moving it, resizing it or switching frame

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/items/widgets/JfxBrowser.java

    r517 r533  
    77import java.awt.event.ComponentListener;
    88import java.io.File;
     9import java.io.IOException;
    910import java.lang.reflect.Constructor;
    1011import java.lang.reflect.InvocationTargetException;
     
    1617
    1718import javax.swing.JPanel;
    18 import javax.swing.border.EmptyBorder;
    1919
    2020import org.expeditee.items.Text;
     
    2525 * @author ngw8
    2626 * @author jts21
    27  *
    2827 */
    2928public class JfxBrowser extends DataFrameWidget {
    30 
    3129        private WebBrowserPanel _browser;
    3230
     
    5250        static {
    5351                String javaLibDir = System.getProperty("java.home") + File.separator + "lib" + File.separator;
    54 
    5552                try {
    56 
     53                        System.out.println(javaLibDir);
    5754                        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
     55
    5856
    5957                        File jar = new File(javaLibDir + "jfxrt.jar");
     
    6361                        addURL.invoke(classLoader, jar.toURI().toURL());
    6462
    65                        
     63                        System.out.println(System.getProperty("java.class.path"));
     64
    6665                        JFXPanel = Class.forName("javafx.embed.swing.JFXPanel", false, classLoader);
     66
     67                        final File f = new File(JFXPanel.getProtectionDomain().getCodeSource().getLocation().getPath());
     68                        System.out.println(f.getPath());
    6769
    6870                        Platform = classLoader.loadClass("javafx.application.Platform");
    6971                        PlatformRunLater = Platform.getMethod("runLater", Runnable.class);
     72
     73                        // Setting JFX to not exit when all panels are hidden, otherwise moving, resizing and changing frames will
     74                        // cause JFX to exit, preventing JFX elements from displaying at all
     75                        Platform.getMethod("setImplicitExit", boolean.class).invoke(null, false);
    7076
    7177                        Group = classLoader.loadClass("javafx.scene.Group");
     
    8389                        WebEngineLoad = WebEngine.getMethod("load", String.class);
    8490
     91
    8592                } catch (MalformedURLException e) {
    8693                        e.printStackTrace();
     
    96103                        e.printStackTrace();
    97104                } catch (InvocationTargetException e) {
     105                        e.printStackTrace();
     106                } catch (IOException e) {
    98107                        e.printStackTrace();
    99108                }
     
    118127                                this.add((Component) jfxPanel);
    119128
     129                                // Removing the gap between the top of the panel and the JFXPanel
    120130                                ((FlowLayout) this.getLayout()).setVgap(0);
    121                                 this.setBackground(Color.cyan);
    122                                 this.setBorder(new EmptyBorder(0, 0, 0, 0));
     131
     132                                // Setting bg color to be the same as bg color of widgets when they're being dragged/resized
     133                                this.setBackground(new Color(100, 100, 100));
    123134
    124135                                PlatformRunLater.invoke(null, new Runnable() {
     
    140151
    141152                /**
    142                  * Sets up the browser frame. JFX requires it to be run on a new thread.
     153                 * Sets up the browser frame. JFX requires this to be run on a new thread.
    143154                 *
    144155                 * @param url
     
    147158                private void initFx(String url) {
    148159                        try {
    149                                 Object group = Group.newInstance();
    150                                 Object scene = SceneConstructor.newInstance(group);
     160                                this.webview = WebView.newInstance();
     161                                WebViewSetPrefSize.invoke(this.webview, this.getWidth(), this.getHeight());
     162
     163                                Object scene = SceneConstructor.newInstance(this.webview);
    151164
    152165                                JFXPanelSetScene.invoke(jfxPanel, scene);
    153 
    154                                 this.webview = WebView.newInstance();
    155 
    156                                 WebViewSetPrefSize.invoke(this.webview, this.getWidth(), this.getHeight());
    157 
    158                                 ((List) GroupGetChildren.invoke(group)).add(this.webview);
    159166
    160167                                WebEngineLoad.invoke(WebViewGetEngine.invoke(this.webview), url);
     
    167174                        } catch (InstantiationException e) {
    168175                                e.printStackTrace();
     176                        } catch (SecurityException e) {
     177                                e.printStackTrace();
    169178                        }
    170179                }
     
    180189                @Override
    181190                public void componentResized(ComponentEvent e) {
     191                        // Webview is initialized in a 'run later' thread, so must check that it has actually been created before
     192                        // trying to resize
    182193                        if (webview != null) {
    183194                                try {
     
    189200                                } catch (InvocationTargetException e1) {
    190201                                        e1.printStackTrace();
     202                                } catch (SecurityException e1) {
     203                                        e1.printStackTrace();
    191204                                }
    192205                        }
    193                        
    194206                }
    195207
     
    197209                public void componentShown(ComponentEvent e) {
    198210                }
    199 
    200 
    201211        }
    202212
    203213        public JfxBrowser(Text source, String[] args) {
    204                 super(source, new WebBrowserPanel("http://waikato.ac.nz"), 100, 500, -1, 100, 300, -1);
     214                super(source, new WebBrowserPanel("http://waikato.ac.nz"), -1, 500, -1, -1, 300, -1);
    205215
    206216                _browser = (WebBrowserPanel) _swingComponent;
    207         }
    208 
    209         @Override
    210         protected String[] getArgs() {
    211                 return null;
    212217        }
    213218
     
    222227                        e.printStackTrace();
    223228                }
     229        }
     230
     231        @Override
     232        protected String[] getArgs() {
     233                return null;
    224234        }
    225235
     
    236246                return _textRepresentation.getData();
    237247        }
    238        
    239248}
Note: See TracChangeset for help on using the changeset viewer.