开发者

Java Text String: Want Only One Word In Bold

开发者 https://www.devze.com 2023-01-21 14:42 出处:网络
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 und

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消