Scrolling JTextArea to Last Line

Hi Gurus:
I have seached the forum, but didn't find a solution to this problem. It must be something really simple that I'm not getting!
I have a JTextArea with JScrollbar attached to it. My application appends output text to this text area for user information. I would like to scroll the text up, so that the last line of text is always visible in that area.
Currently, this is what I'm doing:
txtOutput.setCaretPosition(txtOutput.getText().length());where txtOutput is the JTextArea in question.
This does scroll the text up (as desired), however, it also scrolls the text leftwards to show the latest text (not desired), since that's what I asked it to do. How can I get it to scroll back to the top of the line? Or should I use some other approach?
Regards,
Kamran

If I understand your problem you should be able to use the following methods from the JTextArea API:
int getLineCount() - Determines the number of lines contained in the area.
int getLineStartOffset(int line) - Determines the offset of the start of the given line.
You should be able to do something like:
caretPosition = textArea.getLineStartOffset( getLineCount() );
This should position the caret to the beginning of the last line.
BTW - JTextArea is a Swing Component. There is a whole forum devoted to Swing questions. You should post your question there - you might get an answer faster. This will also improve your searches as the search function only search the current forum you are looking at.

Similar Messages

  • Scrolling without last line

    hello
    I can't manage scrolling
    this is quite weird. I have a button with this action:
    controller.verticalScrollPosition+=controller.getScrollDelta(1);
    it works fine until the last line of text (which is always invisible). Then it stops and cannot event move back by:
    controller.verticalScrollPosition-=controller.getScrollDelta(1);
    just won't work. textFlow is as simple as possible
    textFlow = TextFilter.importToFlow(markup, TextFilter.TEXT_LAYOUT_FORMAT);
    but if I import a plain text
    textFlow = TextFilter.importToFlow(markup2, TextFilter.PLAIN_TEXT_FORMAT);
    scrolling doesn't work at all.

    Take a look at ContainerController.getContentBounds.  If that is wrong then scrolling isn't going to work.
    There's been quite a bit of work on scrolling lately and a few bugs were introduced.  Expect much better results with this weeks build.
    Sorry about that,
    Richard

  • SQL window keep scrolling down to the last line

    Is there a setting that will keep the SQL window from always scrolling down to the last line in the window? This feature is very annoying when trying to review code that is longer that the window size.

    You'd have to give more details as to what's your environment and what exactly is failing, but FWIW my tables' SQL tabs behave the same as all others; they start up scrolled up, and can be scrolled as you whish...
    K.

  • How to set a JScrollPane always display the last line?

    I have put a JTextArea in a JScrollPane.
    Whenever I append a new line of sentence to the JTextArea, the JScrollPane only shorten the vertical scroll bar.
    Is there any command that can scroll down to the last line whenever I append a new sentence to the JTextArea?

    I have tried to add the line you shown, but it can just show the second last line. It cannot scroll down to the last line.
    e.g. I have these message:
    AAA
    BBB
    CCC
    If the message board can only show 3 lines and I enter one more line "DDD", then the last line will be shown is CCC. I need to scroll down a bit more myself to see DDD. If I scroll to the top and enter another line"EEE", it will scroll down to show "DDD" but not "EEE".

  • How to set scrolledPane/textArea to display last lines?

    I have a JTextArea in a JScrollPane.
    I'm adding to the text area and the information scrolls off the visible portion of the screen.
    How can I force the scrolling to always display the last lines at the bottom of the screen when they are added?
    Thanks.

    See [url http://www.camick.com/java/blog.html?name=text-area-scrolling]Text Area Scrolling

  • Auto scrolling JTextAreas

    Hi,
    I am having an infuriating problem with my JTextArea which hopefully someone can help me with.
    On my GUI I have a JTextArea in a JScrollPane which is uneditable as it
    is there purely to give the user feedback. The user presses a button
    and the program happily does its thing creating datafiles and other
    such things and all the while telling the user what it is up to.
    The code I use to update the display is this:
    public void updateText(String str)
        textarea.append(string + "\n");
        textarea.paintImmediately();
        textarea.scrollRectToVisible(textarea.modelToView(
                          textarea.getDocument().getLength());
    }This works fine until the text reaches the bottom of the JTextArea
    because what ever the last line of text is, it gets repeated evertime
    it scrolls until the program stops updating the display at which point
    the text changes to what it should be. So for example, if the last text
    was "This is rubbish" before it starts scrolling then the
    display would look like this...
    This is rubbish
    This is rubbish
    This is rubbish
    This is rubbish....(and so on until the end where it changes to the proper text).
    One thing I have noticed is that the last line in the JTextArea before
    scrolling starts has about the bottom two rows of pixels missing from
    the text, could this be causing the scrolling to mess up. I have tried
    making the JTextArea double buffered and swapping the order of the
    scrolling and the paintImmediatly.
    I am really stuck and have searched through the forums trying all the
    usual suggestions for scrolling such as setting the caret position but
    this seems to be the only one that make the JTextArea scroll, albeit
    with a messed up display.
    Please can someone help me, this is really bugging me. (No pun intended).
    Cheers Jim.

    Check this code
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    class Test extends JFrame implements ActionListener
         static int nCount = 0;
         JTextArea m_areaTest = null;
         JButton m_btnTest = new JButton("Add");
         public Test()
              m_areaTest = new JTextArea(10,30);
              JScrollPane scrollTest = new JScrollPane(m_areaTest);
              m_areaTest.setLineWrap(true);
              getContentPane().setLayout(new FlowLayout());
              getContentPane().add(scrollTest);
              getContentPane().add(m_btnTest);
              m_btnTest.addActionListener(this);
              addWindowListener(new WindowAdapter()
                   public void windowClosing(WindowEvent e)
                        dispose();
                        System.exit(0);
         public static void main(String args[])
              System.out.println("Starting CopyPaste...");
              Test mainFrame = new Test();
              mainFrame.setSize(400, 400);
              mainFrame.setTitle("CopyPaste");
              mainFrame.setVisible(true);
         public void actionPerformed(ActionEvent e)
              if(e.getSource() == m_btnTest)
                   m_areaTest.append("This is rubbish"+ nCount+ "\n");
                   nCount++;
                   //m_areaTest.paintImmediately();   
                   //bm_areaTest.scrollRectToVisible(m_areaTest.modelToView(textarea.getDocument().getLength()));
    }regards
    Praveen

  • Text Area Problem. Last Line not displayed

    HI,
    I am using TextArea component in the MXML script. We are having the problem of not displaying the last line in textarea. The textarea has scroll also. But, when scrolling only last line is not displayed and we have to see only by moving the mouse to component. We want to see all the text only by scrolling the bar. Please help me to resolve this issue.
    Thanks,
    Krishna

    Hi,
    Please check once your transport log, wether it was tranported correctly or not.
    Regards,
    Kumar

  • Boris Title Crawl Stop at last line

    I have the Boris Title Crawl that comes with FCP version 6. I have a "Credits" list at the end of the video with my company at the last line.
    TC continues (while the music is playing) until the last line scrolls off of the screen (I am using "Roll")
    I cant figure out how to make it stop rolling when the last line is at the middle of the screen.
    Any help would be appreciated

    Hi -
    Use the Percent Completion Slider. It is at the bottom of the Controls tab for your Title Crawl/Roll clip. You might need to scroll the controls window down to see it, It is near the bottom of the Controls pane.
    Start your roll by placing a keyframe with 0 Percent Complete, then move your playhead to where you want the roll to stop and make another keyframe. Adjust the slider till the line of type is in the position you want. The Roll will stop there. If you control-click on the second keyframe, you can spline the motion so that the roll will smoothly come to a stop.
    !http://www.spotsbeforeyoureyes.com/BorisRollPercentComplete.jpg!
    Hope this helps.

  • Can't read last line of SMS on 5530

    Hi,
    Just bought my new Nokia 5530 and out of the many issues I faced the one thats most annoying is I can't read the last line of a long SMS in the default view.
    Let me explain....
    If I recieve a SMS that is long and I have to scroll down (with the slider on the right) to read the complete message, The last line seems to be always hidden by the 'Forward/Delete' icons that appears at the bottom. I have to either go into the edit mode and scroll to the bottom to read the last line or have to go into options and change something when the last line would suddenly appear!
    This is very annoying to say the least.
    Anyone faced the same issue? Any resolutions please?

    Hi,
    Just bought my new Nokia 5530 and out of the many issues I faced the one thats most annoying is I can't read the last line of a long SMS in the default view.
    Let me explain....
    If I recieve a SMS that is long and I have to scroll down (with the slider on the right) to read the complete message, The last line seems to be always hidden by the 'Forward/Delete' icons that appears at the bottom. I have to either go into the edit mode and scroll to the bottom to read the last line or have to go into options and change something when the last line would suddenly appear!
    This is very annoying to say the least.
    Anyone faced the same issue? Any resolutions please?

  • TS2570 Still cant access my computer, followed instructions to perform a safe boot and last line of script it shows: BootCacheControl: Unable to open /Var/db/BootCache.playlist:2 No such file or directory. Any idea?

    Brand new Mac Book Pro
    Purchased in Mexico's Department store Liverpool on June 20th 2012
    Purchased Memory upgade to 8Gb, on june 27th, at Apple Store Memorial City, Houston
    Upgraded memory from 4Gb to 8Gb on June 28th.
    Tried to write on my external HD (previously written on a windows based PC) with no success.
    Upon reccomendation from a Mac assistant, copied all my external HD contents into my Mac Hard drive.
    Then formatted my external HD and copied back all my information to it.
    deleted all the  information from my MacBook Pro HD.
    Attempted to repeat the same operation with another external HD, but got a message saying there was not enough epace on the computer HD (even though I have a 750Gb Hard Drive.
    Looked in the trash bin and there it was, all the information previously deleted..
    Could not empty the trash bin, although I got a message asking me to safely delete all the information from the trash bin,
    Could not get enough space released, after several attpempts the trash bin was finally emptied.
    All happy getting acquainted with my new MacBook Pro. Two days later, got a gray screen and not able to start the computer.
    Looking in to Mac support I found article http://support.apple.com/kb/TS2570 and followed instructions to perform a safe boot:
    "Perform a Safe Boot
    Simply performing the Safe Boot may resolve this issue.
    Shut down your Mac. If necessary, hold your Mac's power button for several seconds to force it to power down.
    Start your Mac, then immediately hold the Shift key. This performs a Safe Boot. Advanced tip: If you want to
    see the status of a Safe Boot as it progresses, you can hold Shift-Command-V during start up (instead of just Shift).
    Note: A Safe Boot takes longer than a typical start up because it includes a disk check and other operations.
    The following is the script that appears on the screen upon safe boot. and halt after the last line.
    AppleACPICPU:Processor Id=6 LocalAplicId=255 Disabled
    AppleACPICPU:Processor Id=7 LocalAplicId=255 Disabled
    AppleACPICPU:Processor Id=8 LocalAplicId=255 Disabled
    calling mpo_policy_init for TMSafetyNet
    Security policy loaded: Safety net for Time Machine (TMSafetyNet)
    calling mpo_policy_init for Sandbox
    Security policy loaded: Seatbelt sandbox policy (Sandbox)
    calling mpo_policy_init for Quarantine
    Security policy loaded: Quarantine Policy (Quarantine)
    Copyright (c) 1982, 1986, 1989, 1991, 1993
            The Regents of the University of California. All Rights Reserved.
    MAC Framework Succesfully initializad
    using 16384 buffer headers and 10240 cluster IO buffer headers
    IOAPIC: Version 0x20 Vextors 64:87
    ACPI: System State [SO S3 S4 S5] (S3)
    PFM64 (36cpu) 0xf10000000, 0xf0000000
    Aplconsole relocated to 0xf1000000
    PCI configuration changed (bridge=16 device=4 cardbus=0)
    [ PCI configuration end, bridges 12 devices 16 ]
    Firewire (OHCI) Lucent ID 5901 built-in now active, GUID 3c0754fffe9b2aa2; max speed s800.
    Pthread support ABORTS when sync kernel primitives misused
    com.apple.AppleFSCompressionTypeZlib kmod start
    com.apple.AppleFSCompressionTypeDataless kmod start
    com.apple.AppleFSCompressionTypeZlib load succeeded
    com.apple.AppleFSCompressionTypeDateless load succeeded
    AppleIntelCPUPowerManagementClient: ready
    BTCOEXIST off
    wl0: Broadcom BCM4331 802.11 Wireless controller
    5.100.98.75
    AppleUSBMultitouchDriver::checkStatus - received Status Packet, Payload 2: device was reinitializad
    rooting via boot-uuid from /chosen: 6E918706-FC0D-37460-A3A0-6268A51DF93B
    Waiting on <dict ID="0"><key><string ID="1">IOResources</string><key>IOResourceMatch</key><string ID="2">boot-uuid-media</string></dict>
    Got boot device = IOService:/AppleACPIPlatformExpert/PCI0@0/AppleAPIPCI/SATA@1F,2/AppleIntelPchSe riesAHCI/PRT0@0/AOAHCIDevice@0/AppleAHCIDiskDriver/IOAHCIBlock
    storageevice /IoBlockStorageDriver/TOSHIBA MK7559GSXF Media/IOGUIDPartit
    BSD root: disk0s2, major 14, minor 2
    Kernel is LP64
    com.apple.launchd 1   com.apple.launchd 1   *** launchd[1] has started up. ***
    com.apple.launchd 1   com.apple.launchd 1   *** Verbose boot, will log to /dev/console. ***
    Running fsck on the boot volume...
    ** /dev/rdisk0s2 (NO WRITE)
    ** Root file system
       Executing FSCK_HFS (version diskdev_cmds-540.1~25).
    BootCacheControl: UNable to open /Var/db/BootCache.playlist:2 No such file or directory
    launchctl:Dubious permissions on file (skipping): /Library/LaunchDaemons
    launchctl:Dubious permissions on file (skipping): /System/Library/LaunchDaemons
    Any help or suggestions on what to do next would be welcomed.
    I am in the middle of the Atlantick, stuck with a brand new, non working Apple MacBook Pro.
    Best regards
    Sergio Ramos

    Reinstalling MacOS does NOT fix the problem for me.  I'm still searching a solution !
    Bernard

  • Last line is still not read

    Hi,
    I fixed error in last post and rewrote the code using ProcessBuilder instead.
    Output from my unix tool is:
    Command is a shell application' /home/myid/test/myshell/bin/run' that starts a ascii gui that looks like ( see below). It waits for input from user.
    *   My shell Application                   *
    HELP: h
    COMMAND: c
    QUIT:q
    135.19.45.18>
    And still the last line is not showing when I read the text.
    I have tried with readLine() and I get the same error.
    Note: When I debug, after characters in line:
    OUIT:q
    have been read I can see that debugger hangs on
    br.read()
    And now the code looks like:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.List;
    public class RunShellCmd {
        private BufferedReader processInputStream;
        private BufferedWriter processWriter;
        public static void main(String args[]) throws Exception {
            RunShellCmd cmd = new RunShellCmd();
            cmd.runCmd();
        public void runCmd() throws Exception {
            Process process = null;
            List<String> cmd = new ArrayList<String>();
            cmd.add("/bin/bash");
            cmd.add("/home/myid/test/myshell/bin/run");
            ProcessBuilder processBuilder = new ProcessBuilder(cmd);
            processBuilder.redirectErrorStream(true);
            try {
                System.out.println("start process ....");
                process = processBuilder.start();
                InputStream is = process.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                StringBuilder sb = new StringBuilder();
                int intch;
                while ((intch = br.read()) != -1) {
                  char ch = (char) intch;
                  sb.append(ch);
                  System.out.println(sb.toString());
            } catch (IOException e) {
                System.out
                        .println("An error occourd: "
                                + e.toString());
            } finally {
                processInputStream.close();
                process.destroy();
                System.out.println("exit value: " + process.exitValue());
                processWriter.close();

    DUPLICATE THREAD!
    Please don't create duplicate threads. You already have a thread for this same issue.
    I fixed error in last post and rewrote the code using ProcessBuilder instead.
    Good - then post your progress in the original thread and keep using it until the problem is resolved. That way the people that tried to help you before can see the entire context of what you are trying.
    https://forums.oracle.com/thread/2611844

  • How we can Print a field Value in last line of the layout

    Hello:
    I am doing Check printing report ... Accroding to that I am facing a problem to print a Field value in last line of the layout....Anyone Can help to this please.....

    Hello,
    Take a look to the example provided in the note :
    Note.253881.1 How to Create a Report With a Frame Only on the Last Page at a Fixed Position
    Regards

  • How do I prevent Pages from pushing paragraphs to next page when they would otherwise begin on the last line of the previous page?

    I am writing papers for school which have strict page limits so I can't afford to be losing lines at the bottom of a page because Pages thinks it looks better to have a new paragraph start on the next page. It does this automatically and I have to go back and manually format to get the first line of the paragraph to start on the last line of the previous page. This then creates problems down the line when I am editing so I would really like to be able to turn the feature off.
    e.g. with Pages auto-formatting
    -----page 1-----
         Paragraph #1 blah blah
    blah blah blah
         Paragraph #2 blah blah
    blah blah blah
    -----page 2-----
         Paragraph #3 blah blah
    blah blah
    e.g. after manual formatting
    -----page 1-----
         Paragraph #1 blah blah
    blah blah blah
         Paragraph #2 blah blah
    blah blah blah
         Paragraph #3 blah blah
    -----page 2-----
    blah blah

    Change the keep with next rules in:
    Inspector > Text > More > Pagination & Break
    Peter

  • Best way to remove last line-feed in text file

    What is the best way to remove last line-feed in text file? (so that the last line of text is the last line, not a line-feed). The best I can come up with is: echo -n "$(cat file.txt)" > newfile.txt
    (as echo -n will remove all trailing newline characters)

    What is the best way to remove last line-feed in text file? (so that the last line of text is the last line, not a line-feed). The best I can come up with is: echo -n "$(cat file.txt)" > newfile.txt
    (as echo -n will remove all trailing newline characters)
    According to my experiments, you have removed all line terminators from the file, and replaced those between lines with a space.
    That is to say, you have turned a multi-line file into one long line with no line terminator.
    If that is what you want, and your files are not very big, then your echo statement might be all you need.
    If you need to deal with larger files, you could try using the 'tr' command, and something like
    tr '
    ' ' ' <file.txt >newfile.txt
    The only problem with this is, it will most likely give you a trailing space, as the last newline is going to be converted to a space. If that is not acceptable, then something else will have to be arranged.
    However, if you really want to maintain a multi-line file, but remove just the very last line terminator, that gets a bit more complicated. This might work for you:
    perl -ne '
    chomp;
    print "
    " if $n++ != 0;
    print;
    ' file.txt >newfile.txt
    You can use cat -e to see which lines have newlines, and you should see that the last line does not have a newline, but all the others still do.
    I guess if you really did mean to remove all newline characters and replace them with a space, except for the last line, then a modification of the above perl script would do that:
    perl -ne '
    chomp;
    print " " if $n++ != 0;
    print;
    ' file.txt >newfile.txt
    Am I even close to understanding what you are asking for?

  • How to select LAST line from a SAP table?

    Hi developers,
    I need to select the last line from a SAP table, NOT from an internal table.
    Something like
    SELECT SINGLE LAST FROM pa00169..
    any help is more than welcome.

    Javier,
    Based on this statement from you:
    "i created a Z table, its a config table that stores some bussines rules, and an id counter, so in order to update that counter i need the last line of the ztable."
    I have one additional solution which is the "text box" approach IF the 'id counter' mentioned above is a numeric data type.
    If so, use an SQL function to get the highest value of ID_COUNTER.
    Then simply add one to it to produce a new, unique, higher-order primary key.
    See below:
    data: l_id_counter like ztable-id_counter.
    data: new_id_counter like ztable-id_counter.
    select max( ID_COUNTER ) from ztable into l_id_counter.
    new_id_counter = l_i_counter + 1.
    Now new_id_counter will be the unique, primary key IF this is the only field in ZTABLE's primary key.
    This will also GUARANTEE that your new record is built correctly.

Maybe you are looking for