Search code examples
javaarraylistjtextarea

How to set different colors to ArrayList content in Java


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();
}

Solution

  • 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