Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

List all style attributes in a JTextPane  RSS feed

 
Diederick de Vries
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am attempting to create a text editor which should allow the user to type CTRL+B to type in boldface, CTRL+I for italics and CTRL+U for underlined (code below). When I want to store the text I call getDocument(). It returns the text, but doesn't tell me which attributes are used on which character. Since editorPane seems to remember these, I'm sure there must be a list of some form somewhere. Can someone tell me how I can get to it?

Thanks for any help!


 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Diederick de Vries wrote:I am attempting to create a text editor which should allow the user to type CTRL+B to type in boldface, CTRL+I for italics and CTRL+U for underlined (code below).


Fyi, JTextPane's action map comes prepopulated with Actions for "font-bold", "font-italic", and "font-underline". Unless you want the thrill of implementing them yourself, you might as well use them. It's this easy:

inputMap.put(ctrlb, "font-bold");
inputMap.put(ctrli, "font-italic");
inputMap.put(ctrlu, "font-underline");


Btw, instead of hard-coding KeyEvent.CTRL_MASK you may want to use Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(). That way you automatically get CTRL-B on windows but CMD-B on mac.

If you do want to implement it yourself, then your code may need some help. (You must be setting the boldStyle/italicStyle/underlineStyle fields elsewhere, right, or else you're throwing NPEs?) Some comments:

  • Your code adds Actions directly to the inputMap, but that is not the way it's usually done. Typically you add your Action to the actionMap with a key of your choosing, then you map something to that same key in the inputMap. (The key itself is arbitrary. You could decide, for example, to use the String "my-font-bold" for the key.)


  • You may find it easier to extend TextAction rather than Action. It has a handy getTextComponent() method that allows you to create the Action once and use it on multiple text components.


  • Why does your TextEditor class extend JPanel? You don't seem to be using its panelness for anything.




  • When I want to store the text I call getDocument(). It returns the text, but doesn't tell me which attributes are used on which character. Since editorPane seems to remember these, I'm sure there must be a list of some form somewhere. Can someone tell me how I can get to it?


    You seem to be confusing the JTextPane's document with it's text.

    Text is text, but the Document contains the text plus also the style information. The setDocument()/getDocument() methods you wrote actually set/get the text, not the Document, so I recommend you rename them.

    Anyway, to see the attributes for a specific character, first do something like: StyledDocument sdoc = editorPane.getStyledDocument();
    Then examine the AttributeSets returned by:
  • sdoc.getLogicalStyle(charIndex)
  • sdoc.getCharacterElement(charIndex).getAttributes()
  • sdoc.getParagraphElement(charIndex).getAttributes()


  •  
    Diederick de Vries
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a lot for your very thorough answer. This completely solved my question.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!