0

I have a JTextPane for users to write journal entries which they can style by selecting text and choosing a menu item such as bold or italic etc. These items are connected to one of the Styled Editor kits (eg StyledEditorKit.BoldAction() ).

Is there any way of detecting whether text at a given document position has been styled with one of these kits? And if so how?

       //Create the style menu.
protected JMenu createStyleMenu() {
    JMenu menu = new JMenu("Style");

    Action action = new StyledEditorKit.BoldAction();  
    action.putValue(Action.NAME, "Bold");        
    menu.add(action);

    action = new StyledEditorKit.ItalicAction();
    action.putValue(Action.NAME, "Italic");
    menu.add(action);

    action = new StyledEditorKit.UnderlineAction();
    action.putValue(Action.NAME, "Underline");
    menu.add(action);

    menu.addSeparator();

    menu.add(new StyledEditorKit.FontSizeAction("12", 12));
    menu.add(new StyledEditorKit.FontSizeAction("14", 14));
    menu.add(new StyledEditorKit.FontSizeAction("18", 18));
    menu.add(new StyledEditorKit.FontSizeAction("36", 36));

    menu.addSeparator();

    menu.add(new StyledEditorKit.FontFamilyAction("Serif",
                                                  "Serif"));
    menu.add(new StyledEditorKit.FontFamilyAction("SansSerif",
                                                  "SansSerif"));

    menu.addSeparator();

    menu.add(new StyledEditorKit.ForegroundAction("Red",
                                                  Color.red));
    menu.add(new StyledEditorKit.ForegroundAction("Green",
                                                  Color.green));
    menu.add(new StyledEditorKit.ForegroundAction("Blue",
                                                  Color.blue));
    menu.add(new StyledEditorKit.ForegroundAction("Black",
                                                  Color.black));

    return menu;
}

Any help would be much appreciated. Thank you.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

2

You can query the attributes of a certain character of your document:

StyledDocument doc = (StyledDocument)textPane.getDocument();
Element element = doc.getCharacterElement(position);

Boolean isItalic = element.getAttributes().getAttribute(StyleConstants.Italic);
isnot2bad
  • 24,105
  • 2
  • 29
  • 50
  • Thanks for the info. I had trouble with the syntax in the 2nd and 3rd lines. However, I got the general idea of what you were suggesting and came up with this code based on what you suggested:`StyledDocument doc = (StyledDocument) gui.txpEntryText.getDocument(); Element element = doc.getCharacterElement(1); System.out.print("Bold = " + element.getAttributes().getAttribute(StyleConstants.Bold))`; – Justin Farrell Dec 02 '13 at 18:25
  • Sorry, I just saw that there was a mistake in my code (already fixed it). Note that you might also use the other methods `getParagraphElement` and `getLogicalStyle`. – isnot2bad Dec 02 '13 at 20:04
  • No problem. I found your code really helpful (thanks also for the additional info). There was a bit of an issue with the last line of code in your original reply. This part of it was fine - `element.getAttributes().getAttribute(StyleConstants.Italic);`, but it would not let me assign it to a boolean variable. It said the types were incompatible (which was strange since it returned a true/false value). It let me do this, however, - `if (elmt.getAttributes().getAttribute(StyleConstants.Bold) == true) strFormattingData[0] = "true";` - so I got round it that way. – Justin Farrell Dec 04 '13 at 19:04