Here's the problem code (there's more of it, but I figure this illustrates enough of the problem):
While testing this, the problem I encounter is that the vertical scrollbar's position is set to the bottom of the text within its viewport. By default I imagine the behavior should be at the top. Indeed this is confirmed by looking at the JScrollBar's getValue method (retrieved using getVerticalScrollBar from the scroll pane). The value is 0, but 0 shouldn't be at the bottom it should be at the top. I surmise that the issue has something to do with how I've constructed the objects, some sort of gotcha with regards to the behavior during construction and display. What that issue is I have no idea. Suggestions for a fix? Keep in mind I've already tried the various methods of scrolling like setValue, scrollRectToVisible, etc. and they did not work.
JMenuItem contentsItem = new JMenuItem("Contents"); contentsItem.setMnemonic(KeyEvent.VK_C); contentsItem.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_F1, 0)); contentsItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ev){ Container container = (Container)ev.getSource(); do{ if(container instanceof JPopupMenu){ container = (Container)((JPopupMenu)container).getInvoker(); }else if (container instanceof JMenuBar){ container = ((JMenuBar)container).getTopLevelAncestor(); }else { container = container.getParent(); } }while(!(container instanceof JFrame)); JTextPane helpContents = new JTextPane(); helpContents.setEditable(false); helpContents.setEditorKit(new HTMLEditorKit()); helpContents.setText("<html><body>" + "<h2>General Information</h2>" + "<p>" + APPLICATION_TITLE + " is an application coded in pure Java that is designed to enable the user to take screenshots of their desktop and save them to the local filesystem.</p>" + "<p>Upon pressing the <strong>Snap</strong> button, the application will hide itself from user view and take a screenshot. Dependent on the user options selected, the application will reappear once it has captured and written the screenshot to the local filesystem.</p>" + "<h2>Capture Area</h2>" + "<p>In the area titled <strong>Region</strong> there are two buttons that control the program's area capture behavior:</p>" + "<ul>" + "<li>Redefine</li>" + "<li>Fullscreen</li>" + "</ul>" + "<p><strong>Redefine</strong> brings up a dialog that allows the user to define a specific area of the screen to be captured. The area that the dialog window takes up on the user's screen is indicative of the region that the application will capture.</p>" + "<p><strong>Fullscreen</strong> ensures that the area to be captured is the entire screen width and height.</p>" + "<h2>Capture Timing</h2>" + "<p>In the area titled <strong>Timing</strong> there three radio buttons and two spinners that work in tandem with the <strong>Snap</strong> button to control the program's timed capture behavior:</p>" + "<ul>" + "<li>Immediate</li>" + "<li>Delay</li>" + "<li>Repeat</li>" + "</ul>" + "<ul>" + "<li>Frequency</li>" + "<li>Period</li>" + "</ul>" + "<p><strong>Immediate</strong> is selected when the user wishes to take a screenshot upon immediate pressing of the <strong>Snap</strong> button.</p>" + "<p><strong>Delay</strong> is selected when the user wishes to capture a screenshot after a preset amount of time has passed. The amount of time is controlled by the <strong>Frequency</strong> spinner.</p>" + "<p><strong>Repeat</strong> is selected when the user wishes to capture a series of screenshots for a given length of time. The total time that the application will capture screenshots is controlled by the <strong>Period</strong> spinner. The delay between each successive screenshot is controlled by the <strong>Frequency</strong> spinner.</p>" + "<h2>Saving Screenshots</h2>" + "<p>By default, screenshots are saved to the directory where the application resides. Users can modify this by using the <em><span style=\"text-decoration:underline;\">F</span>ile</em> menu's <em><span style=\"text-decoration:underline;\">S</span>ave To</em> option.</p>" + "<p>Screenshot filenames are automatically generated and saved as PNG images. The structure of the filename is a sequence of numbers (appended with the .png extension) which represent the following:</p>" + "<p>The smallest unit of time used for capturing screenshots is the millisecond. The user's machine, however, may not have the resources available to capture successive screenshots at such a pace. The application's performance will vary according to the user's machine setup.</p>" + "<ul>" + "<li>{1,4} Year screenshot was captured</li>" + "<li>{5,6} Month screenshot was captured</li>" + "<li>{7,8} Day screenshot was captured</li>" + "<li>{9,10} Hour screenshot was captured</li>" + "<li>{11,12} Minute screenshot was captured</li>" + "<li>{13,14} Second screenshot was captured</li>" + "<li>{15,18} Millisecond screenshot was captured</li>" + "</ul>" + "<h2>Warning</h2>" + "<p>Screenshot filename generation makes use of the local system time. There is no prompt for overwrite should two files share the same name. A user must take care when capturing screenshots between sessions, or in already populated directories, because the possibility of overwriting files does exist.</p>" + "</body></html>"); JScrollPane helpPane = new JScrollPane(helpContents); helpPane.setPreferredSize(new Dimension(400, 200)); JOptionPane.showMessageDialog((JFrame)container, helpPane, "Help", JOptionPane.PLAIN_MESSAGE, null); } });So I'm creating a "Contents" menu item within my "Help" menu. Give it a shortcut of C and an accelerator of F1. I add to it a listener for when it is pressed. Within the listener I determine the frame that is its parent. Now I create a JTextPane, make it non-editable, set it to be text/html and include a bunch of HTML markup text. Then I create a JScrollPane to encapsulate it, and give it a size. Finally, I create a dialog that outputs the info to the user.
While testing this, the problem I encounter is that the vertical scrollbar's position is set to the bottom of the text within its viewport. By default I imagine the behavior should be at the top. Indeed this is confirmed by looking at the JScrollBar's getValue method (retrieved using getVerticalScrollBar from the scroll pane). The value is 0, but 0 shouldn't be at the bottom it should be at the top. I surmise that the issue has something to do with how I've constructed the objects, some sort of gotcha with regards to the behavior during construction and display. What that issue is I have no idea. Suggestions for a fix? Keep in mind I've already tried the various methods of scrolling like setValue, scrollRectToVisible, etc. and they did not work.