aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Controlling Where a JScrollPane Opens Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Controlling Where a JScrollPane Opens" Watch "Controlling Where a JScrollPane Opens" New topic
Author

Controlling Where a JScrollPane Opens

Bier Drolf
Greenhorn

Joined: May 04, 2004
Posts: 2
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.
Thanks,
Bier
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);
hep.setModal(true);
//hep.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
hep.setSize(screenSize.width * 3/8, screenSize.height * 3/8);
hep.show();
}

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) {
super(parent);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

public BoundaryHelp() {
this(null);
}
private void jbInit() throws Exception {
this.setTitle("Boundary Help");
panel1.setLayout(borderLayout1);
titlePanel.setLayout(flowLayout1);
panel1.setMaximumSize(new Dimension(2147483647, 2147483647));
titleLabel.setRequestFocusEnabled(true);
titleLabel.setText("Orange Boundary Instructions");
helpTextArea.setEnabled(true);
helpTextArea.setFont(new java.awt.Font("Lucida Grande", 1, 16));
helpTextArea.setEditable(false);
helpTextArea.setSelectionEnd(0);
helpTextArea.setText("Overview\n\n");
helpTextArea.setLineWrap(true);
helpTextArea.setTabSize(8);
helpTextArea.setWrapStyleWord(true);
buttonPanel.setLayout(flowLayout2);
buttonPanel.setMinimumSize(new Dimension(10, 10));
quitButton.setActionCommand("quitButton");
quitButton.setText("jButton1");
helpScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
helpScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
getContentPane().add(panel1);
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));
helpTextArea.append(
" 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.
//helpScroll.setLocation(0,0);
//helpScroll.setAutoscrolls(false);
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,
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.,
theTextArea.setCaretPosition(0);


[Jess in Action][AskingGoodQuestions]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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
Greenhorn

Joined: May 04, 2004
Posts: 2
Hey Thanks,
That did the trick.
Bier
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Controlling Where a JScrollPane Opens