Search code examples
javahtmlswingloggingjeditorpane

HTML-JScrollPane as Log (scrolling text with timestamp)


I hope you can help me. I try to make a "Log", to inform the users about the programs progress. Without HTML it was no problem:

package view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class LogComponent extends JPanel {
    protected JTextArea textArea;
    private final static String newline = "\n";

    public LogComponent() {
        super(new GridBagLayout());

        textArea = new JTextArea(5, 20);
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);

        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        add(scrollPane, c);
    }
    public void setEntry(String entry) {
        SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
        Date d = new Date();

        textArea.append("[" + s.format(d) + "] " + entry + newline);
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }
}

But I want to use colors in parts of the log entrys. So I changed textArea for JEditPane, to be able to use HTML. This is kinda working, Iam able to set entrys, but I dont get, how to keep the old stuff... If I try to keep the old entry(s), there doesnt appear a new entry:

package view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;

public class HtmlLogComponent extends JPanel {
    protected JEditorPane editor;

    public HtmlLogComponent() {
        super(new GridBagLayout());

        editor = new JEditorPane();
        editor.setEditable(false);

        JScrollPane scrollPane = new JScrollPane(editor);

        HTMLEditorKit kit = new HTMLEditorKit();
        editor.setEditorKit(kit);

        Document doc = kit.createDefaultDocument();
        editor.setDocument(doc);

        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        add(scrollPane, c);

    }

    public void setEntry(String entry) {
        SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
        Date d = new Date();


        editor.setText("[" + s.format(d) + "] " + entry);
        System.out.println(editor.getText());
    }

}

Do you have any idea :)?

Kind regards Aca


Solution

  • Its not a very "clean" solution, but it works very well ;D... Here is my class:

    package view;
    
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import javax.swing.DefaultListModel;
    import javax.swing.DefaultListSelectionModel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.ScrollPaneConstants;
    
    
    public class HtmlLogComponent extends JPanel {
        protected DefaultListModel model = new DefaultListModel();
        protected JList list;
        public static int maxLen = 110;
    
        public HtmlLogComponent(int maxLen) {
            super(new GridBagLayout());
            this.maxLen = maxLen;
    
            list = new JList(model);
    
            JScrollPane scrollPane = new JScrollPane(list);
            scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            list.setSelectionModel(new DisabledItemSelectionModel());
    
            GridBagConstraints c = new GridBagConstraints();
            c.gridwidth = GridBagConstraints.REMAINDER;
    
            c.fill = GridBagConstraints.BOTH;
            c.weightx = 1.0;
            c.weighty = 1.0;
            add(scrollPane, c);
    
        }
    
        public void setEntry(String entry) {
            SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
            Date d = new Date();
            Integer lenght = entry.length();
            String string = "<html>[" + s.format(d) + "] ";
            String substring = entry;
    
            if (lenght >= maxLen) { // Linebreak needed
                while (lenght >= maxLen) {              
                    for (int i=maxLen - 1; i> 0; i--) {
                        if (substring.charAt(i) == ' ') {
                            string += substring.substring(0, i) + "</html>";
                            model.addElement(string);
                            substring = substring.substring(i, lenght);
                            System.out.println(substring);
                            break;
                        }
                    }
                lenght = substring.length();
                string = "<html>";          
                }
                model.addElement("<html>" + substring  + "</html>");
            } else {
                model.addElement("<html>[" + s.format(d) + "] " + entry + "</html>");
            }
    
            int lastIndex = list.getModel().getSize() - 1;
            if (lastIndex >= 0) list.ensureIndexIsVisible(lastIndex);
        }
    
        class DisabledItemSelectionModel extends DefaultListSelectionModel {
    
            @Override
            public void setSelectionInterval(int index0, int index1) {
                super.setSelectionInterval(-1, -1);
            }
        }
    
    }
    

    Thanks to MadProgrammer for helping me here :)!