JTextPane Zeilen überlappen sich beim scrollen

Palme4001

Grünschnabel
Hallo,
ich versuche grade eine Art Konsole in mein Programm für simple outputs einzubauen. Dafür gibt es die Klasse "Console" :

Java:
public class Console extends JTextPane{
   
    private static final long serialVersionUID = 8336134746819638718L;

    private final StyledDocument doc = this.getStyledDocument();
   
    private int width, height;
   
    private Style normal;
    private Style bold;
    private Style blue;
    private Style red;
   
    public Console(int width, int height){
       
        this.width = width;
        this.height = height;
       
        this.setEditable(false);
        this.setFont( new Font( "Consolas", Font.BOLD, 20 ) );
       
        normal = doc.addStyle("regular", null);
        StyleConstants.setForeground(normal, Color.BLACK);
       
        bold = doc.addStyle("bold", bold);
        StyleConstants.setBold(bold, true);
       
        blue = doc.addStyle("blue", null);
        StyleConstants.setForeground(blue, Color.BLUE);
       
        red = doc.addStyle("red", bold);
        StyleConstants.setForeground(red, Color.RED);     
       
    }
   
    public void logNormal(String t){
        try {
            doc.insertString(doc.getLength(), t+"\n", normal);
        } catch (BadLocationException e) {
        }
    }

Nun ein Ausschnitt aus der Klasse "Frame":

Java:
private void initConsole(){
       
        cs_console = new Console(getWidth(), getHeight()/2);
        cs_scrollPane = new JScrollPane(cs_console);
        cs_scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        cs_scrollPane.setPreferredSize(new Dimension(cs_console.getWidth() -20, cs_console.getHeight()));
        cs_scrollPane.setBorder(new TitledBorder(new LineBorder(Color.black, 2), "Console"));
        cs_scrollPane.setOpaque(true);
       
        cs_consolePanel = new JPanel();
        cs_consolePanel.add(cs_scrollPane);
        cs_consolePanel.setBounds(-10, getHeight() - cs_console.getHeight() -40, cs_console.getWidth() +10, cs_console.getHeight() +10);


    }

Java:
public void loadConsole(){
       
        getContentPane().add(cs_consolePanel);
       
        for(int a = 0; a < 50; a++){
            this.cs_console.logNormal("Normaaaaaaal");
            this.cs_console.logWarning("Waaaaarning");
            this.cs_console.logError("Errrooooor");
        }
    }

Somit werden eine Menge Zeilen zum Test in das JTextPane geschrieben.
Wenn ich das nun ausführe ist die Sroll Leiste ganz unten, und wenn man anfängt nach oben zu srollen überlappen sich die Zeilen, sodass man keine mehr lesen kann. Die einzigen Zeilen die ohne Probleme lesbar sind die obersten, für welche man nicht scollen müsste.

Wenn nun zB noch ein Button überhalt des JTextPane ist, wird dieser ebenfalls etliche male beim Srollen IM JTextPane angezeigt und gedoppelt.

Wäre echt gut, wenn jemand mir da weiterhelfen könnte, ich bin hier grade am verzweifeln :/

Vielen Dank im Vorraus :)

Palme4001
 

Neue Beiträge

Zurück