[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[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