| [news.eclipse.newcomer] Re: Getting scroll pane to display bottom of text (using Swing) |
In article <dj86ss$pj6$1@xxxxxxxxxxxxxxxx>, yaofahua@xxxxxxxxxxx (JACK YAO) wrote:
public class test extends JFrame implements ActionListener { JTextArea ta = new JTextArea(10, 10); int i = 0; JPanel p = new JPanel(); JScrollPane sp = new JScrollPane(ta); public test() { p.add(sp); JButton btn = new JButton("test"); btn.addActionListener(this); p.add(btn, BorderLayout.NORTH); this.add(p); pack(); setVisible(true); } public void actionPerformed(ActionEvent e) { new AddThread().start(); } class AddThread extends Thread { public void run() { for (int i = 0; i < 100; i++) { ta.append("line :" + i + "\n"); //JScrollBar sb = sp.getVerticalScrollBar(); //sb.setValue(sb.getMaximum()); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) { new test(); } }Jack,
I've tried your program now, but modified it to append more than one line. When you comment out the lines the slider bar stays at the top. When you don't, last appended line isn't visible.
I'm obviously going to have to experiment more with my program, and learn about threading.
On a wider point, this seems to be an issue which makes Swing difficult to use - why isn't the obvious behaviour of the slider bar on append made a default?
Peter
Peter, did you solve your problem ? I change the for loop to :
//**************************************************************
for(int i=0;i<100;i++) {
System.out.println(sb.getMaximum());//1 ta.append("line :" + i + "\n");
sleep(500);
System.out.println(sb.getMaximum());//2 ta.append("line :" + i + "\n");
sleep(500);
System.out.println(sb.getMaximum());//3JACK YAO, Beijing, China