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!

JPanel within JScrollPane default position - scroll to top

ejwghinosJul 10 2012 — edited Jul 18 2012
Hi all,

I have a problem with a JScrollPane and a JPanel:
- If I fill the panel with 100 buttons, the scroll position is at the top
- If in the same panel I fill with 100 JTextAreas, the scroll position is at the bottom
package testing.scrollablePanel;

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import org.jdesktop.swingx.VerticalLayout;

public class TestScrollablePanel {
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				JPanel panel = new JPanel(new VerticalLayout());
				
				for (int i = 0; i < 100; i++) {
					// panel.add(new JButton("Hello there my friend"));
					panel.add(new JTextArea("Hello there my friend"));
				}
				
				JScrollPane scrollPane = new JScrollPane(panel);

				JFrame frame = new JFrame();
				frame.getContentPane().add(scrollPane);
				frame.setPreferredSize(new Dimension(300, 300));
				frame.pack();
				frame.setVisible(true);
			}
		});
	}
}
Now, how can I make the JTextArea behave as the JButton (i.e. leave the scroll to the top?

Thanks in advance
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 15 2012
Added on Jul 10 2012
4 comments
1,028 views