April 23, 2018
Hot Topics:

Swingin' Java: Doing it in style

  • April 26, 1999
  • By Dan Simon
  • Send Email »
  • More Articles »

Example 1.

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.*;

public class Example1 extends JFrame{

  public Example1() {
        super("Swingin' Java -- Styled Text");

  public static void main(String[] args) {
    Example1 frame = new Example1();

    //first we'll create a JEditorPane and populate it
    JEditorPane editor = new JEditorPane();
    //set the content type to HTML
    //set the editable to false to hide the head and title tags
    //let's create a simple html formatted document to put in the JEditorPane
    String editorText = "



This is a title

    //we can do just about any styling that we want...
    editorText += "
This is centered text
"; //yes, tables work too editorText += "
This is cell 0,0This is cell 1,0This is cell 2,0
This is cell 0,1This is cell 1,1This is cell 2,1
"; //and any other tag that adheres to the HTML3.2 specification editorText += "

This is a heading (h2)

"; //you get the idea, so we'll close it off editorText += ""; //all we need to do now is set the text into our pane editor.setText(editorText); //now let's create a JTextPane, some AttributeSets, etc JTextPane textPane = new JTextPane(); textPane.setEditable(false); //here's an example of some AttributeSets SimpleAttributeSet boldItalicRedText = new SimpleAttributeSet(); StyleConstants.setForeground(boldItalicRedText, Color.red); StyleConstants.setBold(boldItalicRedText, true); StyleConstants.setItalic(boldItalicRedText, true); SimpleAttributeSet centeredBlackText = new SimpleAttributeSet(); StyleConstants.setForeground(centeredBlackText, Color.black); StyleConstants.setAlignment(centeredBlackText, StyleConstants.ALIGN_CENTER); SimpleAttributeSet largeBlackText = new SimpleAttributeSet(); StyleConstants.setForeground(largeBlackText, Color.black); StyleConstants.setFontSize(largeBlackText, 32); //now let's use them try { Document doc = textPane.getDocument(); doc.insertString(doc.getLength(), "This is centered, black text...\n", centeredBlackText); textPane.setParagraphAttributes(centeredBlackText, true); doc.insertString(doc.getLength(), "This is bold, italic, red text...\n", boldItalicRedText); doc.insertString(doc.getLength(), "This is 32 point black text.\n", largeBlackText); } catch(BadLocationException exp) { exp.printStackTrace(); } //all we need to do now is display our components JPanel panel = new JPanel(); panel.setLayout(new GridLayout(2,1)); panel.add(new JScrollPane(editor)); panel.add(new JScrollPane(textPane)); panel.setPreferredSize(new Dimension(400,400)); //now we'll set the panel as our content pane frame.setContentPane(panel); //just to be nice, we'll shut down when the window is closed frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.pack(); frame.show(); } }

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date