The Artima Developer Community
Sponsored Link

Java Answers Forum
Following the last line

6 replies on 1 page. Most recent reply: Mar 24, 2004 11:16 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Following the last line Posted: Mar 23, 2004 9:07 AM
Reply to this message Reply
Advertisement
I use a JTextArea in a JScrollPane to log outputs.
How can I follow (moving the scoll bar) the last line added?


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Following the last line Posted: Mar 23, 2004 4:00 PM
Reply to this message Reply
short question, short answer.
By setting the ViewPort :) or more exactly the JViewPort.

To translate the origine of the view port, you can do smthg like this :

Point point = viewPort.getViewPosition ();
point.x += translation.x;
point.y += translation.y;
viewPort.setViewposition (point);


\T,

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Following the last line Posted: Mar 23, 2004 11:07 PM
Reply to this message Reply
thx

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Following the last line Posted: Mar 23, 2004 11:19 PM
Reply to this message Reply
(I hit "post message" too soon)
This method using a incremental value would work if I had no line wraps and if the user would not do anything.

Since I have some long Strings in the log, there are line wraps.

I tried to get the absolute height from the JTextArea Object, but I only got the displayed size.

This is why I hoped that there would be a special method to do this.

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Following the last line Posted: Mar 24, 2004 3:57 PM
Reply to this message Reply
Matthias,

Please do find the partial answer in this example !
There is still one thing I miss in my example ...
Why is it well shown only after I resized it ?
Not much else to say / write aboutit at the moment ....
I am struggling with paint () & repaint

Cheers,

\T,


/*
* Created on 24-Mar-04
*
*/
package com.tls.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.util.Random;

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

/**
* @author tsmets
*
*/
public class ViewPort
extends JFrame
{
public static final char[] LOWER_ALPHABET = "azertyuiopmlkjhgfdsqwxcvbn".toCharArray();
private JTextArea textArea = null;

public ViewPort (String aFrameName)
{
super (aFrameName);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setSize ( 400, 400);
setVisible (true);
textArea = new JTextArea ( "Test Scroll AREA ");
JScrollPane scrollPane = new JScrollPane ( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.add (textArea);
scrollPane.setViewportView (textArea);
Container container = getContentPane();

JComponent component = new JPanel ();
component.setOpaque (true);
component.add (scrollPane);

container.setLayout (new BorderLayout ());
container.add ( component, BorderLayout.CENTER);
container.add ( new JTextField (40), BorderLayout.NORTH);
new Thread (new TextAdder ()).start ();
}

public static void main(String[] args)
{
new ViewPort ("TestFrame");
}

public class TextAdder
implements Runnable
{
Random random = new Random ();
public void run ()
{
int i = 0,
j = 1;

while (true)
{
if ( (i % j) == 0)
{
textArea.setText ( textArea.getText() + "\n");
j = Math.abs(random.nextInt()) % 80 + 1;
System.out.println ("j = " + j);
}
System.out.println ("public void run () " + i);
textArea.setText ( textArea.getText() + " "
+ LOWER_ALPHABET[++i % LOWER_ALPHABET.length] );
// This is what interests you
textArea.setCaretPosition(textArea.getDocument().getLength());
try
{
Thread.sleep (5);
} catch (Exception e)
{
System.out.println ("Exception. Message " + e.getMessage());
}
}
}
}
}


For colorizing of the code you just need to annoy Bill ;)

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Following the last line Posted: Mar 24, 2004 4:03 PM
Reply to this message Reply
Matthias,
Just update the constructor witht the code herebelow.
Same remark about the colorization ;p
\T,


*******************

public ViewPort (String aFrameName)
{
super (aFrameName);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

textArea = new JTextArea ( "Test Scroll AREA ", 20, 35);
JScrollPane scrollPane = new JScrollPane ( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.add (textArea);
scrollPane.setViewportView (textArea);
Container container = getContentPane();

JComponent component = new JPanel ();
component.setOpaque (true);
component.add (scrollPane);

container.setLayout (new BorderLayout ());
container.add ( component, BorderLayout.CENTER);
container.add ( new JTextField (40), BorderLayout.NORTH);
new Thread (new TextAdder ()).start ();
setVisible (true);
pack();
}

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Following the last line Posted: Mar 24, 2004 11:16 PM
Reply to this message Reply
Thanks for the help.
I was abe to do solve the problem.


I'm not completely happy with the result (sometimes the background of the dialog doesn't get painted, sometimes the view jumps for a short moment to the right, when a new line is added), but It does what it it's supposed to do.

Flat View: This topic has 6 replies on 1 page
Topic: TSR Programm Previous Topic   Next Topic Topic: How to

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use