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!

Change AutoScroll Bounds in JTextPane

843805Jan 24 2007 — edited Jan 25 2007
I cannot figure this out for the life of me. I would like to CHANGE the bounds for the autoscroll on a JTextPane that resides inside a JScrollPane when the user selects text and moves the mouse outside of the "autoscroll bounds".

The normal bounds for scrolling down a JTextPane (when inside a JScrollPane) are usually the bounds of the JTextPane itself. (You can verify this with my example by selecting some text with the mouse button held down, and moving your cursor outside the bottom of the window). I would like to move the bottom bounds up higher into the window (indicated by the black line that resides on the glasspane).

I have provided this example to use as a starting point. It doesn't do anything of use right now. I have tried about 20 things, but things have gotten so messed up, and the level of internal code that I started to mess around with got way over my head... so I'm just going to post this with a clean start, and hopefully we can move in a positive direction. I will relay my findings about a paticular route if we start moving down one i've already failed at.

Thanks for you time.

-Js
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;

public class AutoScrollTest extends JFrame
{
	//DATA
	private static final int Y_SCROLL_THRESHOLD = 150;
	
	//GUI
	private JPanel glassPanel;
	
	private JScrollPane scrollPane;
		private JTextPane textPane;
	
	public AutoScrollTest()
	{
		this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
		this.setGlassPane(getGlassPanel());
		this.setContentPane(getScrollPane());
		getGlassPanel().setVisible(true);
		this.pack();
		this.setVisible(true);
	}

	private JPanel getGlassPanel()
	{
		if(glassPanel == null)
		{
			glassPanel = new JPanel()
			{
				public void paintComponent(Graphics g)
				{			
					super.paintComponent(g);
					g.setColor(Color.BLACK);
					g.drawLine(0, Y_SCROLL_THRESHOLD, 300, Y_SCROLL_THRESHOLD);
				}
			};
			glassPanel.setOpaque(false);
		}
		return glassPanel;
	}
	
	private JScrollPane getScrollPane()
	{
		if(scrollPane == null)
		{
			scrollPane = new JScrollPane(getTextPane());
			scrollPane.setPreferredSize(new Dimension(300,300));
		}
		return scrollPane;
	}
	
	private JTextPane getTextPane()
	{
		if(textPane == null)
		{
			textPane = new JTextPane();
			try
			{
				StyledDocument sd = textPane.getStyledDocument();
				for(int i=1; i<1000; i++)
				{
					sd.insertString(sd.getLength(),"This is line: " + i + "\n", null);
				}
			}
			catch(BadLocationException ble)
			{
				ble.printStackTrace();
			}
		}
		return textPane;
	}
	
	public static void main(String args[])
	{
		new AutoScrollTest();
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 22 2007
Added on Jan 24 2007
11 comments
252 views