Html with java swing

Embedding HTML into Java Swing Applications

HTML in Swing applications? Yes, you have read it right. Swing doesn’t provide components to embed HTML and I have seen lot of people asking how can we add html content in Swing applications. Your concerns have been addressed by the latest release of JavaFX which is JavaFX 2.0. You might wonder what this JavaFX is, so let me just touch upon that as well.

JavaFX is the RIA platform envisaged by Sun Microsystems as the next UI toolkit for Java, as Swing has been around for a long time and has started to age. The initiative was good, but was not rightly implemented. The JavaFX 1, 1.2, 1.3 or in general anything before 2.0 is no longer supported. Pre JavaFX 2.0 one had to use JavaFX Script to create the applications and these could invoke the Java API. But learning a new language altogether was not appealing for a Java developer as one couldn’t easily use with existing Java code base. In 2.0 the JavaFX Script was totally removed to make way for the Java based api for creating JavaFX components. Once you have setup the JavaFX SDK, you get to use the JavaFX specific API in the javafx.* packages. There are lot of cool things underneath the JavaFX platform, but that’s out of the scope of this article.

Coming back to our main focus, JavaFX provides a component called WebView which with the help of WebEngine can be used to load web pages along with constructing the DOM and running the required JavaScript. But that’s a JavaFX component. How is it going to help us in our Swing application? Exactly, JavaFX provides another component called JFXPanel which can be used to embed JavaFX components into Swing applications and in turn the JFXPanel is added to our JFrame or other Swing containers.

JFrame myFrame = new JFrame(); JFXPanel myFXPanel = new JFXPanel(): myFrame.add(myFXPanel);

Now the required JavaFX components would be added to the JFXPanel. The JavaFX components are to be created only in the JavaFX Application thread and not in the main thread or the thread created using SwingUtilities.invokeLater. Before that lets just get familiar with a few JavaFX components which we would be using in this example:

  • javafx.scene.Scene: All components in the JavaFX application are represented as a scene graph – a collection of parent and child components. The Scene component is the container for the scene graph or the components, and we define the root for all these containers when we instantiate the Scene component.
  • javafx.scene.layout.BorderPane: Its the JavaFX’s version of all familiar Swing BorderLayout.

So lets try to add a WebView component to the JFXPanel.

Platform.runLater(new Runnable() < @Override public void run() < BorderPane borderPane = new BorderPane(); WebView webComponent = new WebView(); webComponent.getEngine().load("http://google.com/"); borderPane.setCenter(webComponent); Scene scene = new Scene(borderPane,450,450); myFXPanel.setScene(scene); >>);

The Platform.runLater creates a JavaFX application thread, and this thread has to be used for all the JavaFX based operations (we can create multiple such threads, but yes, for any of the JavaFX related operation is carried out as part of this thread).

In the above snippet, we create an instance of BorderPane, set it as the root of scene when we create a Scene object and then add the WebView to the BorderPane. We also ask the WebEngine of the WebView to load the URL: www.google.com. The scene object is then added to the JFXPanel. Remember, that JFXPanel is alread added to the JFrame or some Swing components. So you have a Swing application loading your web pages.

Читайте также:  Window settimeout javascript function

Below is the complete code for loading web pages in Swing applications.

import javafx.application.Platform; import javafx.embed.swing.JFXPanel; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.scene.web.WebView; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class SwingHtmlDemo < public static void main(String [] args)< SwingUtilities.invokeLater(new Runnable() < @Override public void run() < ApplicationFrame mainFrame = new ApplicationFrame(); mainFrame.setVisible(true); >>); > > /** * Main window used to display some HTML content. */ class ApplicationFrame extends JFrame < JFXPanel javafxPanel; WebView webComponent; JPanel mainPanel; JTextField urlField; JButton goButton; public ApplicationFrame()< javafxPanel = new JFXPanel(); initSwingComponents(); loadJavaFXScene(); >/** * Instantiate the Swing compoents to be used */ private void initSwingComponents() < mainPanel = new JPanel(); mainPanel.setLayout(new BorderLayout()); mainPanel.add(javafxPanel, BorderLayout.CENTER); JPanel urlPanel = new JPanel(new FlowLayout()); urlField = new JTextField(); urlField.setColumns(50); urlPanel.add(urlField); goButton = new JButton("Go"); /** * Handling the loading of new URL, when the user * enters the URL and clicks on Go button. */ goButton.addActionListener(new ActionListener() < @Override public void actionPerformed(ActionEvent e) < Platform.runLater(new Runnable() < @Override public void run() < String url = urlField.getText(); if ( url != null && url.length() >0) < webComponent.getEngine().load(url); >> >); > >); urlPanel.add(goButton); mainPanel.add(urlPanel, BorderLayout.NORTH); this.add(mainPanel); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(700,600); > /** * Instantiate the JavaFX Components in * the JavaFX Application Thread. */ private void loadJavaFXScene() < Platform.runLater(new Runnable() < @Override public void run() < BorderPane borderPane = new BorderPane(); webComponent = new WebView(); webComponent.getEngine().load("http://google.com/"); borderPane.setCenter(webComponent); Scene scene = new Scene(borderPane,450,450); javafxPanel.setScene(scene); >>); > >

