pessoal tenho um JScrollPanel, como eu faco para ele ficar tipo o comando tail(linux),sempre acompanhando a ultima linha inserida,ha algum flag que é só setar true algo do tipo, ou tenho q implementalo?(e como seria?)
Duvida em JScrollPane[RESOLVIDO]
6 Respostas
Você está referindo ao JScrollPane em qual componente ?
Porque em um JTextArea por exemplo ele já acompanha a última linha por default.
estou usando o JTextArea mesmo, e não esta acompanhando a cada append q eu dou nao!!
Oi,
Na realidade você não precisa alterar seu JScrollPane e sim o seu TextArea.:
textArea.setCaretPosition(textArea.getDocument().getLength());
ou
textArea.setCaretPosition(textArea.getText().getLength());
Tchauzin!
vlw brow !!!,era isso mesmo obrigadao !!!
Você está referindo ao JScrollPane em qual componente ?
Porque em um JTextArea por exemplo ele já acompanha a última linha por default.
De onde vc tirou isso? Ele não acompanha não.
O que você tem que fazer é posicionar o cursor na última linha do seu JTextArea, via código:
textComponent.setCaretPosition(textComponent.getDocument().getLength());
Obs: Postei junto da lina. Pelo visto essa é a solução mesmo.
Essa é uma classe "Writer" que escreve num JTextArea. Inclui uma opção de AutoScroll e já resolve alguns problemas comuns, como o travamento da tela caso vários textos sejam enviados juntos.
Você pode associa-la ou usa-la como outros writers:
import java.awt.EventQueue;
import java.io.IOException;
import java.io.Writer;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
public class TextComponentWriter extends Writer
{
private StringBuilder buffer = new StringBuilder();
private Thread bufferThread;
private JTextComponent textComponent;
private boolean autoScroll = true;
public TextComponentWriter(JTextComponent textComponent)
{
this.textComponent = textComponent;
Runnable r = new Runnable()
{
public void run()
{
try
{
while (!Thread.interrupted())
{
writeBuffer();
Thread.sleep(100);
}
}
catch (InterruptedException e)
{
}
writeBuffer();
}
};
bufferThread = new Thread(r, "TextComponentWriter Thread");
bufferThread.setDaemon(true);
bufferThread.start();
}
@Override
public void write(final int b) throws IOException
{
write(new char[] {(char) b}, 0, 1);
}
@Override
public synchronized void write(char[] cbuf, int off, int len)
throws IOException
{
buffer.append(cbuf, off, len);
}
private synchronized void writeBuffer()
{
if (buffer.length() == 0)
return;
final String str = buffer.toString();
buffer = new StringBuilder();
EventQueue.invokeLater(new Runnable()
{
public void run()
{
if (isAutoScroll())
textComponent.setCaretPosition(textComponent.getDocument().getLength());
else if (textComponent.getCaretPosition() == textComponent.getDocument().getLength()
&& textComponent.getDocument().getLength() > 0)
textComponent.setCaretPosition(textComponent.getDocument().getLength() - 1);
try
{
textComponent.getDocument().insertString(
textComponent.getDocument().getLength(), str, null);
}
catch (BadLocationException e)
{
}
}
});
}
@Override
public void flush()
{
}
@Override
public void close()
{
bufferThread.interrupt();
}
public boolean isAutoScroll()
{
return autoScroll;
}
public void setAutoScroll(boolean autoScroll)
{
this.autoScroll = autoScroll;
}
@Override
protected void finalize() throws Throwable
{
close();
}
}
Isso permite fazer coisas como:
PrintWriter writer = new PrintWriter(new TextComponentWriter(seuTextArea));
writer.println("Olá enfermeira!!");
writer.println("Tudo bem?");