0

I am scanning the files with different extension and putting them in String ArrayList in Java(the names with extension). I want to print with different colors into JTextArea by looking the extesions for example .xls extension as green color , .txt extension as blue color. Here is my code below ;

public void setScanResult(ArrayList<String> x)
{
    textArea.append("|");

    for (int i = 0; i < x.size(); i++) {                
        textArea.append("\n");
        textArea.append((String) x.get(i));                
    }    
    x.clear();
}
mussdroid
  • 732
  • 1
  • 9
  • 22

3 Answers3

1

For coloring text use JTextPane instead of JTextArea, here is simple example:

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
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 Example extends JFrame{

    private JTextPane pane;
    public Example(){
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    private void init() {
        JScrollPane p = new  JScrollPane(pane = new JTextPane());
        add(p);
        List<String> files = new ArrayList<>();
        files.add("1.txt");
        files.add("2.txt");
        files.add("3.doc");
        files.add("4.xls");
        for(String s : files){
            addText(s);
        }
    }

    private void addText(String s) {
        Color color = getColor(s);
        StyleContext style = StyleContext.getDefaultStyleContext();
        AttributeSet attrs = style.addAttribute(SimpleAttributeSet.EMPTY,StyleConstants.Foreground, color);
        pane.setCaretPosition(pane.getDocument().getLength());
        pane.setCharacterAttributes(attrs , false);
        pane.replaceSelection(s+"\n");
    }

    private Color getColor(String s) {
        return  s.endsWith(".txt") ? Color.RED : (s.endsWith(".doc") ? Color.GREEN : Color.CYAN );
    }

    public static void main(String... s){
        new Example();
    }
}

enter image description here

alex2410
  • 10,904
  • 3
  • 25
  • 41
1

Ok , Now I can recognize file extensions but still cant give color when I am printing to TextArea , is it because TextArea and all the font changes after running the program to *"serif", Font.ROMAN_BASELINE, 5*

 public void setScanResult(ArrayList<String> x) {

  for (int i = 0; i < x.size(); i++) {
        if (x.get(i).endsWith(".xls")) {
            Font fono1 = new Font("sansserif", Font.BOLD, 50);
            textArea.setFont(fono1);
            textArea.append((String) x.get(i));
        } 
        else if (x.get(i).endsWith(".exe")) {
            Font fono2 = new Font("Monospaced", Font.ITALIC, 10);
            textArea.setFont(fono2);
            textArea.append((String) x.get(i));
        } else {
            Font fono3 = new Font("serif", Font.ROMAN_BASELINE, 5);
            textArea.setFont(fono3);
            textArea.append((String) x.get(i));
        }
         textArea.append("\n");
     }
    x.clear();
}
mussdroid
  • 732
  • 1
  • 9
  • 22
0

Below is the code for recognizing the extension from the fileName. After knowing the extension, color can be applied to the text as done by @alex2410

public class Test {
    public static void main(String[] args) {
        ArrayList<String> t = new ArrayList<>();
        t.add("A.txt");
        t.add("b.pdf");
        t.add("c.exe");
        t.add("d.xls");
        t.add("e.xlsx");
        for (String fileName : t) {
            int code = getExtentionCode(fileName);
            fillColor(fileName, code);
        }

    }

    private static void fillColor(String fileName, int code) {

        // change color as per requirement
        switch (code) {
        case 0:

            break;
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            // unknown extention
            break;
        default:
            break;
        }
    }

    private static int getExtentionCode(String fileName) {
        int code = 5;
        if (fileName.endsWith(".txt")) {
            code = 0;
        } else if (fileName.endsWith(".pdf")) {
            code = 1;
        } else if (fileName.endsWith(".exe")) {
            code = 2;
        } else if (fileName.endsWith(".xls")) {
            code = 3;
        } else if (fileName.endsWith(".xlxs")) {
            code = 4;
        } else {
            code = 5;
        }
        return code;
    }
}
Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72