Вы пытались установить текст HTML? Я считаю, что JTextPane поддерживает HTML, поэтому попробуйте установить для своего текста что-то вроде:
myTextPane.setText("<html>This text box has <b>bold text</b> in it!</html>");
ИЛИ
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class SimpleAttributeBoldItalic {
public static void main(String args[]) {
JFrame frame = new JFrame("Simple Attributes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
StyledDocument document = new DefaultStyledDocument();
SimpleAttributeSet attributes = new SimpleAttributeSet();
attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.TRUE);
attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.TRUE);
try {
document.insertString(document.getLength(), "Bold, Italic", attributes);
} catch (BadLocationException badLocationException) {
System.err.println("Bad insert");
}
JTextPane textPane = new JTextPane(document);
textPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textPane);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
ИЛИ
Font boldFont=new Font(textArea.getFont().getName(), Font.BOLD, textArea.getFont().getSize());
textArea.setFont(boldFont); // bold text
ИЛИ
Поскольку вы используете JTextPane, вы должны использовать SimpleAttributeSet:
SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setUnderline(attributeSet, true);
jta.getStyledDocument().setCharacterAttributes(0, jta.getText().length(),
attributeSet, false);
Вот так.