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!

JEditorPane always scrolls down to the bottom, how can I prevent this?

801912Apr 16 2008 — edited Apr 17 2008
Hello, I have the following code that contains a JTable and JEditorPane in a panel. When I click on a headline on the table, I have some text showing up in the lower pane. My problem is that the lower pane (that is inside a JScrollPane) always scrolls down to the end of the text, but I want to to always point to the beginning.
Does anyone know how to do this?
Your help will be appreciated.
Thanks!
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

public class MyHTMLPaneTest {

	JFrame frame;
	JPanel panel;
	JSplitPane splitPane;
	JTable table;
	JEditorPane htmlPane;
	JPanel tablePanel;
	DefaultTableModel model;
	JViewport viewPort;
	
	public MyHTMLPaneTest()
	{
		frame = new JFrame("MyCalHTMLPane Test");
		panel = new JPanel(new BorderLayout());
		splitPane = new JSplitPane();
		tablePanel = new JPanel(new BorderLayout());
		setTable();
		tablePanel.add(table.getTableHeader(), BorderLayout.NORTH);
		tablePanel.add(table, BorderLayout.CENTER);
		htmlPane = new HTMLPane();
		JScrollPane pane = new JScrollPane(htmlPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		viewPort = pane.getViewport();
		viewPort.setViewPosition(new Point(0, 0));
		splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tablePanel, pane);
        panel.add(splitPane, BorderLayout.CENTER); 
		frame.getContentPane().add(panel);
		frame.setSize(new Dimension(400, 300));
		frame.setVisible(true);
	}
	
	public void setTable()
	{
		String[] columnNames = {"Date","Time","Subject"};
	    Object[][] data = 
	    {
		    {"03/31/05", "8:20 AM EDT", "Headline 1"},
		    {"08/24/05", "8:19 AM EDT", "Headline 2"},
		    {"08/18/05", "8:18 AM EDT", "Headline 3"},
	        {"08/12/05", "8:17 AM EDT", "Headline 4"},
	        {"06/20/05", "8:16 AM EDT", "Headline 5"},
	        {"06/20/05", "8:15 AM EDT", "Headline 6"}
	    };
	    model = new DefaultTableModel(data, columnNames);
	    table = new JTable(model);
	    table.getSelectionModel().addListSelectionListener(new ListSelectionListener()
	    {
	        public void valueChanged(ListSelectionEvent e)
	        {
	        	String text = "LOOOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOOOOOOOOOONG" +
	        			"\nLOOOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOONG"+
	        			"\nLOOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG"+
	        			"\nSTRIIIIIIIIIIIIIIIII" +
	        			"\nIIIIIIIIIIIIIIIIIIII" +
	        			"\nIIIIIIIIIIIIIIIIIIIII" +
	        			"\nIIIIIIIIIIIIIIIIIIIII" +
	        			"\nIIIIIIIIIIIIIIIIIIIIING"+
	        			"LOOOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOOOOOOOOOONG" +
	        			"\nLOOOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOONG"+
	        			"\nLOOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOOOO" +
	        			"\nOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG"+
	        			"\nSTRIIIIIIIIIIIIIIIII" +
	        			"\nIIIIIIIIIIIIIIIIIIII" +
	        			"\nIIIIIIIIIIIIIIIIIIIII" +
	        			"\nIIIIIIIIIIIIIIIIIIIII" +
	        			"\nIIIIIIIIIIIIIIIIIIIIING";
	        	htmlPane.setText(text);
	        }
	    });
	    
	}
	
	public static void main(String[] args) {
		MyHTMLPaneTest test = new MyHTMLPaneTest();

	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 15 2008
Added on Apr 16 2008
2 comments
306 views