[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:

> *From:* JACK YAO <yaofahua@xxxxxxxxxxx>
> *Date:* Thu, 20 Oct 2005 21:43:11 +0800
> 
> Peter Toye:
> This is another test program. I started a new thread to append 100 
> strings. When I commented the two lines , it always displayed the first 
>      several lines, and when not, it displayed the last several lines. 
> I think that maybe the two concurrent threads make the problem, each 
> one only does half of the "right display" task. But because they are 
> done separately in different threads, they can't work one by one.
> Did you start a new thread to append or still have some other causes?
> ********************************************************
> 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 YAO, Beijing, China
> 
Jack,

The thread idea's an interesting one - I set up the GUI in a separate 
thread (as told to in the Sun "Learning Swing by Example" demo), and the 
update's done in an event listener, but long after the GUI's been 
displayed. So I don't see how that would make any difference.

But I notice that you're moving the slider on the JScrollBar by doing an 
sb.setValue on it rather than a scrollRectToVisible on the JScrollPane. 
This works on mine but only if I do a validate before getting the sb 
maximum- otherwise it's always a bit behind.

I'll play a bit more tomorrow - this isn't a no. 1 priority (yet), but 
it's puzzling and I'd like to get it right! So thanks a lot for all your 
help.

Peter