Created
June 6, 2019 07:50
-
-
Save hakanai/ed0ceb38e97878dafffe1bf4ca784353 to your computer and use it in GitHub Desktop.
Small JxBrowser example for crash reproduction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.BorderLayout; | |
import java.awt.Dimension; | |
import java.io.File; | |
import javax.swing.JFrame; | |
import javax.swing.JMenuBar; | |
import javax.swing.JPanel; | |
import javax.swing.SwingUtilities; | |
import javax.swing.UIManager; | |
import com.google.common.io.Files; | |
import com.teamdev.jxbrowser.chromium.Browser; | |
import com.teamdev.jxbrowser.chromium.BrowserContext; | |
import com.teamdev.jxbrowser.chromium.BrowserContextParams; | |
import com.teamdev.jxbrowser.chromium.BrowserType; | |
import com.teamdev.jxbrowser.chromium.swing.BrowserView; | |
import org.trypticon.gum.eawt.Application; | |
import org.trypticon.gum.eawt.MacFactory; | |
public class Main | |
{ | |
public static void main(String[] args) | |
{ | |
SwingUtilities.invokeLater(() -> { | |
if (isAqua()) | |
{ | |
Application application = MacFactory.getApplication(); | |
if (application.isMac()) // which it should be | |
{ | |
application.setDefaultMenuBar(new JMenuBar()); | |
} | |
} | |
JFrame frame = createFrame(); | |
frame.pack(); | |
frame.setVisible(true); | |
}); | |
} | |
private static JFrame createFrame() | |
{ | |
JFrame frame = new JFrame("Welcome"); | |
frame.setJMenuBar(new JMenuBar()); | |
frame.setSize(1280, 735); | |
frame.setMinimumSize(new Dimension(1280, 735)); | |
JPanel browserPanel = createBrowserPanel(); | |
frame.setLayout(new BorderLayout()); | |
frame.add(browserPanel, BorderLayout.CENTER); | |
return frame; | |
} | |
private static JPanel createBrowserPanel() | |
{ | |
File dataDir = Files.createTempDir(); | |
BrowserContext browserContext = new BrowserContext(new BrowserContextParams(dataDir.toString())); | |
// Browser browser = new Browser(BrowserType.LIGHTWEIGHT, browserContext); | |
Browser browser = new Browser(BrowserType.HEAVYWEIGHT, browserContext); | |
BrowserView browserView = new BrowserView(browser); | |
JPanel panel = new JPanel(new BorderLayout()); | |
panel.add(browserView, BorderLayout.CENTER); | |
browser.loadHTML("<html><body><p>Hi there."); | |
return panel; | |
} | |
private static boolean isAqua() | |
{ | |
String uiClassName = UIManager.getLookAndFeel().getClass().getName(); | |
// Taking the cheap and dirty solution for now. This can be improved using reflection. | |
return "org.trypticon.haqua.HaquaLookAndFeel".equals(uiClassName) || | |
"ch.randelshofer.quaqua.QuaquaLookAndFeel".equals(uiClassName) || | |
"com.apple.laf.AquaLookAndFeel".equals(uiClassName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment