JEditorPane and the Swing HTML Package 2 : Text EditorPane « Swing JFC « Java






JEditorPane and the Swing HTML Package 2

JEditorPane and the Swing HTML Package 2
    
/*
Core SWING Advanced Programming 
By Kim Topley
ISBN: 0 13 083292 8       
Publisher: Prentice Hall  
*/

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.EditorKit;

public class EditorPaneExample2 extends JFrame {
  public EditorPaneExample2() {
    super("JEditorPane Example 2");

    pane = new JEditorPane();
    pane.setEditable(false); // Read-only

    getContentPane().setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = 2;
    c.gridheight = 1;
    c.anchor = GridBagConstraints.CENTER;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(2, 2, 2, 2);
    getContentPane().add(new JScrollPane(pane), c);

    // Build the panel of controls
    JPanel upperPanel = new JPanel();
    upperPanel.setLayout(new BorderLayout());

    // Add a text area with a surrounding border
    textArea = new JTextArea(5, 30);
    JPanel textPanel = new JPanel();
    textPanel.setLayout(new BorderLayout());
    textPanel.setBorder(BorderFactory.createTitledBorder("Source Text"));
    textPanel.add(new JScrollPane(textArea));
    upperPanel.add(textPanel, "Center"); // Add the text panel

    // Build a subpanel with two radio buttons to
    // select document type and a button to install
    // new text.
    Box controlPanel = new Box(BoxLayout.Y_AXIS);

    // Add the radio buttons
    ButtonGroup group = new ButtonGroup();
    plainButton = new JRadioButton("Plain Text");
    htmlButton = new JRadioButton("HTML");
    group.add(plainButton);
    group.add(htmlButton);

    controlPanel.add(plainButton);
    controlPanel.add(htmlButton);

    // Add a button to install the text
    JButton installButton = new JButton("Install text");
    controlPanel.add(installButton);

    // Add glue to pad out extra space
    controlPanel.add(Box.createVerticalGlue());

    // Add the controls to the upper panel
    // and the upper panel to the content pane
    upperPanel.add(controlPanel, "East");

    c.gridy = 1;
    c.weighty = 0.0; // No vertical expansion
    getContentPane().add(upperPanel, c);

    // Add labels showing the current editor kit and document
    c.gridy = 2;
    c.gridwidth = 1;
    c.weightx = 0.0;
    c.fill = GridBagConstraints.NONE;
    getContentPane().add(new JLabel("Document: ", JLabel.RIGHT), c);
    c.gridy = 3;
    getContentPane().add(new JLabel("Editor Kit:", JLabel.RIGHT), c);

    docLabel = new JLabel("", JLabel.LEFT);
    kitLabel = new JLabel("", JLabel.LEFT);
    c.gridy = 2;
    c.gridx = 1;
    c.weightx = 1.0;
    c.fill = GridBagConstraints.HORIZONTAL;
    getContentPane().add(docLabel, c);
    c.gridy = 3;
    getContentPane().add(kitLabel, c);

    // Add a listener to the button
    installButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        // Get the text and install it in the JEditorPane
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            pane.setText(textArea.getText());
          }
        });
      }
    });

    // Add listeners to the radio buttons
    ActionListener radioButtonListener = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        JRadioButton b = (JRadioButton) evt.getSource();
        String type = ((b == plainButton) ? "text/plain" : "text/html");
        final EditorKit kit = pane.getEditorKitForContentType(type);

        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            pane.setEditorKit(kit);
          }
        });
      }
    };

    // Listen to the properties of the editor pane
    pane.addPropertyChangeListener(new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
        String prop = evt.getPropertyName();
        if (prop.equals("document")) {
          docLabel.setText(evt.getNewValue().getClass().getName());
        } else if (prop.equals("editorKit")) {
          kitLabel.setText(evt.getNewValue().getClass().getName());
        }
      }
    });

    plainButton.addActionListener(radioButtonListener);
    htmlButton.addActionListener(radioButtonListener);

    // Finally, start off by selecting plain text
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        plainButton.setSelected(true);
        docLabel.setText(pane.getDocument().getClass().getName());
        kitLabel.setText(pane.getEditorKit().getClass().getName());
      }
    });
  }

  public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {}
  
    JFrame f = new EditorPaneExample2();
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent evt) {
        System.exit(0);
      }
    });
    f.setSize(500, 400);
    f.setVisible(true);
  }

  private JEditorPane pane;

  private JTextArea textArea;

  private JRadioButton plainButton;

  private JRadioButton htmlButton;

  private JLabel docLabel;

  private JLabel kitLabel;
}


           
         
    
    
    
  








Related examples in the same category

1.Viewing RTF formatViewing RTF format
2.EditorPane SampleEditorPane Sample
3.Setting the Font and Color of Text in a JTextPane Using Styles: Makes text red
4.Setting the Font and Color of Text in a JTextPane Using Styles: Inherits from "Red"; makes text red and underlined
5.Setting the Font and Color of Text in a JTextPane Using Styles: Makes text 24pts
6.Setting the Font and Color of Text in a JTextPane Using Styles: Makes text italicized
7.JEditorPane and the Swing HTML PackageJEditorPane and the Swing HTML Package
8.JEditorPane and the Swing HTML Package 3JEditorPane and the Swing HTML Package 3
9.JEditorPane and the Swing HTML Package 4JEditorPane and the Swing HTML Package 4
10.JEditorPane and the Swing HTML Package 5JEditorPane and the Swing HTML Package 5
11.JEditorPane and the Swing HTML Package 6JEditorPane and the Swing HTML Package 6
12.JEditorPane and the Swing HTML Package 7JEditorPane and the Swing HTML Package 7
13.JEditorPane and the Swing HTML Package 8JEditorPane and the Swing HTML Package 8
14.JEditorPane and the Swing HTML Package 9JEditorPane and the Swing HTML Package 9
15.EditorPane Example 10EditorPane Example 10
16.JEditorPane Example 10 - using getIteratorJEditorPane Example 10 - using getIterator
17.JEditorPane Example 11JEditorPane Example 11
18.JEditorPane Example 12JEditorPane Example 12
19.JEditorPane Example 13JEditorPane Example 13
20.JEditorPane Example 14JEditorPane Example 14
21.JEditorPane Example 15JEditorPane Example 15
22.JEditorPane Example 16JEditorPane Example 16
23.JEditorPane Example 17JEditorPane Example 17
24.JEditorPane Example 18JEditorPane Example 18
25.JEditorPane Example 19JEditorPane Example 19
26.JEditorPane Example 20JEditorPane Example 20
27.JEditorPane and HyperlinkListener Demo
28.Create a right-aligned tab stop at 200 pixels from the left margin
29.Create a center-aligned tab stop at 300 pixels from the left margin
30.Create a decimal-aligned tab stop at 400 pixels from the left margin
31.Use SimpleAttributeSet with JTextPane
32.Show html
33.Show web page
34.Change mouse cursor during mouse-over action on hyperlinks
35.Create a simple browser in Swing
36.Add colored text to the document
37.Returns the offset of the bracket matching the one at the specified offset of the document
38.Return the leading whitespace of that string, for indenting subsequent lines
39.Locates the end of the word at the specified position.
40.Locates the start of the word at the specified position.
41.Get Trailing White Space
42.This program demonstrates how to display HTML documents in an editor pane.This program demonstrates how to display HTML documents in an editor pane.