I have a string in a textbox and want only one of the words to be in bold. Is there a way to do that in code without appending the text? Sort of like how it would be done in xml/html... How about an underline, too?
开发者_Python百科Prefer not using xml or html for this - prefer to keep it a string in java code...
Thanks
Many Swing components already understand a subset of HTML as long as you surround the text in <html>...</html>
.
I like using a JTextPane with attributes:
JTextPane textPane = new JTextPane();
textPane.setText( "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight" );
StyledDocument doc = textPane.getStyledDocument();
// Define a keyword attribute
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBold(keyWord, true);
// Change attributes on some text
doc.setCharacterAttributes(4, 3, keyWord, false);
精彩评论