TextArea replacement

Can anyone recommend a lightweight replacement for TextArea? The
most common complaint about TextArea used as a chat window is that
it auto-scrolls inappropriately, but it would also be nice to have access
to fonts and color.

I would prefer something lightweight and not tied to some other
massive package.

You can programatically make it scroll how you want. How is it not scrolling how you need it to?

the big complaint is that when the text is updated, the window auto-scrolls to the bottom.
I haven’t seen a way to prevent this.

It took some experimenting, but here is a solution.


import java.awt.Adjustable;
import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollBar;
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 

/** 
 * 
 */ 
public class Test extends JFrame implements ActionListener, AdjustmentListener { 
    private JTextArea text; 
    private JScrollPane scroll; 
    private JButton addText;
    private int value;
    private int appended = 0;
    private boolean max = false;
    
    public Test() { 
        JPanel panel = new JPanel(new BorderLayout()); 
        setContentPane(panel); 
        text = new JTextArea(); 
        scroll = new JScrollPane(text); 
        panel.add(scroll, BorderLayout.CENTER);
        scroll.getVerticalScrollBar().addAdjustmentListener(this);
        addText = new JButton("Add Text"); 
        panel.add(addText, BorderLayout.SOUTH); 
        addText.addActionListener(this); 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        setSize(400, 200); 
        setVisible(true); 
    } 
    public void actionPerformed(ActionEvent e) { 
        if(e.getSource().equals(addText)) {
            JScrollBar bar = scroll.getVerticalScrollBar(); 
            value = bar.getValue();
            if(value + bar.getVisibleAmount() >= bar.getMaximum()) {
                max = true;
            }
            else {
                max = false;
            }
            //value needs to be set to 2 because the adjustmentValueChanged gets called twice for some reason
            appended = 2;
            text.append("This is a test string to add\r\n"); 
        }
    }
    public void adjustmentValueChanged(AdjustmentEvent arg0) {
        Adjustable adj = arg0.getAdjustable();
        if(appended > 0) {
            if(max) {
                adj.setValue(adj.getMaximum());
            }
            else {
                adj.setValue(value);
            }
        }
        appended--;
    }
    public static void main(String[] args) { 
        Test t = new Test(); 
    } 
} 

Thanks for trying, but this isn’t completely satisfactory. It glitches funny sometimes, and
I’d rather avoid swing components - my applet is still compatible with Java 1.1 and I’d
prefer to keep it that way.

… Still hoping for a nice class specifically designed as a chat area.

Hrm? JTextArea doesn’t automatically scroll to the bottom. I had to make it do that myself.

I think he’s talking about java.awt.TextArea

No idea if it works, but maybe this is something for you?
http://jabberapplet.jabberstudio.org/doxygen/ChatTextArea_8java-source.html

That looks promising, but the big missing feature is cut and paste - there is none.
Textarea somehow manages to implement access to the clipboard without tickling
the security manager, but ordinary java applets have to be signed.