简体   繁体   中英

Set specific text a color in a JEditorPane

Honestly, I have searched for this. I found some answer here and I clearly don't understand because i can't reproduce the color in my code.

I want specific lines to be in red. I've used a JEditorPane and Style but i just don't get it.

Here's the whole code

public class Window extends JFrame implements ActionListener{

private JButton theButton = new JButton("Calculer sur 100");
private JTextPane text = new JTextPane();
private JTextField textField = new JTextField("Écrire un nombre");
private JScrollPane scroller = new JScrollPane(text);
private StringBuilder sb = new StringBuilder();
private StyledDocument doc = text.getStyledDocument();
private Style style;

public Window() {
    setLayout(new BorderLayout());
    setTitle("Test");
    setSize(400, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    theButton.addActionListener(this);

    getContentPane().add(scroller, BorderLayout.CENTER);
    getContentPane().add(textField, BorderLayout.NORTH);
    getContentPane().add(theButton, BorderLayout.SOUTH);
    setVisible(true);
}

/**
 * Prints the result on text
 * @param num
 */
private void print100(int num) {
    for (int i = 1; i < num + 1; i++) {
        text.setText(appendString(i));
    }
}

/**
 * convert the number to string 
 * @param i
 * @return a string that contains the information
 */
private String appendString(int i){
    double a = doMath(i, checkForNumber());

    String s = "";
    if(a < 60){
        style = text.addStyle("red", null);
        StyleConstants.setForeground(style, Color.red);
        s = "\n" + i + " : " + a;//This need to be red
    }
    else
        s = "\n" + i + " : " + a;

    return sb.append(s).toString();
}

/**
* Check if the text in the text is numbers
* return numl
*/
private int checkForNumber() {
    int numl;
    try {
        numl = Integer.parseInt(textField.getText());
    } catch (NumberFormatException e) {
        text.setText("Essayer avec des nombres...");
        return 0;
    }
    return numl;
}

/**
* leave specific number of digit after the dot
* return myNum
*/
private double doMath(int i, int num) {
    double myNum = ((double) i / num) * 100;
    int precision = 100; //Number of zero = number of digits
    myNum = Math.floor(myNum * precision + .5) / precision;
    return myNum;
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == theButton) {
        text.setText("");
        print100(checkForNumber());
    }
}

}

How can i make all the specific line to appear red in the JEditorPane ?

So i got my problem figured out.

First off, I was using a JTextPane instead of a JEditorPane as pointed out by MadProgrammer.

You see, it's not a JEditorPane, it's a JTextPane, that changes things

As you might know, JEditorPane can accept HTML statement such as <span> in which you can change the color of your text.

I've changed those line :

style = text.addStyle("red", null);
    StyleConstants.setForeground(style, Color.red);
    s = "\n" + i + " : " + a;//This need to be red

to :

s = "<br><span style=\"color:red\">" + i + " : " + a + "</span>";

And it all worked.

Here is the whole code :

public class Window extends JFrame implements ActionListener{

private JButton theButton = new JButton("Calculer sur 100");
//Change the next line to have a JEditorPane
private JEditorPane text = new JEditorPane();
private JTextField textField = new JTextField("Écrire un nombre");
private JScrollPane scroller = new JScrollPane(text);
private StringBuilder sb = new StringBuilder();
private Style style;

public Window() {
    setLayout(new BorderLayout());
    setTitle("Test");
    setSize(400, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    //This line was added
    text.setContentType("text/html");

    theButton.addActionListener(this);

    getContentPane().add(scroller, BorderLayout.CENTER);
    getContentPane().add(textField, BorderLayout.NORTH);
    getContentPane().add(theButton, BorderLayout.SOUTH);
    setVisible(true);
}

/**
 * Prints the result on text
 * @param num
 */
private void print100(int num) {
    for (int i = 1; i < num + 1; i++) {
        text.setText(appendString(i));
    }
}

/**
 * convert the number to string 
 * @param i
 * @return a string that contains the information
 */
private String appendString(int i){
    double a = doMath(i, checkForNumber());

    String s = "";
    if(a < 60){
        //This line changed
        s = "<br><span style=\"color:red\">" + i + " : " + a + "</span>";//This need to be red
    }
    else
        s = "<br>" + i + " : " + a;

    return sb.append(s).toString();
}

/**
* Check if the text in the text is numbers
* return numl
*/
private int checkForNumber() {
    int numl;
    try {
        numl = Integer.parseInt(textField.getText());
    } catch (NumberFormatException e) {
        text.setText("Essayer avec des nombres...");
        return 0;
    }
    return numl;
}

/**
* leave specific number of digit after the dot
* return myNum
*/
private double doMath(int i, int num) {
    double myNum = ((double) i / num) * 100;
    int precision = 100; //Number of zero = number of digits
    myNum = Math.floor(myNum * precision + .5) / precision;
    return myNum;
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == theButton) {
        text.setText("");
        print100(checkForNumber());
    }
}

}

ps You can change <span style=\\"color:red\\"> to <font color = FF0000> and it should also work.

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