How can I display red text in JTextArea?

I want to show an error (text) as a result of red after compiling the exec file and displaying it in textarea gui using swing in java.

+3
source share
4 answers

Regular JTextArea does not support fantastic things, such as different text colors. However, there are similar components. See http://java.sun.com/docs/books/tutorial/uiswing/components/text.html

+2
source

JEditorPane can receive HTML formatted content. Sun's official tutorial also gives some insight:

JTextArea , , , . , . , , . , .

+2

JEditorPane AttributeSet StyleConstants.

JEditorPane, HTML.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import javax.swing.border.*;

public class TextColor extends JFrame implements ActionListener {

  JTextPane myTextPane;
  JTextArea inputTextArea;

  public TextColor() {
    super();
    JPanel temp = new JPanel(new BorderLayout());
    inputTextArea = new JTextArea();
    JButton btn = new JButton("Add");
    btn.addActionListener(this);
    temp.add(btn, BorderLayout.SOUTH);
    temp.add(inputTextArea, BorderLayout.NORTH);
    this.getContentPane().add(temp, BorderLayout.SOUTH);
    myTextPane = new JTextPane();
    myTextPane.setBorder(new EtchedBorder());
    this.getContentPane().add(myTextPane, BorderLayout.CENTER);
    this.setSize(600, 600);
    this.setVisible(true);


  }

  public void actionPerformed(ActionEvent ae) {
    Color newTextColor = JColorChooser.showDialog(this, "Choose a Color", Color.black);
    //here is where we change the colors
    SimpleAttributeSet sas = new SimpleAttributeSet(myTextPane.getCharacterAttributes());
    StyleConstants.setForeground(sas, newTextColor);
    try {
      myTextPane.getDocument().insertString(myTextPane.getDocument().getLength(),
          inputTextArea.getText(), sas);
    } catch (BadLocationException ble) {
      ble.printStackTrace();
    }

  }

  public static void main(String args[]) {

    new TextColor();
  }

}
+1

Smita,

take care to insert the code snippet so that you can understand exactly where the problem is or need help.

Getting to your problem

As far as I know, there is no way to set different colors for different text elements in textArea in java. You can set only one color for all.

An alternative is to use JTextPane.

See if the following code helps your case.

String text = "Some Text...";    //This can be any piece of string in your code like 
                                   output of your program...
JTextPane myTextPane = new JTextPane();

SimpleAttributeSet sas = new SimpleAttributeSet(myTextPane.getCharacterAttributes());


// As what error you were referring was not clear, I assume there is some code in your    
   program which pops out some error statement. For convenience I use Exception here..
if( text.contains("Exception") ) //Checking if your output contains Exception...
{
    StyleConstants.setForeground(sas, Color.red); //Changing the color of 
    StyleConstants.setItalic(sas, true);

    try
    {
       myTextPane.getDocument().insertString
       (
          myTextPane.getDocument().getLength(),
          text + "\n",
          sas
       );
    }
    catch( BadLocationException ble )
    {
        text.append(ble.getMessage());
    }
}
else
{
    StyleConstants.setForeground(sas, Color.GREEN);

    try
    {
       myTextPane.getDocument().insertString
       (
          myTextPane.getDocument().getLength(),
          text + "\n",
          sas
        );
    }
    catch(BadLocationException ble)
    {
        text.append(ble.getMessage());
    }
}

I think this will solve your problem with a few changes.

Thank.

Sushil

0
source

Source: https://habr.com/ru/post/1735546/


All Articles