I have a JtextArea in which I have to append text and I want it to scroll down when appending new text. I have done the following but it doesn't work.
showFrame = new JFrame("Gui Console");
showArea = new JTextArea();
showArea.setBorder(new TitledBorder("Console"));
showArea.setPreferredSize(new Dimension(500, 400));
showArea.setMinimumSize(new Dimension(500, 400));
showArea.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
showArea.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
JScrollBar vertical = scrollPane.getVerticalScrollBar();
scrollPane.getVerticalScrollBar().setValue( vertical.getMaximum() );;
}
public void removeUpdate(DocumentEvent e) {
}
public void changedUpdate(DocumentEvent e) {
}
});
The only way that works is to set
showArea.setPreferredSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
but the window becomes too big and I don't want it.