Using a Popup to Apply Styles to a JTextPane

This example implements a text pane that allows the user to display a popup of available styles. If the user selects a style in the popup, the characters in the selection are formatted with the attributes in the style.
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; // Construct a sorted list of style names DefaultStyledDocument doc = (DefaultStyledDocument)textPane.getDocument(); java.util.List l = new ArrayList(); Enumeration enum = doc.getStyleNames(); while (enum.hasMoreElements()) { l.add(enum.nextElement()); } Collections.sort(l); // First sub menu applies character attributes 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); // Second sub menu applies paragraph attributes 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); // Third submenu applies logical attributes 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); // Add a listener to display pop-up 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()); } } }); static final int CHARACTER = 1; static final int PARAGRAPH = 2; static final int LOGICAL = 3; // Assumes the style name is the same as the action name. // The type specifies how the style should be applied. // The style is applied to the text within the selection. public static class DoStyleAction extends StyledEditorKit.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 CHARACTER: c.setCharacterAttributes(doc.getStyle(styleName), true); break; case PARAGRAPH: c.setParagraphAttributes(doc.getStyle(styleName), true); break; case LOGICAL: c.setLogicalStyle(doc.getStyle(styleName)); break; } } } }

Comments

3 Sep 2010 - 1:02am by Anonymous (not verified)

A chanel coco handbags is not only a sign of taste and fashion; it is also a symbol of social status.chanel hand bags in such a way society.We should bring you great chanel handbags online. chanel bag. www.buycocochanel.com is an online luxury chanel handbags retailer. bring together the most fashionable designer chanel handbags,coco chanel handbags, UGG Roxy Tall, authentic chanel handbags, coach sneakers, cheap chanel handbags,UGG Bailey Button Tripletchanel handbag authentic, buy chanel online, discount chanel handbags,chanel purses, discount chanel bags,chanel 2.55 handbag,chanel flap bags,chanel 2.55 bag,chanel tote, chanel cambon, black chanel bag, chanel bag onlinevintage chanel handbags.Latest designs with vintage chanel handbags,black chanel handbags,chanel tote bags and classic chanel bags .You don't have to go anywhere else to find your favorite chanel 2010 handbags. Our chanel bags online offers you a fabulous selection with the most beautiful to buy coco chanel,wholesale chanel handbags,chanel handbags wholesale,chanel cambon bag, chanel designer handbags, chanel handbags sale, chanel tote black, black tote chanelnew chanel handbags and the chanel handbags black. chanel handbags, vibram fivefingers running shoes is a type of shoe manufactured by Vibram.vibram five finger running shoes is becoming popular. The shoe now comes in several varieties as five fingers classic,vibram fivefingers trek,vvibram fivefingers flow,vibram fivefingers kso trek,vibram five finger flow,vibram five fingers kso mens,vibram five fingers classic mens,vibram five fingers moc,vibram five fingers climbing, and vibram five finger toe shoes, many of which incorporate special features, They claim that a "barefoot" style running and walking is safer, www.fivefingervibrams.com is our online vibram five fingers store.We are the vibram five fingers retailers and we are a large quantity of vibram five fingers stockists.We are providing the highest quality vibram five fingers shoes with the lowest price to you with free shipping worldwide.Here is the best choice to buy discount five fingers shoes for vibram five finger classic,vibram five fingers running,vibram five fingers trek,vibram running shoesand vibram five fingers classic.
Many suppliers of vibram five fingers online cannot get their goods by customs due to their lack of skills in five fingers shoes sale,vibram five fingers kso sale,vibram shoes sale,vibram five fingers flow sale, vibram five finger shoes sale and vibram five fingers clearance.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.