package test; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import javafx.application.Platform; import javafx.collections.ObservableList; import javafx.embed.swing.JFXPanel; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; import javax.swing.*; public class MainFrame extends JFrame { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new MainFrame(); } }); } public MainFrame() { JPanel contentPane = new JPanel(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setContentPane(contentPane); JButton openButton = new JButton("Open WebView Tabs"); openButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new PopupFrame(); } }); contentPane.add(openButton); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } public static class PopupFrame extends JFrame { public PopupFrame() { this.setContentPane(createTabbedPane()); this.setSize(new Dimension(400, 300)); this.setLocationRelativeTo(null); this.setVisible(true); } private JTabbedPane createTabbedPane() { JTabbedPane tabbedPane = new JTabbedPane(); JComponent panel1 = new WebBrowserPanel("Tab #1"); tabbedPane.addTab("Tab 1", null, panel1, ""); JComponent panel2 = new WebBrowserPanel("Tab #2"); tabbedPane.addTab("Tab 2", null, panel2, ""); JComponent panel3 = new WebBrowserPanel("Tab #3"); tabbedPane.addTab("Tab 3", null, panel3, ""); return tabbedPane; } } } class WebBrowserPanel extends JPanel { private Stage stage; private WebView browser; private JFXPanel jfxPanel; private JTextField urlInputField; private WebEngine webEngine; public WebBrowserPanel(final String tabName) { this.addComponentListener(new ComponentAdapter() { @Override public void componentHidden(ComponentEvent e) { System.out.println(tabName + " hidden"); resetComponents(); } @Override public void componentShown(ComponentEvent e) { System.out.println(tabName + " shown"); initComponents(); } }); } private void resetComponents() { if (jfxPanel != null && urlInputField != null) { remove(jfxPanel); remove(urlInputField); } stage = null; browser = null; jfxPanel = null; urlInputField = null; webEngine = null; } private void initComponents() { jfxPanel = new JFXPanel(); createScene(); urlInputField = new JTextField("http://www.oracle.com"); urlInputField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Platform.runLater(new Runnable() { public void run() { webEngine.load(urlInputField.getText()); } }); } }); setLayout(new BorderLayout()); add(jfxPanel, BorderLayout.CENTER); add(urlInputField, BorderLayout.NORTH); } private void createScene() { Platform.runLater(new Runnable() { public void run() { Group root = new Group(); Scene scene = new Scene(root); browser = new WebView(); webEngine = browser.getEngine(); webEngine.load(urlInputField.getText()); ObservableList children = root.getChildren(); children.add(browser); jfxPanel.setScene(scene); } }); } }