Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Controlling Where a JScrollPane Opens  RSS feed

Bier Drolf
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem: User presses help button and a new dialog window is created with a scoll pane and a text area. When the user views the window they are looking at the bottom of the text area and have to scroll back to the top to read the help text.
How can I control where the JScrollPane will Open when created. Most importantly so it will open at the top of the text area.
Code Follows.
Called with
//Help | Boundary_Help action performed
//Help | Quick Tips action performed
public void jMenuBoundaryHelp_actionPerformed(ActionEvent e) {
BoundaryHelp hep = new BoundaryHelp(this);
Dimension hepSize = hep.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
hep.setLocation((frmSize.width - hepSize.width) / 2 + loc.x,
(frmSize.height - hepSize.height) / 2 + loc.y);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
hep.setSize(screenSize.width * 3/8, screenSize.height * 3/8);;

Called Class
public class BoundaryHelp extends JDialog implements ActionListener{
JPanel panel1 = new JPanel();
JPanel titlePanel = new JPanel();
FlowLayout flowLayout1 = new FlowLayout();
BorderLayout borderLayout1 = new BorderLayout();
JLabel titleLabel = new JLabel();
JScrollPane helpScroll = new JScrollPane();
JTextArea helpTextArea = new JTextArea();
JPanel buttonPanel = new JPanel();
FlowLayout flowLayout2 = new FlowLayout();
JButton quitButton = new JButton();
// Build the Window
public BoundaryHelp(Frame parent) {
try {
catch(Exception e) {

public BoundaryHelp() {
private void jbInit() throws Exception {
this.setTitle("Boundary Help");
panel1.setMaximumSize(new Dimension(2147483647, 2147483647));
titleLabel.setText("Orange Boundary Instructions");
helpTextArea.setFont(new java.awt.Font("Lucida Grande", 1, 16));
buttonPanel.setMinimumSize(new Dimension(10, 10));
panel1.add(titlePanel, BorderLayout.NORTH);
titlePanel.add(titleLabel, null);
panel1.add(helpScroll, BorderLayout.CENTER);
helpScroll.getViewport().add(helpTextArea, null);
panel1.add(buttonPanel, BorderLayout.SOUTH);
buttonPanel.add(quitButton, null);
// Help Message
helpTextArea.setFont(new java.awt.Font("Lucida Grande", 0, 13));
" The Orange Boundary Tool creates orange Old Doc Boundaries " +
"using a pre-existing Blue Old Doc Boundary. After the user " +
"enters the boundary name, the program uses the coordinates " +
"from this Blue Old Doc Boundary to create this new Orange Old " +
"Doc. The program will fail if the Blue Old Doc doesn't exist " +
"or if an Orange Boundary already exists for this boundary name. " +
"\n\n\nRunning The Program \n\n" +
"Enter boundary name in the name field and then select the " +
"\"Create\" button to run the program. Any problems will be " +
"identified in the error message section or by a pop-up. If " +
"the program ran smoothly, no messages will appear. " +
"\n\n\nError Messages\n\n" +
"No Boundary Defined pop-up - This message will appear if the " +
"boundary field is not filled out prior to pressing the " +
"\"Create\" button.\n\n" +
"Error Message: Orange Boundary Exists in Database - If an " +
"Orange Boundary was created previously with the same name " +
"the program will fail. If this happens ask the SA to delete " +
"this boundary and then rerun the program. \n\n" +
"Error Message: The Boundary Does Not Exist - This message will " +
"appear if the corresponding Blue Old Doc does not exist in the " +
"database. This failure is required because the program uses the " +
"coordinates from the Blue Boundary to draw the Orange Boundary. " +
"If this happens check the name you entered into the Boundary Name " +
"Field. Next check IMS for the existence of the Blue Boundary. " +
"Redraw the Blue Old Doc if necessary.\n\n\n ");
// Set the scroll window to start at the top.
Ernest Friedman-Hill
author and iconoclast
Posts: 24215
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!
I believe the trick is to move the caret in the text area to position 0; the scroll pane will automatically scroll to show the cursor -- i.e.,
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the JTextArea api scroll down to the section Methods inherited from class javax.swing.text.JTextComponent to find the setCaretPosition method. You can try helpTextArea.setCaretPosition(0) after the text area is loaded.
Bier Drolf
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Thanks,
That did the trick.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!