2

I'm trying to BOLD the text in JPane but it not working. If it ITALIC or UNDERLINE its working fine. Below is my code.

String strText = "<b><i><u>Testing</b></i></u>";
javax.swing.text.Style style1 = jTextPane1.addStyle("I'm a Style", null);
StyleConstants.setForeground(style1, Color.BLACK);
StyleConstants.setFontSize(style1, 15);
StyleConstants.setFontFamily(style1, "Arial Unicode MS");
if(strText.contains("<b>"))
                        {
                            StyleConstants.setBold(style1, true);
                            strText = strText.replace("<b>", " ");
                            strText = strText.replace("</b>", " ");

                        }
                        if(strText.contains("<i>"))
                        {
                            StyleConstants.setItalic(style1, true);
                            strText = strText.replace("<i>", " ");
                            strText = strText.replace("</i>", " ");

                        }
                        if(strText.contains("<u>"))
                        {
                            StyleConstants.setUnderline(style1, true);
                            strText = strText.replace("<u>", " ");
                            strText = strText.replace("</u>", " ");

                        }
try {
strText = strText.trim();
doc.insertString(doc.getLength(), strText, style1);
} 

The UNDERLINE and ITALIC working fine meanwhile BOLD not working as per expected. The text not BOLDED. Please advice where i did mistakes.

Sled
  • 17,743
  • 27
  • 115
  • 155
chinna_82
  • 6,177
  • 17
  • 76
  • 131

1 Answers1

3

Based on this example, the fragment below illustrates three related styles based on the same font and size:

image

SimpleAttributeSet normal = new SimpleAttributeSet();
StyleConstants.setFontFamily(normal, "SansSerif");
StyleConstants.setFontSize(normal, 16);

SimpleAttributeSet bold = new SimpleAttributeSet(normal);
StyleConstants.setBold(bold, true);

SimpleAttributeSet italic = new SimpleAttributeSet(normal);
StyleConstants.setItalic(italic, true);

doc.insertString(doc.getLength(), s + "\n", normal);
doc.insertString(doc.getLength(), s + "\n", bold);
doc.insertString(doc.getLength(), s + "\n", italic);
Community
  • 1
  • 1
trashgod
  • 200,320
  • 28
  • 229
  • 974
  • you are printing 3 line with 3 different style. How i do if i want to print 1 line with BOLD and ITALIC at the same time. – chinna_82 Nov 25 '13 at 06:44
  • You can repeatedly append the text without a newline to get different styles on the same line. – trashgod Nov 25 '13 at 14:04