How to make Jtextpane autoscroll and add a scrollbar

5.9k views Asked by At

I have added a jtextpane and am trying to make it autoscroll to the bottom when I added another line of text which goes past the last line of the rectangle. I also would like to add a scrollbar so people could scroll back up to see old text. I have looked on the oracle docs and in other stack overflow docs, but many people are referring to jtextarea. I know it involves the setcarrot position method, but im not really sure how to implement it. Any help? Here is a bit of what I have:

    public void textField(){

   field.setText("Hello and welcome to Stack overflow\n" );      
   Color color=new Color(255,255,255); 
   field.setBackground(color);  
   field.setBounds(100, 335, 350, 100);
   field.setEditable(false);
   add(field); 


}
0

There are 0 answers