Using a Popup to Apply Styles to a JTextPane - Java Swing

Java examples for Swing:JTextPane

Description

Using a Popup to Apply Styles to a JTextPane

Demo Code


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextPane;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit.StyledTextAction;

public class Main {
  static final int CHARACTER = 1;
  static final int PARAGRAPH = 2;
  static final int LOGICAL = 3;
  
  public void main(String[] argv) {
    JTextPane textPane = new JTextPane();

    // Makes text red
    Style style = textPane.addStyle("Red", null);
    StyleConstants.setForeground(style, Color.red);

    // Inherits from "Red"; makes text red and underlined
    style = textPane.addStyle("Red Underline", style);
    StyleConstants.setUnderline(style, true);

    // Makes text 24pts
    style = textPane.addStyle("24pts", null);
    StyleConstants.setFontSize(style, 24);

    // Makes text 12pts
    style = textPane.addStyle("12pts", null);
    StyleConstants.setFontSize(style, 12);

    // Makes text italicized
    style = textPane.addStyle("Italics", null);
    StyleConstants.setItalic(style, true);

    // Makes text bold
    style = textPane.addStyle("Bold", null);
    StyleConstants.setBold(style, true);

    JTextPane c = textPane;/*from  ww  w .  ja va2s.  c o m*/
    DefaultStyledDocument doc = (DefaultStyledDocument) textPane.getDocument();
    java.util.List l = new ArrayList();
    Enumeration e = doc.getStyleNames();
    while (e.hasMoreElements()) {
      l.add(e.nextElement());
    }
    Collections.sort(l);

    final JPopupMenu menu = new JPopupMenu();
    JMenu submenu = new JMenu("Character");
    for (int i = 0; i < l.size(); i++) {
      submenu.add(new JMenuItem(new DoStyleAction((String) l.get(i), CHARACTER)));
    }
    menu.add(submenu);

    submenu = new JMenu("Paragraph");
    for (int i = 0; i < l.size(); i++) {
      submenu
          .add(new JMenuItem(new DoStyleAction((String) l.get(i), PARAGRAPH)));
    }
    menu.add(submenu);

    submenu = new JMenu("Logical");
    for (int i = 0; i < l.size(); i++) {
      submenu.add(new JMenuItem(new DoStyleAction((String) l.get(i), LOGICAL)));
    }
    menu.add(submenu);

    textPane.addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent evt) {
        if (evt.isPopupTrigger()) {
          menu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
      }

      public void mouseReleased(MouseEvent evt) {
        if (evt.isPopupTrigger()) {
          menu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
      }
    });
  }
}
class DoStyleAction extends StyledTextAction {
  int type;

  public DoStyleAction(String styleName, int type) {
    super(styleName);
    this.type = type;
  }

  public void actionPerformed(ActionEvent e) {
    JTextPane c = (JTextPane) getEditor(e);
    if (c != null) {
      String styleName = e.getActionCommand();
      StyledDocument doc = (StyledDocument) c.getDocument();

      switch (type) {
      case Main.CHARACTER:
        c.setCharacterAttributes(doc.getStyle(styleName), true);
        break;
      case Main.PARAGRAPH:
        c.setParagraphAttributes(doc.getStyle(styleName), true);
        break;
      case Main.LOGICAL:
        c.setLogicalStyle(doc.getStyle(styleName));
        break;
      }
    }
  }
}

Related Tutorials