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

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