I am trying to create a browser app with JxBrowser. I have imported all the jar files in my project but it still throws an error for the statement
import com.teamdev.jxbrowser.chromium.BrowserContext;
and further on for
Browser browser = BrowserFactory.createBrowser(BrowserType.Mozilla);
frame.add(browser.getComponent(), BorderLayout.CENTER);
browser.navigate("http://www.google.com");
I am running this code on a MAC OS X and have the appropriate jar file imported as well.
Can someone help me with this
I suppose you use one of the latest JxBrowser versions. Probably 4.x. In this case please note that public API in 4.x has been changed. Now, to create Browser instance you need to use the following way:
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;
import javax.swing.*;
import java.awt.*;
/**
* This sample demonstrates how to create Browser instance,
* embed it into Swing BrowserView container, display it in JFrame and
* navigate to the "www.google.com" web site.
*/
public class BrowserSample {
public static void main(String[] args) {
Browser browser = new Browser();
BrowserView browserView = new BrowserView(browser);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(browserView, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
browser.loadURL("http://www.google.com");
}
}
Related
Respected all,
I am making a Swing-application window in ECLIPSE. When I run the program as 'JAVA-Application' the program functions well. However when I try to run the program as "Java_applet" the components like 'command button', 'textbox' are invisible.
I am entirely new to Java. I had previously worked on C#. Kindly please help me.
import java.awt.EventQueue;
import java.applet.*;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import java.awt.BorderLayout;
public class sa extends Applet{
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
sa window = new sa();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public sa() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
frame.getContentPane().add(rdbtnNewRadioButton, BorderLayout.CENTER);
}
}
You can't just have your class extend Applet and expect that that is all that is necessary for it to behave like a proper Applet. You need to give the class a proper init method and build the GUI from within this method. But most importantly you will need to read an Applet tutorial, any decent tutorial should do. Myself, I'd have my GUI extend JPanel, build the GUI in its constructor, and then I could use this JPanel in a JApplet or JFrame as needed.
As Andrew aptly notes in comment,
I think the OP should also dump the entire 'applet' part of it and develop the JFrame with an idea to launching it from a link using Java Web Start. At least then it would have a better chance for working for users of Chrome and FireFox (which are both planning to remove all support for applets).
I need to create a web browser with XULRunner (or another rendering engine) using Java, but I don't have any experience with it. All I need is to open a full screen Java Panel, to load XULRunner (or another web rendering engine) inside it and to load a local Web Page there.
Somebody can help me to start? Unfortunately, I haven't so much time to try alone...
Which java GUI framework do you use?
There is a (Mozilla-) Browser implemented in SWT. You also can create this Browser within Swing:
import javax.swing.JFrame;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class BrowserSwingPanel {
public static void main(String args[]) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
Canvas canvas = new Canvas();
f.setSize(500, 400);
f.add(canvas);
f.setVisible(true);
Display display = new Display();
Shell shell = SWT_AWT.new_Shell(display, canvas);
shell.setSize(500, 400);
Browser browser = new Browser(shell, SWT.NONE);
browser.setSize(500, 400);
browser.setUrl("http://www.google.com");
shell.open();
while (!shell.isDisposed() && f.isVisible()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
f.dispose();
}
}
I found the former solution at a german webpage:
http://www.java-forum.org/awt-swing-javafx-swt/66264-swt-browser-swing.html
If you can use JavaFX, you can create a simple Browser (WebKit based) following this tutorial: http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm
I have just recently added a update log to my game. I am using tumblr to show the update news. I used simple code (Listed Below) to show it but when I run it it looks nothing like the original tumblr!
package javaapplication32;
import javax.swing.*;
public class GetWebPage {
public static void main(String args[]) throws Exception {
JEditorPane website = new JEditorPane("http://smo-gram.tumblr.com/");
website.setEditable(false);
JFrame frame = new JFrame("Google");
frame.add(new JScrollPane(website));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
}
}
Check out http://java.dzone.com/articles/web-browser-your-java-swing.
JxBrowser lets you display any webpage,by embedding a browser into your swing application.
There's a few other options available. If you need HTML5 support, switch over to JavaFX (It's way better than Swing IMO). There's also a HTML4/CSS2 renderer available for Swing called Cobra, and it seems pretty good. Another option would be Frostwire JWebBrowser, if you don't mind including native code, it seems to give you full HTML5 and CSS3 in Swing.
Remove the call of pack() method. In another way, do not expect much of HTML Swing. Supports up to HTML 3.2 (http://www.w3.org/TR/REC-html32.html).
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class GetWebPage {
public static void main(String args[]) throws Exception {
JEditorPane website = new JEditorPane("http://smo-gram.tumblr.com/");
website.setEditable(false);
JFrame frame = new JFrame("Google");
frame.add(new JScrollPane(website));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setVisible(true);
}
}
Right, so Ive got an interesting problem here concerning SWT and swing integration on mac running java 1.7. Im trying to embed an SWT Browser widget into my swing project as a panel which is pretty simple to do on java version 1.6. There has been a number of posts which explain how to do this with SWT_AWT bridge classes along with the following example:
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MySWTBrowserTest implements ActionListener {
public JButton addCodeButton;
public JButton launchBrowserButton;
public JTextField inputCode;
public JFrame frame;
static Display display;
static boolean exit;
public MySWTBrowserTest() {
frame = new JFrame("Main Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new FlowLayout());
inputCode = new JTextField(15);
inputCode.setText("999");
addCodeButton = new JButton("Add Code");
addCodeButton.addActionListener(this);
addCodeButton.setActionCommand("addcode");
launchBrowserButton = new JButton("Launch Browser");
launchBrowserButton.addActionListener(this);
launchBrowserButton.setActionCommand("launchbrowser");
mainPanel.add(inputCode);
mainPanel.add(addCodeButton);
mainPanel.add(launchBrowserButton);
frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("addcode")) {
} else if (e.getActionCommand().equals("launchbrowser")) {
createAndShowBrowser();
}
}
public void createAndShowBrowser() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Canvas canvas = new Canvas();
f.setSize(850, 650);
f.getContentPane().add(canvas);
f.setVisible(true);
display.asyncExec(new Runnable() {
#Override
public void run() {
Shell shell = SWT_AWT.new_Shell(display, canvas);
shell.setSize(800, 600);
Browser browser = new Browser(shell, SWT.NONE);
browser.setLayoutData(new GridData(GridData.FILL_BOTH));
browser.setSize(800, 600);
browser.setUrl("http://www.google.com");
shell.open();
}
});
}
public static void main(String args[]) {
//SWT_AWT.embeddedFrameClass = "sun.lwawt.macosx.CEmbeddedFrame";
display = new Display();
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MySWTBrowserTest mySWTBrowserTest = new MySWTBrowserTest();
}
});
while (!exit) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Im using the swt-3.8M5-cocoa-macosx-x86_64 JAR files which obviously need to be included to run the above example. When using both the 32 bit and 64 bit versions of the 1.6 JDK, this runs perfectly fine, but when switching to the JDK 1.7 or 1.8 VM the reproducible error is thrown:
2012-05-14 15:11:30.534 java[1514:707] Cocoa AWT: Apple AWT Java VM was loaded on first thread -- can't start AWT. (
0 liblwawt.dylib 0x00000008db728ad0 JNI_OnLoad + 468
1 libjava.dylib 0x00000001015526f1 Java_java_lang_ClassLoader_00024NativeLibrary_load + 207
2 ??? 0x00000001015a4f90 0x0 + 4317663120
)
_NSJVMLoadLibrary: NSAddLibrary failed for /libjawt.dylib
JavaVM FATAL: lookup of function JAWT_GetAWT failed. Exit
Java Result: 255
Ive inspected the java 1.7 vm and did find the libraries there, so Im struggling to see what could cause it to not load that library. Of course I make sure to use: -XstartOnFirstThread as one of the VM parameters, as is required for the SWING/AWT integration.
On a further note, I have tried the DJ Native Widgets framework, and it throws the exact same error as it also uses the underlying SWT framework.
To reproduce the effects i suggest installing JDK 1.7 (release not the developer preview) on mac, downloading the: http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops4/S-4.2M7-201205031800/swt-S-4.2M7-201205031800-cocoa-macosx-x86_64.zip to get the library and then running it with the -XstartOnFirstThread -d64 java 1.7 vm.
really hoping someone has been able to sort this out as Im sure Im not the only one trying to integrate SWT into swing on the 1.7 vm
I also spent 8 hours on google to see if this error has been reproduced anywhere else, and it has come up on a few Matlab mailing lists, but other than that I haven't been able to find something even close to a solution.
Thanks in advance.
>> UPDATE 1
Looks like we may have a winner: https://bugs.eclipse.org/bugs/show_bug.cgi?id=374199
Going to monitor this and see where it goes.
>> UPDATE 2
Here is a working example: https://stackoverflow.com/a/27754819/363573
Unfortunately there's no good answer to this. In Java 7 the AWT has been completely rewritten to use CoreAnimation layers. The SWT assumes that an AWT Canvas will be backed by an NSView but that's no longer the case. Your only choice right now is to stick with Java 6.
The AWT team is aware of the problem but you may want to file another bug on bugs.sun.com.
I was looking for a way to integrate a Web-Browser-Component in an existing Swing-Application and found WebView for Java FX 2.0. Furthermore I found a blog post on java.net showing how to integrate a Java FX component into a Swing Application . So I guess it might be doable, but I haven't tried yet.
I'm curious, do you think this is a good approach? Are there any better solutions? Is it even doable? Is maybe something prebundled out there?
The motivation is: I want to integrate some WebBrowser-whatever into an existing Swing-Application, the long-term goal being to get rid of the whole Java Desktop Application at all, replacing it with a web-based solution (the plan is to slowly convert existing aspects into webpages which are then displayed in the WebBrowser-Component until nothing is left of the swing application except for the browser-skeleton). The backend of course remains Java :-)
I haven't tried yet since I simply lack the time to integrate JavaFX with my project (its a job, we're just exploring alternatives fpr the long run), so I better ask before I get burned.
It is very well possible!
One has to install JavaFX 2.0, and somehow manage to have jfxrt.jar in the Classpath.
The following code renders a JFXPanel inside a JFrame. The JFXPanel contains a WebView which loads google.com.
However, at least on my machine, the WebView feels rather sloppy. I'm working on Mac OS X 10.6; JavaFX 2.0 is still in beta for OS X.
Alternatives I found include MozSwing, which looked very promising and feels quite fast actually. Sadly the project is not being developed any further since 2008 and the bundled XUL runner is rather old (no new fancy html 5).
Both approaches are a nightmare to include via maven, you better setup your own local repository.
import java.awt.Dimension;
import java.awt.Point;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class JavaFX {
/* Create a JFrame with a JButton and a JFXPanel containing the WebView. */
private static void initAndShowGUI() {
// This method is invoked on Swing thread
JFrame frame = new JFrame("FX");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null); // do the layout manually
final JButton jButton = new JButton("Button");
final JFXPanel fxPanel = new JFXPanel();
frame.add(jButton);
frame.add(fxPanel);
frame.setVisible(true);
jButton.setSize(new Dimension(200, 27));
fxPanel.setSize(new Dimension(300, 300));
fxPanel.setLocation(new Point(0, 27));
frame.getContentPane().setPreferredSize(new Dimension(300, 327));
frame.pack();
frame.setResizable(false);
Platform.runLater(new Runnable() { // this will run initFX as JavaFX-Thread
#Override
public void run() {
initFX(fxPanel);
}
});
}
/* Creates a WebView and fires up google.com */
private static void initFX(final JFXPanel fxPanel) {
Group group = new Group();
Scene scene = new Scene(group);
fxPanel.setScene(scene);
WebView webView = new WebView();
group.getChildren().add(webView);
webView.setMinSize(300, 300);
webView.setMaxSize(300, 300);
// Obtain the webEngine to navigate
WebEngine webEngine = webView.getEngine();
webEngine.load("http://www.google.com/");
}
/* Start application */
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
initAndShowGUI();
}
});
}
}