JScrollPane autoscroll from a Thread does not work
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Richard_Brochu
Posted On:   Thursday, June 19, 2003 04:54 AM

Text is appended to the displayArea using the text entered in the JTextField. If I append text to a JTextArea (in a JScrollPane) from a running Thread, the JScrollPane's autoscrolling does not work. The viewport remains at the top of the JScrollPane instead of scrolling to the newest entries. This works fine if not running with a Thread. What can be the problem and how can I get around this? Thank you in advance. To reproduce simply enter text in the textfield. The threaded application automatically retrieves the text from the text field. In the non-threaded application, the user must press . See code below (Non-Threaded application follows Threaded application). Threaded application: package documentev   More>>

Text is appended to the displayArea using the text entered in the JTextField. If I append text to a JTextArea (in a JScrollPane) from a running Thread, the JScrollPane's autoscrolling does not work. The viewport remains at the top of the JScrollPane instead of scrolling to the newest entries. This works fine if not running with a Thread. What can be the problem and how can I get around this? Thank you in advance.


To reproduce simply enter text in the textfield. The threaded application automatically retrieves the text from the text field. In the non-threaded application, the user must press . See code below (Non-Threaded application follows Threaded application).

Threaded application:

			
package documentevent;

/*
* Swing version
*/

import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;

public class DocumentEventThread extends JFrame {
JTextField textField = new JTextField(20);
JTextArea displayArea = new JTextArea(5, 20);

public DocumentEventThread() {
createFrame();
setVisible(true);

new Thread() {
public void run() {
while(!isInterrupted()) {
try {
Thread.sleep(3000);
}
catch(InterruptedException ie) {
break;
}

displayArea.append(textField.getText() + "
");
textField.setText("");
}
}
}.start();
}

public void createFrame() {
JButton button = new JButton("Clear");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
displayArea.setText("");
textField.requestFocus();
}
});

displayArea.setEditable(false);

getContentPane().add(textField, BorderLayout.NORTH);
getContentPane().add(new JScrollPane(displayArea), BorderLayout.CENTER);
getContentPane().add(button, BorderLayout.SOUTH);

pack();
}

public static void main(String[] args) {
DocumentEventThread def = new DocumentEventThread();
def.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
}



Non-Threaded application:
			

package documentevent;

/*
* Swing version
*/

import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;

public class DocumentEventFrame extends JFrame {
JTextField textField = new JTextField(20);
JTextArea displayArea = new JTextArea(5, 20);

public DocumentEventFrame() {
createFrame();
setVisible(true);
}

public void createFrame() {
JButton button = new JButton("Clear");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
displayArea.setText("");
textField.requestFocus();
}
});

textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
displayArea.append(textField.getText() + "
");
}
});

displayArea.setEditable(false);

getContentPane().add(textField, BorderLayout.NORTH);
getContentPane().add(new JScrollPane(displayArea), BorderLayout.CENTER);
getContentPane().add(button, BorderLayout.SOUTH);

pack();
}

public static void main(String[] args) {
DocumentEventFrame def = new DocumentEventFrame();
def.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
}
   <<Less

Re: JScrollPane autoscroll from a Thread does not work

Posted By:   Darshan_Bildikar  
Posted On:   Wednesday, July 30, 2003 07:21 AM

Hi,

I faced exactly the same problem except that I was updating the contents of the text area in a remote method (which runs in a separate thread I guess). Any clues as to why this exacyly happens? Is this a bug?
About | Sitemap | Contact