Results 1 to 5 of 5
  1. #1
    DinnerFork is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Default Prevent Text Format Changes

    I am trying to have a JTextPane automatically change the color of certain text to bold and blue when it meets certain criteria, while the rest remains black.
    Is there a way to prevent text format changes if I type next to the bold blue text when I need the rest to be black and plain?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Prevent Text Format Changes

    Moved from New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Prevent Text Format Changes

    Quote Originally Posted by DinnerFork View Post
    I am trying to have a JTextPane automatically change the color of certain text to bold and blue when it meets certain criteria, while the rest remains black.
    Is there a way to prevent text format changes if I type next to the bold blue text when I need the rest to be black and plain?
    Yes. A solution would likely involve a DocumentFilter.

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DinnerFork is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Default Re: Prevent Text Format Changes

    I will try getting a document filter to work in that case.
    This is what I wanted to use the default text style on after the inserted text.

    Java Code:
    public static void main(String[] args){
            JFrame aFrame = new JFrame();
    		aFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		aFrame.setPreferredSize(new Dimension(700,400));
    		
    		StyledDocument document = new DefaultStyledDocument();
    		
    		SimpleAttributeSet attributes = new SimpleAttributeSet();
    		StyleConstants.setUnderline(attributes,true);
    		StyleConstants.setFontSize(attributes, 14);
    		StyleConstants.setForeground(attributes, Color.BLUE);
    		StyleConstants.setBold(attributes, true);
    		
    		JTextPane tPane = new JTextPane(document);
    		try {
    			tPane.getDocument().insertString(0, "Type some text after this point:", attributes);
    		} catch (BadLocationException e) {e.printStackTrace();}
    	
    		
    		aFrame.add(tPane);
    		aFrame.pack();
    		aFrame.setVisible(true);
    }

  5. #5
    DinnerFork is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Default Re: Prevent Text Format Changes

    Thanks a lot for telling me to use DocumentFilter. After looking at the oracle tutorial, I made a subclass of DocumentFilter and overrode the replace method to have it use the attributes I wanted instead of the attributes passed to it.

Similar Threads

  1. Text file format lost in AIX
    By rak in forum Advanced Java
    Replies: 8
    Last Post: 07-27-2011, 04:39 PM
  2. Text format in JTextArea
    By goodlily in forum AWT / Swing
    Replies: 4
    Last Post: 04-11-2011, 12:57 AM
  3. Text/String format..??
    By Neullson in forum New To Java
    Replies: 14
    Last Post: 07-23-2010, 09:51 AM
  4. Format some text with Java
    By vampire in forum New To Java
    Replies: 0
    Last Post: 02-18-2010, 07:45 AM
  5. Text Format Error
    By MrFish in forum New To Java
    Replies: 2
    Last Post: 01-13-2010, 02:06 AM

Posting Permissions

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