简体   繁体   中英

How to make only part of the text bold

Can you tell me how I can make only part of the text bold:

TextArea dataPane = new TextArea();        
        dataPane.appendText("Product Version: " + "1.0");
        dataPane.appendText("\nJava: " + "7");
        dataPane.appendText("\nRuntime: " + "7");
        dataPane.appendText("\nSystem: " + "Linux");
        dataPane.appendText("\nUser directory: " + "/home/dir");

I want to make only these strings bold: Product Version, Java, Runtime, System, User directory ? What will be the easiest way to do this?

UPDATE

I also tested this code:

TextArea dataPane = new TextArea();

    dataPane.setPrefRowCount(10);
    Text wd = new Text("Version");
    wd.setFont(Font.font ("Verdana", FontWeight.BOLD, 20));

    dataPane.appendText(wd + "1.0");
    dataPane.appendText("\nJava: " + "7");
    dataPane.appendText("\nRuntime: " + "7");
    dataPane.appendText("\nSystem: " + "Linux");
    dataPane.appendText("\nUser directory: " + "/home/dir");

I get this: Text@149e84241.0

The text is not properly formatted.

You could use an editor pane with the content type set to html instead of a text area. I threw an editor pane into a scratch project really quick and just used the default variable names and object properties. Here's how I produced what I believe you're looking for:

jEditorPane1.setContentType("text/html"); //by default this is text/plain
//use margin-top and margin-bottom to prevent gaps between paragraphs
//or actually customize them to create space if you desire so
jEditorPane1.setText("<p style='margin-top:0pt;'><b>Product Version:</b> 1.0</p>" + 
    "<p style='margin-top:0pt; margin-bottom:0pt;'><b>Java:</b> 7</p>" +
    "<p style='margin-top:0pt; margin-bottom:0pt;'><b>Runtime:</b> 7</p>" +
    "<p style='margin-top:0pt; margin-bottom:0pt;'><b>System:</b> Linux</p>" +
    "<p style='margin-top:0pt; margin-bottom:0pt;'><b>User directory:</b> /home/dir</p>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM