Results 1 to 3 of 3
  1. #1
    jkhoa is offline Member
    Join Date
    Aug 2007
    Posts
    3
    Rep Power
    0

    Default Preventing inserted text from becoming colored from previous style

    I have a jtextpane I have alreadly colored in black, and red. Red is for the text inside the brackets. This seems to work, but the problem is when I insert text imediately next to a red bracket, the next text that comes out is red as well, while it should be black. Right now what I'm doing to fix the problem is coloring everything black first then color in the red, but that makes the jtextpane blinks between two colors. I was wondering if there is some simpler way to prevent the style from being "contagious".

    thanks in advance.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    This seems to work okay.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class StyleIntegrity {
        JTextPane textPane;
    
        public StyleIntegrity() {
            System.setProperty("swing.aatext", "true");
            String text = "This component models paragraphs that are composed of " +
                "runs of character level attributes. Each paragraph may have a " +
                "logical style attached to it which contains the default attributes " +
                "to use if not overridden by attributes set on the paragraph or " +
                "character run.";
            textPane = new JTextPane();
            StyledDocument doc = textPane.getStyledDocument();
            createStyles(doc);
            setContent(doc, text);
            styleContent(doc);
        }
    
        private void createStyles(StyledDocument doc) {
            Style baseStyle = doc.addStyle("base", null);
            StyleConstants.setFontFamily(baseStyle, "Lucida Sans Unicode");
            StyleConstants.setFontSize(baseStyle, 18);
            StyleConstants.setFirstLineIndent(baseStyle, 20f);
            StyleConstants.setLeftIndent(baseStyle, 10f);
    
            Style style = doc.addStyle("red", baseStyle);
            StyleConstants.setForeground(style, Color.red);
        }
    
        private void setContent(StyledDocument doc, String text) {
            try {
                doc.insertString(0, text, doc.getStyle("base"));
            } catch(BadLocationException e) {
                System.out.printf("Bad location error: %s%n", e.getMessage());
            }
        }
    
        private void styleContent(StyledDocument doc) {
            Style style = doc.getStyle("base");
            doc.setLogicalStyle(0, style);
            style = doc.getStyle("red");
            doc.setCharacterAttributes(116, 66, style, false);
        }
    
        private JMenuBar getMenuBar() {
            JMenu edit = new JMenu("Edit");
            edit.add(new DefaultEditorKit.CutAction());
            edit.add(new DefaultEditorKit.CopyAction());
            edit.add(new DefaultEditorKit.PasteAction());
            edit.getItem(0).setText("cut");
            edit.getItem(1).setText("copy");
            edit.getItem(2).setText("paste");
            JMenuBar menuBar = new JMenuBar();
            menuBar.add(edit);
            return menuBar;
        }
    
        private JScrollPane getContent() {
            return new JScrollPane(textPane);
        }
    
        public static void main(String[] args) {
            StyleIntegrity test = new StyleIntegrity();
            JFrame f = new JFrame();
            f.setJMenuBar(test.getMenuBar());
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(test.getContent());
            f.setSize(500,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  3. #3
    jkhoa is offline Member
    Join Date
    Aug 2007
    Posts
    3
    Rep Power
    0

Similar Threads

  1. NetBeans-style Dockable-windows and toolbar?
    By jimm1 in forum Advanced Java
    Replies: 0
    Last Post: 01-31-2008, 08:41 PM
  2. Using SWT Text
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-10-2008, 10:41 AM
  3. how to set indentation to a text
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:42 PM
  4. Using previous with CardLayout
    By uncopywritable in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 09:43 PM
  5. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •