The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.text  [49 examples] > Styles  [9 examples]

e1008. Sharing Styles Between JTextPanes

Styles are stored in a style context. By default, text panes do not share style contexts, so any styles created for a text pane are available only in that text pane. This example creates two text panes that share a style context. Any new styles created in one text pane are available in the other. This also applies to changes in a shared style.
    JTextPane c1 = new JTextPane();
    JTextPane c2 = new JTextPane();
    
    // Create new styled documents with the same style context
    StyleContext styleContext = new StyleContext();
    c1.setDocument(new DefaultStyledDocument(styleContext));
    c2.setDocument(new DefaultStyledDocument(styleContext));
    
    // Create a new style with one text pane
    Style style = c1.addStyle("style name", null);
    StyleConstants.setForeground(style, Color.red);
    
    // Modify an existing style using the other text pane
    style = c2.getStyle("style name");
    StyleConstants.setBold(style, true);

 Related Examples
e1006. Highlighting Words in a JTextComponent
e1007. Setting the Font and Color of Text in a JTextPane Using Styles
e1009. Listing the Styles Associated with a JTextPane
e1010. Listing the Attributes in a Style
e1011. Using a Popup to Apply Styles to a JTextPane
e1012. Retaining the Logical Style When Setting a New Paragraph Style
e1013. Automatically Updating Styled Text When a Style Is Updated
e1014. Determining If a Style Attribute Applies to a Character or the Paragraph

See also: Actions and Key Bindings    Caret and Selection    Events    JEditorPane    JFormattedTextField    JTextArea    JTextComponent    JTextField    JTextPane   


© 2002 Addison-Wesley.