JTextPane - phrase with two styles

I just ran into something interesting.

I changed the selected text style. The fact is that I change the style for ONE WORD one by one, but then, if I select a whole stylized phrase and change its font color, the whole phrase will turn into ONE style (the first style in the selected text) :(

Here is a snippet of the problem

private void setFontColorStyle() { JTextPane editor=this.getTextPane(); String text=this.getTextPane().getSelectedText(); StyledDocument doc=(StyledDocument) editor.getDocument(); int selectionEnd=this.getTextPane().getSelectionEnd(); int selectionStart=this.getTextPane().getSelectionStart(); Element element=doc.getCharacterElement(selectionStart); AttributeSet as = element.getAttributes(); String family = StyleConstants.getFontFamily(as); int fontSize = StyleConstants.getFontSize(as); boolean isBold=StyleConstants.isBold(as); boolean isItalic=StyleConstants.isItalic(as); boolean isUnderlined=StyleConstants.isUnderline(as); StyleContext context = new StyleContext(); Style style; this.getTextPane().replaceSelection(""); style = context.addStyle("mystyle", null); style.addAttribute(StyleConstants.FontSize, fontSize); style.addAttribute(StyleConstants.FontFamily, family); style.addAttribute(StyleConstants.Foreground, this.fontColor); style.addAttribute(StyleConstants.Bold, isBold); style.addAttribute(StyleConstants.Italic, isItalic); style.addAttribute(StyleConstants.Underline, isUnderlined); this.getTextPane().replaceSelection(""); try { this.getTextPane().getStyledDocument().insertString(selectionEnd - text.length(), text, style); } catch (BadLocationException ex) { } } 

And here’s the bold code of the creation method ... () Italics and the same logic are underlined, so I think it’s completely clear.

 private void setFontBoldStyle() { if(this.getTextPane().getSelectedText()!=null) { String text = this.getTextPane().getSelectedText(); int selectionStart=this.getTextPane().getSelectionStart(); int selectionEnd=this.getTextPane().getSelectionEnd(); StyleContext context = new StyleContext(); Style style; Element element=doc.getCharacterElement(selectionStart); Enumeration en=doc.getStyleNames(); AttributeSet as = element.getAttributes(); /** * Get style from history... */ String family = StyleConstants.getFontFamily(as); int fontSize = StyleConstants.getFontSize(as); Color currentColor=StyleConstants.getForeground(as); boolean isBold=StyleConstants.isBold(as)?false:true; boolean isItalic=StyleConstants.isItalic(as); boolean isUnderlined=StyleConstants.isUnderline(as); String styleName=String.valueOf(Math.random()); style = context.addStyle(styleName, null); // style.addAttribute(StyleConstants.FontSize, fontSize); // style.addAttribute(StyleConstants.FontFamily, family); style.addAttribute(StyleConstants.Foreground, currentColor); style.addAttribute(StyleConstants.FontFamily, family); style.addAttribute(StyleConstants.FontSize, fontSize); style.addAttribute(StyleConstants.Bold, isBold); style.addAttribute(StyleConstants.Italic, isItalic); style.addAttribute(StyleConstants.Underline, isUnderlined); this.getTextPane().replaceSelection(""); try { this.getTextPane().getStyledDocument().insertString(selectionEnd - text.length(), text, style); } catch (BadLocationException ex) { } }//if end... } 

Here is the bold method call code:

 private void setFontBold() { this.setFontBoldStyle(); } 

... and the color method call

  private void setFontColor(Color fontColor) { this.fontColor=fontColor; this.setFontColorStyle(); } 

... and action listeners (in bold) ...

  private void boldButtonActionPerformed(java.awt.event.ActionEvent evt) { this.getTextPane().requestFocusInWindow(); this.setFontBold(); } 

... and for color

 private void colorButtonActionPerformed(java.awt.event.ActionEvent evt) { this.getTextPane().requestFocusInWindow(); ColorDialog colorEditor=new ColorDialog(); //returns rgb color... Color color=colorEditor.getSelectedColor(this.getDialog(), true,false); if(color==null){ JOptionPane.showMessageDialog(this.getDialog(), "null color"); return; } this.setFontColor(color); } 

I really need your advice on how to keep selected text styles unchanged (for example, in bold or a font family) when I want, for example, to change the entire text style with a selected color?

To be more clear ...

For example, I have text

My Hello World is not very :)

Then I select the whole phrase and change its color from black to red. The following text turns red, but the whole phrase becomes bold according to the first style. But it would be interesting to preserve bold and italic styles, but at the same time have a red phrase :) It's so simple, but I just confused how to manage several styles within the selected area of ​​the text?

Any helpful comment is greatly appreciated.

+7
source share
2 answers

TextComponentDemo , discussed in How to Use Editor Tabs and Text Panels , is a good example of how to manage this, as well as other functions of text components.

Application: TextComponentDemo uses predefined Action objects to handle editing tasks. Conveniently, StyledEditorKit contains a series of nested classes that derive from StyledTextAction . As a specific example, here you can add AlignmentAction to the Style menu of the TextComponentDemo in the createStyleMenu() method:

 protected JMenu createStyleMenu() { JMenu menu = new JMenu("Style"); Action action = new StyledEditorKit.AlignmentAction( "left-justify", StyleConstants.ALIGN_LEFT); action.putValue(Action.NAME, "Left"); menu.add(action); menu.addSeparator(); ... } 

The remaining (arbitrary) names of the alignment actions are defined confidentially in StyledEditorKit .

Application: setCharacterAttributes() is the general procedure used by nested editing actions. It calls a method with the same name in StyledDocument , proposed by @StanislavL.

Addendum: I cannot reproduce the effect you described. When I set the highlight color, the style attributes remain unchanged.

Appendix: StyledEditorKit actions StyledEditorKit work with JButton or JToolBar .

 new JButton(new StyledEditorKit.ForegroundAction("Red", Color.red)) 

TextComponentDemo

+3
source

Use this.getTextPane (). getStyledDocument (). setCharacterAttributes ()

+3
source

All Articles