2 Replies - 14132 Views - Last Post: 12 August 2008 - 07:15 PM Rate Topic: -----

#1 lwstory  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 29-October 07

Scroll to end of a JTextArea.

Posted 12 August 2008 - 04:27 PM

I would like to make the scroll pane scroll to the end of the text field every time text is added to the text area. I would like to add it at the end of the append method where I left the comment. Any help?

package graphics;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Locale;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ContentPane extends JPanel{

	protected JTextArea textArea;
	private final static String newline = "\n";
	JScrollPane scrollPane;

	public ContentPane(){
		super(new GridBagLayout());

		textArea = new JTextArea(5, 20);
		textArea.setEditable(false);
		scrollPane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setWheelScrollingEnabled(true);
		//Add Components to this panel.
		GridBagConstraints c = new GridBagConstraints();
		c.gridwidth = GridBagConstraints.REMAINDER;

		c.fill = GridBagConstraints.HORIZONTAL;

		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.weighty = 1.0;
		add(scrollPane, c);
		for(int j = 0; j<100; j++){
			append(j + "   12345678901234567890123456789012345678901234567890123456789012345678901234567890" + "\n");
		}
	}
	
	public void append(String st){
		textArea.append(st);
	   //Move to the end of the text field
	
	}

	public static void main(String[] s){
		JFrame jf = new JFrame("lame");
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setVisible(true);
		jf.add(new ContentPane());
		jf.pack();
	}
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Scroll to end of a JTextArea.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Scroll to end of a JTextArea.

Posted 12 August 2008 - 06:23 PM

Google first

http://java.sun.com/...s/textarea.html

Unless the user has moved the caret (insertion point) by clicking or dragging in the text area, the text area automatically scrolls so that the appended text is visible. You can force the text area to scroll to the bottom by moving the caret to the end of the text area after the call to append:

textArea.setCaretPosition(textArea.getDocument().getLength());


Was This Post Helpful? 0
  • +
  • -

#3 lwstory  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 29-October 07

Re: Scroll to end of a JTextArea.

Posted 12 August 2008 - 07:15 PM

View Postpbl, on 12 Aug, 2008 - 06:23 PM, said:

Google first

http://java.sun.com/...s/textarea.html

Unless the user has moved the caret (insertion point) by clicking or dragging in the text area, the text area automatically scrolls so that the appended text is visible. You can force the text area to scroll to the bottom by moving the caret to the end of the text area after the call to append:

textArea.setCaretPosition(textArea.getDocument().getLength());




I actually went to that site before but i couldnt figure out the right way to move the carrot to the end. I figured that I wasnt using the right thing. I didnt know to use the getDocument(). Thank you very much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1