Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

public class Main {

    public static void main(String args[]) throws Exception {
        JFrame frame = new JFrame("MultiHighlight");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextArea comp = new JTextArea(5, 20);
        comp.setText("this is a test");
        frame.getContentPane().add(new JScrollPane(comp), BorderLayout.CENTER);

        String charsToHighlight = "a";
        Highlighter h = comp.getHighlighter();
        h.removeAllHighlights();
        String text = comp.getText().toUpperCase();

        for (int j = 0; j < text.length(); j += 1) {
            char ch = text.charAt(j);
            if (charsToHighlight.indexOf(ch) >= 0)
                h.addHighlight(j, j + 1, DefaultHighlighter.DefaultPainter);
        }
        frame.pack();
        frame.setVisible(true);
    }
}