Question

I got a program with a JTextPane. My problem comes when I want to print in it. Every time, it will print at first line and move the rest down one. How can I make it so it prints at the end of the text?

I got this:

//...

public static enum Level {
    CLASSIC,
    MAJ,
    AJOUT,
    SUPRESSION,
    CONFIG;
}

public static void add(Level level,String Message){
    switch(level){
        case CLASSIC:
            try{
                Color BLACK = new Color(0, 0, 0);
                StyleConstants.setForeground(Colors, BLACK);
                Text.insertString(0, "\n\t- Mise à jour # " + Message + " -\n\n", Colors);
            }catch(Exception e) { ConsoleError(); }
            break;
        case MAJ:
            try{
                Color ORANGE = new Color(252, 156, 51);
                StyleConstants.setForeground(Colors, ORANGE);
                Text.insertString(0, Message + "\n", Colors);
            }catch(Exception e) { ConsoleError(); }
            break;
        case AJOUT:
            Color GREEN = new Color(58, 157, 52);
            StyleConstants.setForeground(Colors, GREEN);
            try{
                Text.insertString(0, Message + "\n", Colors);
            }catch(Exception e) { ConsoleError(); }
            break;
        case SUPRESSION:
            Color RED = new Color(183, 19, 0);
            StyleConstants.setForeground(Colors, RED);
            try{
                Text.insertString(0, Message + "\n", Colors);
            }catch(Exception e) { ConsoleError(); }
            break;
        case CONFIG:
            Color BLACK = new Color(0, 0, 0);
            StyleConstants.setForeground(Colors, BLACK);
            try{
                Text.insertString(0, Message + "\n", Colors);
            }catch(Exception e) { ConsoleError(); }
            break;
    }
}
//...
Was it helpful?

Solution

Instead of doing

Text.insertString(0, Message + "\n", Colors);

do this

Text.insertString(Text.getLength(), Message + "\n", Colors)

The 0 is the index position, where the text is inserted. With Text.getLength(), it will always be inserted at the end.

See this for more info: JTextPane appending a new string

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top