Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTextArea prevent scrolling

843805Oct 17 2005 — edited Oct 25 2005
Hi,
I have a problem with the automatic scrolling capabilities of a JTextArea.
I have different Panels that contain JTextAreas on one main Panel (with BoxLayout(Y_Axis). This main Panel is put on a JScrollPane, so that I can scroll to the different TextAreas. This works fine. Even if i add more lines to one JTextArea the resizing of the components works.
But there is a problem if i change the Content of a JTextArea from my code. The ScrollPane immediatly changes its position to bring the changed JTextArea to view.
What i am looking for right now is a way to prevent the ScrollPane from scrolling each time i uptade the Text of one of the JTextFields.
The following sample illustrates the problem.
(Just add enough textPanels for the ScrollBar to appear, then click the "Change all texts" button. I am looking for a way to change the texts without the ScrollPane to scroll)

Sample:
package test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;

public class ScrollTest extends JFrame {
	
	public JButton		buttonAdd;
	public JButton		buttonChange;
	public JPanel		mainPanel;
	public JScrollPane	scrollPane;

	public ScrollTest() {
		super();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(500, 300);

		buttonAdd = new JButton("add TextPanel");
		buttonAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainPanel.add(new textPanel());
				mainPanel.revalidate();
			}
		});

		buttonChange = new JButton("change all Texts");
		buttonChange.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				for (int i = 0; i < mainPanel.getComponentCount(); i++)
					((textPanel) (mainPanel.getComponent(i)))
							.setText("changed !!");
			}
		});

		mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
		scrollPane = new JScrollPane(mainPanel);

		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(buttonAdd, BorderLayout.NORTH);
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		getContentPane().add(buttonChange, BorderLayout.SOUTH);

	}

	public static void main(String[] args) {
		JFrame mainFrame = new ScrollTest();
		mainFrame.setVisible(true);
	}

}

class textPanel extends JPanel {
	JTextArea	myText	= null;

	public textPanel() {
		super();
		this.setLayout(new BorderLayout());
		myText = new JTextArea();
		myText.setText("some Text ...");
		myText.setBorder(new LineBorder(new Color(0, 0, 0)));
		myText.setAutoscrolls(false);
		this.add(myText, BorderLayout.CENTER);
	}

	public void setText(String s) {
		myText.setText(s);
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 22 2005
Added on Oct 17 2005
4 comments
97 views