Results 1 to 3 of 3
  1. #1
    tbuchwalter is offline Member
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Question JScrollPane and viewport issues

    Hi all,

    I am trying to create a UI that uses GridLayout. As a part of this UI I've created a JTextArea and added it to a JScrollPane. I then add text to the text area through out the program using .append() However, the text area keeps expanding down out of a viewable area. I want the text to scroll upward as new text is added but I can not figure out for the life of me how to make this happen. I've tried several different setting i.e. (setMaximumSize() or setPrefferedSize() but neither seem to work.

    Here is a sample of the code:
    //set text area
    jta_area = new JTextArea();
    jta_area.setBackground(null);
    jta_area.setFont((Font) Game.f);
    jta_area.setColumns(15);
    jta_area.setAutoscrolls(true);
    jta_area.setEditable(false);

    combatScroll = new JScrollPane(jta_area);
    //combatScroll.getViewport().setScrollMode(0);
    Dimension d = new Dimension(15,15);
    combatScroll.setPreferredSize(d);
    combatScroll.setMaximumSize(d);

    Any help you give can give me is much appreciated

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JScrollPane and viewport issues

    What if wherever you are appending to the JTextArea, you add a line after this that sets the caret position to the end of the document:

    Java Code:
    jta_area.append("whatever you're adding\n");
    
    // *** add code below ***
    jta_area.setCaretPosition(jta_area.getText().length()); // to place the caret at end of doc

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

Similar Threads

  1. JScrollPane issues - no autoscroll wished
    By Flo in forum AWT / Swing
    Replies: 3
    Last Post: 08-18-2011, 02:17 PM
  2. Need Help With JScrollPane Please
    By MemorableSmile in forum New To Java
    Replies: 4
    Last Post: 01-31-2011, 03:39 AM
  3. Zoom map in JScrollPane, keep center of viewport
    By knuth in forum New To Java
    Replies: 0
    Last Post: 10-01-2009, 08:45 PM
  4. jscrollpane scroll issues
    By kumar_gemi in forum AWT / Swing
    Replies: 11
    Last Post: 09-30-2009, 09:33 PM
  5. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •