Java - Showing a Webpage In Jframe - java

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);
}
}

Related

Java components are invisible when swing app is run as APPLET

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).

JxBrowser related

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");
}
}

How to create an hello world in Java Swing? What is wrong in my code?

I am studying Java Swing and I have some problem with the following simple code:
package com.techub.exeute;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class Main{
public static void main(String[] args) {
JFrame frame = new JFrame("FrameDemo");
frame.setMinimumSize(new Dimension(800, 400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel myLabel = new JLabel("Hello World !!!", SwingConstants.CENTER);
myLabel.setFont(new Font("Serif", Font.BOLD, 22));
myLabel.setBackground(Color.blue);
myLabel.setOpaque(true);
myLabel.setPreferredSize(new Dimension(100, 80));
frame.getContentPane().add(myLabel, BorderLayout.NORTH);
}
}
My idea is to create a JFrame object and insert into it an Hello World JLabel object settings some property.
I do it into the main() method. The problem is that when I execute the program I don't see anything !!! Why? What is wrong in my code?
Tnx
Andrea
You are creating the frame but you are not displaying it. Call
frame.setVisible(true);
to display it.
Another thing: you should not manipulate GUI components in the main thread. Instead, create a new method for creating the frame and setting up the components, and run that method in the event dispatch thread, like in the example from the official tutorial:
import javax.swing.*;
public class HelloWorldSwing {
private static void createAndShowGUI() {
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Just add
frame.setVisible(true);
to your code
See the steps to Creating and Showing Java Swing Frames
//1. Create the frame.
JFrame frame = new JFrame("FrameDemo");
//2. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//3. Create components and put them in the frame.
//...create emptyLabel...
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//4. Size the frame.
frame.pack();
//5. Show it.
frame.setVisible(true);
You missed out #5
You need a
frame.setVisible(true);
call in your code.
As others mentioned you should not use the main Thread for gui operations. I suggest you should refer to the official tutorials of SWING, they are rather helpful and you'll see examples there for proper threading.
keep this line in your method
frame.setVisible(true);

Where to place gui item code

I am starting a new GUI project, and I am wondering where is the best place to put item code, such as a button, text field or something else? I don't think that the best place for the code is in the main class, because it seems like that would be too much code for one file and harder to manage. This is how I usually do it (The all in one file way).
package apollo;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Apollo{
protected JFrame frame = new JFrame("Apollo");
public Apollo(){
frame.setSize(800, 600);
frame.setLayout(new FlowLayout());
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
this.buildLayout();
frame.revalidate();
}
protected void buildLayout(){
JTextField txt = new JTextField(30);
frame.add(txt);
JButton btn = new JButton("Submit");
frame.add(btn);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args){
Apollo a = new Apollo();
}
}
Your main class should typically have nothing but a main method.
That main method should create a class which handles initializing your GUI.
For other UI components, if you reuse them, or their code is large, the component needs its own class.
If you'll never reuse the component, and its code is small, it doesn't need its own class.

Java SWING: adding a JTextField (never used anywhere) randomly makes the screen go white

I'm developing on ubuntu 10.04 with using Eclipse, and when I add a JTextField into the following code (that I don't use anywhere, or make visible!) the window, instead of displaying the images like it's supposed to, goes blank.
Anyone have any idea what's causing this?
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Testi {
public static void main(String[] args) {
ImageIcon icon1 = new ImageIcon("background.jpg");
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,500);
JPanel panel = new JPanel();
panel.setSize(600, 600);
panel.setOpaque(false);
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
JLabel label = new JLabel();
label.setSize(500, 500);
label.setIcon(icon1);
label.setLayout(new FlowLayout(FlowLayout.CENTER));
// FOLLOWING LINE IS THE PROBLEM: when in code, the screen goes white
JTextArea text1 = new JTextArea("asd");
label.add(panel);
frame.add(label);
}
}
Works for me, which makes me think it is a EDT issue. Move your call to setVisible to the end of your main method.
From this link: http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
This method is thread safe, although most Swing methods are not.
An application's GUI can often be constructed and shown in the main thread: The following typical code is safe, as long as no components (Swing or otherwise) have been realized:
public class MyApplication {
public static void main(String[] args) {
JFrame f = new JFrame("Labels");
// Add components to
// the frame here...
f.pack();
f.show();
// Don't do any more GUI work here...
}
}
All the code shown above runs on the "main" thread. The f.pack() call realizes the components under the JFrame. This means that, technically, the f.show() call is unsafe and should be executed in the event-dispatching thread. However, as long as the program doesn't already have a visible GUI, it's exceedingly unlikely that the JFrame or its contents will receive a paint() call before f.show() returns. Because there's no GUI code after the f.show() call, all GUI work moves from the main thread to the event-dispatching thread, and the preceding code is, in practice, thread-safe.
I had the same problem.
For me the fix was to move the call to frame.setVisible() below the part where I added my components.

Categories