Change the background color of a JTEXTAREA text

Asked

Viewed 445 times

0

I am a beginner in Java and I am trying to create a function that changes the background of words, I took a print of the example as I would like it to be. What is expected is for a table to be shown, but the separation is by comma. If anyone can help me, I’d appreciate it.

inserir a descrição da imagem aqui

  • 1

    I closed it as a duplicate because that’s not possible with Jtextarea, as it says documentation. But in the linked answer is exemplified how to do the same thing with the appropriate component for this.

1 answer

0

To give a table effect, you will have to create a class that will inherit the DefaultTableCellRenderer and overwrite the method getTableCellRendererComponent

You can do as in the example below, in this it changes beyond the background, the edge and the source.

private class TableRowHeaderCellRenderer extends DefaultTableCellRenderer {
 @Override
  public Component getTableCellRendererComponent(
                    JTable table, Object value, boolean isSelected,
                    boolean hasFocus, int row, int column) {
        setFont(new java.awt.Font("Arial Black", 0, 21 ));
        setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
        setBackground(new Color(244, 242, 228));
        setValue((String) value);
        return this;
    }
}
  • Tablecellrenderer in a Jtextarea? Are you sure it works?

  • Opa valeu... But would not have a way to change only the background color of a few words of the line? I do not want to create a table for this. Thank you so much for helping me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.