Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class Main {
    private static void appendToPane(JTextPane tp, String msg, Color f, Color b) {
        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, f);
        aset = sc.addAttribute(aset, StyleConstants.Background, b);

        aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
        aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

        int len = tp.getDocument().getLength();
        tp.setCaretPosition(len);
        tp.setCharacterAttributes(aset, false);
        tp.replaceSelection(msg);
    }

    public static void main(String... args) {
        JTextPane tPane = new JTextPane();
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        appendToPane(tPane, "this is a test.\n", Color.RED, Color.WHITE);
        appendToPane(tPane, "this is a test \n", Color.PINK, Color.BLUE);
        appendToPane(tPane, "test", Color.GRAY, Color.BLACK);
        appendToPane(tPane, "test", Color.RED, Color.BLUE);
        appendToPane(tPane, "test", Color.RED, Color.YELLOW);

        f.getContentPane().add(tPane);

        f.pack();
        f.setVisible(true);
    }
}