I used JavaFX SDK for Linux on Ubuntu and IntelliJ to develop this sample application. One can download the JavaFX SDK from here.

About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

Источник

How to Use HTML in Swing Components

Many Swing components display a text string as part of their GUI. By default, a component’s text is displayed in a single font and color, all on one line. You can determine the font and color of a component’s text by invoking the component’s setFont and setForeground methods, respectively. For example, the following code creates a label and then sets its font and color:

label = new JLabel("A label"); label.setFont(new Font("Serif", Font.PLAIN, 14)); label.setForeground(new Color(0xffffdd));

If you want to mix fonts or colors within the text, or if you want formatting such as multiple lines, you can use HTML. HTML formatting can be used in all Swing buttons, menu items, labels, tool tips, and tabbed panes, as well as in components such as trees and tables that use labels to render text.

To specify that a component’s text has HTML formatting, just put the tag at the beginning of the text, then use any valid HTML in the remainder. Here is an example of using HTML in a button’s text:

button = new JButton("Two
lines");

Here is the resulting button.

An Example: HtmlDemo

An application called HtmlDemo lets you play with HTML formatting by setting the text on a label. You can find the entire code for this program in HtmlDemo.java . Here is a picture of the HtmlDemo example.

Screenshot of HtmlDemo in the Metal look and feel.

Try This:

  1. Click the Launch button to run HtmlDemo using Java™ Web Start (download JDK 7 or later). Alternatively, to compile and run the example yourself, consult the example index.
  2. Edit the HTML formatting in the text area at the left and click the «Change the label» button. The label at the right shows the result.
  3. Remove the tag from the text area on the left. The label’s text is no longer parsed as HTML.
Читайте также:  If else exception java

Example 2: ButtonHtmlDemo

Let us look at another example that uses HTML. ButtonHtmlDemo adds font, color, and other text formatting to three buttons. You can find the entire code for this program in ButtonHtmlDemo.java . Here is a picture of the ButtonHtmlDemo example.

Screenshot of ButtonHtmlDemo in the Metal look and feel.

Click the Launch button to run ButtonHtmlDemo using Java™ Web Start (download JDK 7 or later). Alternatively, to compile and run the example yourself, consult the example index.

The left and right buttons have multiple lines and text styles and are implemented using HTML. The middle button, on the other hand, uses just one line, font, and color, so it does not require HTML. Here is the code that specifies the text formatting for these three buttons:

b1 = new JButton("Disable
" + "middle button", leftButtonIcon); Font font = b1.getFont().deriveFont(Font.PLAIN); b1.setFont(font); . b2 = new JButton("middle button", middleButtonIcon); b2.setFont(font); b2.setForeground(new Color(0xffffdd)); . b3 = new JButton("Enable
" + "middle button", rightButtonIcon); b3.setFont(font);

Note that we have to use a tag to cause the mnemonic characters «D» and «E» to be underlined in the buttons that use HTML. Note also that when a button is disabled, its HTML text unfortunately remains black, instead of becoming gray. (Refer to bug #4783068 to see if this situation changes.)

This section discussed how to use HTML in ordinary, non-text components. For information on components whose primary purpose is formatting text, see Using Text Components.

If you are programming in JavaFX, see HTML Editor.

Источник

How to create a simple Swing HTML viewer with Java

I’ve been working on writing my own Java text editor on and off for several years now, and one feature I just added to this editor is the ability to view (or preview) HTML and CSS content. Using just the base Swing classes this turned out reasonably well — I’m not looking to write a complete browser here — so I thought I’d share some source code to show how this works.

Here then is the source code for a complete class that shows how to create an HTML viewer, and apply some CSS styles to that view, using Java:

import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.SwingUtilities; import javax.swing.text.Document; import javax.swing.text.html.HTMLEditorKit; import javax.swing.text.html.StyleSheet; /** * A complete Java class that demonstrates how to create an HTML viewer with styles, * using the JEditorPane, HTMLEditorKit, StyleSheet, and JFrame. * * @author alvin alexander, devdaily.com. * */ public class HtmlEditorKitTest < public static void main(String[] args) < new HtmlEditorKitTest(); >public HtmlEditorKitTest() < SwingUtilities.invokeLater(new Runnable() < public void run() < // create jeditorpane JEditorPane jEditorPane = new JEditorPane(); // make it read-only jEditorPane.setEditable(false); // create a scrollpane; modify its attributes as desired JScrollPane scrollPane = new JScrollPane(jEditorPane); // add an html editor kit HTMLEditorKit kit = new HTMLEditorKit(); jEditorPane.setEditorKit(kit); // add some styles to the html StyleSheet styleSheet = kit.getStyleSheet(); styleSheet.addRule("body "); styleSheet.addRule("h1 "); styleSheet.addRule("h2 "); styleSheet.addRule("pre "); // create some simple html as a string String htmlString = "\n" + "\n" + "

Welcome!

\n" + "

This is an H2 header

\n" + "

This is some sample text

\n" + "

devdaily blog

\n" + "\n"; // create a document, set it on the jeditorpane, then add the html Document doc = kit.createDefaultDocument(); jEditorPane.setDocument(doc); jEditorPane.setText(htmlString); // now add it all to a frame JFrame j = new JFrame("HtmlEditorKit Test"); j.getContentPane().add(scrollPane, BorderLayout.CENTER); // make it easy to close the application j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // display the frame j.setSize(new Dimension(300,200)); // pack it, if you prefer //j.pack(); // center the jframe, then make it visible j.setLocationRelativeTo(null); j.setVisible(true); > >); > >

Discussion

Here’s a quick overview of how this Java code works:

  1. I create a JEditorPane, and make it read-only.
  2. I place that editor inside a JScrollPane to provide support for scrolling.
  3. I create a new HTMLEditorKit, and set that on the editor pane.
  4. I create a Java StyleSheet, and assign some styles («rules») to it.
  5. I create a Document, set it on the jEditorPane, and add HTML text to it.
  6. I place the scroll pane (which already wraps the editor) in the center of a JFrame.
  7. I tell the JFrame that this application should close when the user presses the close button on the JFrame window.
  8. I set the size of the JFrame, center it, and make it visible.
Читайте также:  Мой домашний питомец питон

Sample image

The following image shows what this simple HTML viewer looks like when it is running:

This simple HTML viewer just scratches the surface of what you can do using Java, Swing, the JEditorPane, the HTMLEditorKit, and the StyleSheet classes, and hopefully it can help you get started on your own project.

Источник

Swing Examples — Using JEditorPane to display an HTML page

Following example showcase how to display a HTML content of a HTML file in swing based application.

We are using the following APIs.

  • JEditorPane − To create a editor box to display an HTML content.
  • jEditorPane.setPage(URL url) − To get HTML from a url passed and display.

Example

Let create a html file test.htm in the current directory where SwingTester.java is present.

test.htm

  • A single API is to be sufficient to support multiple look and feel.

  • API is to be model driven so that the highest level API is not required to have data.

  • API is to use the Java Bean model so that Builder Tools and IDE can provide better services to the developers for use.

  • Model represents component’s data.

  • View represents visual representation of the component’s data.

  • Controller takes the input from the user on the view and reflects the changes in Component’s data.

  • Swing component has Model as a seperate element, while the View and Controller part are clubbed in the User Interface elements. Because of which, Swing has a pluggable look-and-feel architecture.

  • Light Weight − Swing components are independent of native Operating System’s API as Swing API controls are rendered mostly using pure JAVA code instead of underlying operating system calls.

  • Rich Controls − Swing provides a rich set of advanced controls like Tree, TabbedPane, slider, colorpicker, and table controls.

  • Highly Customizable − Swing controls can be customized in a very easy way as visual apperance is independent of internal representation.

  • Pluggable look-and-feel − SWING based GUI Application look and feel can be changed at run-time, based on available values.

SwingTester.htm

import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.LayoutManager; import java.io.IOException; import java.net.URL; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; public class SwingTester < public static void main(String[] args) < createWindow(); >private static void createWindow() < JFrame frame = new JFrame("Swing Tester"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); createUI(frame); frame.setSize(560, 450); frame.setLocationRelativeTo(null); frame.setVisible(true); >private static void createUI(final JFrame frame) < JPanel panel = new JPanel(); LayoutManager layout = new FlowLayout(); panel.setLayout(layout); JEditorPane jEditorPane = new JEditorPane(); jEditorPane.setEditable(false); URL url= SwingTester.class.getResource("test.htm"); try < jEditorPane.setPage(url); >catch (IOException e) < jEditorPane.setContentType("text/html"); jEditorPane.setText("Page not found."); > JScrollPane jScrollPane = new JScrollPane(jEditorPane); jScrollPane.setPreferredSize(new Dimension(540,400)); panel.add(jScrollPane); frame.getContentPane().add(panel, BorderLayout.CENTER); > >

Источник

Оцените статью