You can try my code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextArea1b extends JApplet {
JTextArea jTA;
JPanel jp1,jp2;
JScrollPane jSPan;
JButton but1;
Point vp;
JViewport viewport;
public void init()
{
Container c = getContentPane();
jp1 = new JPanel();
jp2 = new JPanel();
String s = "Now is the\n" +
"time for all good men\n" +
"to come to the\n" +
"aid of their party.";
jTA = new JTextArea(s, 4, 20);
but1 = new JButton("scroll downwards");
c.setBounds(new Rectangle(0,0,400,400));
JScrollPane jSPan = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
viewport = jSPan.getViewport();
viewport.add(jTA);
c.setLayout(new FlowLayout());
c.add(jp1);
c.add(jp2);
jp1.setBackground(Color.BLACK);
jp2.setBackground(Color.RED);
jp2.setLayout(new BorderLayout());
jp2.add(jSPan,BorderLayout.CENTER);
jp2.add(but1,BorderLayout.SOUTH);
but1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
vp = viewport.getViewPosition();
vp.y+=10;
viewport.setViewPosition(vp);
}
});
}
}
The Jtextarea does not refresh well after I have clicked the button.