javaswingjtextareajtextpane

How to create and use a JTextPane


I would like to create a program in Java that it is done this way: What I want

A window containing a button and a component (JTextArea, JTextPane, etc.) that can't be modified in which appear some strings based on the execution of some work. As seen from the drawing, if the job is successful the text will be black, if there were errors will be marked red.

I managed to do everything correctly using a JTextArea and a JButton but I found that you can't change the color of the strings line by line.

I read that should use a JTextPane but I haven't been able to use it. Here's the code I wrote:

public class Example {

   public Example() {
        JFrame frame = new JFrame();
        JTextPane pane = new JTextPane();
        JButton button = new JButton("Button");
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.setPreferredSize(new Dimension(200, 200));
        frame.add(pane);
        frame.add(button);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Example();
    }
}

When I run the program, this is what is created:

enter image description here

Where the TextPane?

Also, before I added the text in JTextArea using append(), I haven't found a similar method to the JTextPane there? How do you use it? How do I change the color of a single line?

I have read and seen and tried various examples found on the internet but I could not finish anything... There are worked examples similar to this?

I apologize for the "generic" request...

Thanks


Solution

  • import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextPane;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.Style;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyledDocument;
    
    public class Example {
    
        public Example() {
    
            JFrame frame = new JFrame();
            JTextPane pane = new JTextPane();;
            JButton button = new JButton("Button");
    
            addColoredText(pane, "Red Text\n", Color.RED);
            addColoredText(pane, "Blue Text\n", Color.BLUE);
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pane.setPreferredSize(new Dimension(200, 200));
            frame.getContentPane().add(pane, BorderLayout.CENTER);
            frame.getContentPane().add(button, BorderLayout.WEST);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            new Example();
        }
    
        public void addColoredText(JTextPane pane, String text, Color color) {
            StyledDocument doc = pane.getStyledDocument();
    
            Style style = pane.addStyle("Color Style", null);
            StyleConstants.setForeground(style, color);
            try {
                doc.insertString(doc.getLength(), text, style);
            } 
            catch (BadLocationException e) {
                e.printStackTrace();
            }           
        }
    }
    

    Try this example