Package com.jedics.swing

Source Code of com.jedics.swing.ScrollableJTextArea

package com.jedics.swing;

import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class ScrollableJTextArea extends JScrollPane {
  private JTextArea text;
  private boolean autoscroll;
 
  public ScrollableJTextArea() {
    text = new JTextArea();
    setViewportView(text);
    getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
      @Override
      public void adjustmentValueChanged(AdjustmentEvent e) {
        if(e.getValue() != ((JScrollBar)e.getSource()).getMaximum() - ((JScrollBar)e.getSource()).getVisibleAmount()) autoscroll = false;
        else autoscroll = true;
      }
    });
    text.setLineWrap(true);
    text.setEditable(false);
    text.setWrapStyleWord(true);
  }
 
  public void append(String s) {
    text.append(s);
    if(autoscroll) {
      text.setCaretPosition(text.getText().length());
    }
  }
}
TOP

Related Classes of com.jedics.swing.ScrollableJTextArea

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.