java jtextarea 不可编辑,将JTextArea设置为“不可编辑”时,如何设置自动滚动?

I have used this answer to get the functionality of Autoscrolling on a JTextArea. But it seems that it works only when the setEditable property of the same JTextArea is set to TRUE. Of course, i use a JScrollPane.

I'm developing a chat application as a college project. The area where the messages are displayed is the very same JTextArea. I've used the 2nd set of code in the 1st answer in the above link. But I need it to make it work when setEditable is set to FALSE.

It wont work even after the setAutoScrolls properties of JTextArea & JScrollPane are set to TRUE.

Please help. Thanx.

解决方案

I have used this answer to get the functionality of Autoscrolling on a

JTextArea. But it seems that it works only when the setEditable

property of the same JTextArea is set to TRUE. Of course, i use a

JScrollPane

I didn't see there any issue for setEditable(false) and/or setEnabled(false) with theirs possible combinations

(wild shot into dark) excluding issue with EventDispatchThread, when setText, append etc isn't done on EDT, e.g. wrapped into invokeLater()

nnxul.jpg

import java.awt.BorderLayout;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.text.DefaultCaret;

public class CaretAndJTextArea {

private JTextArea textArea = new JTextArea();

private static final String string =

"Trail: Creating a GUI with JFC/Swing\n"

+ "Lesson: Learning Swing by Example\n"

+ "This lesson explains the concepts you need to\n"

+ " use Swing components in building a user interface.\n"

+ " First we examine the simplest Swing application you can write.\n"

+ " Then we present several progressively complicated examples of creating\n"

+ " user interfaces using components in the javax.swing package.\n"

+ " We cover several Swing components, such as buttons, labels, and text areas.\n"

+ " The handling of events is also discussed,\n"

+ " as are layout management and accessibility.\n"

+ " This lesson ends with a set of questions and exercises\n"

+ " so you can test yourself on what you've learned.\n"

+ "http://docs.oracle.com/javase/tutorial/uiswing/learn/index.html\n";

public CaretAndJTextArea() {

DefaultCaret caret = (DefaultCaret) textArea.getCaret();

caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

textArea.setEditable(false);

textArea.setEnabled(false);

textArea.getDocument().addDocumentListener(new DocumentListener() {

@Override

public void insertUpdate(DocumentEvent e) {

setModelText();

}

@Override

public void removeUpdate(DocumentEvent e) {

setModelText();

}

@Override

public void changedUpdate(DocumentEvent e) {

setModelText();

}

private void setModelText() {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

//textArea.setText(textArea.getText());

}

});

}

});

JButton button2 = new JButton("Append String");

button2.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

textArea.append(string);

}

});

JFrame frame = new JFrame();

frame.add(new JScrollPane(textArea), BorderLayout.CENTER);

frame.add(button2, BorderLayout.SOUTH);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(400, 200);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

new CaretAndJTextArea();

}

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值