JScrollPaneスクロールバーは自動的に一番下に回転します



Jscrollpane Scroll Bar Automatically Rolls Bottom



タイトルの効果を実現するために、JScrollPane(JTextArea)のJTextAreaにコンテンツを追加します。

JTextArea ta = new JTextArea(10, 40)// Message receiving display box
JScrollPane pane = new JScrollPane(ta)


class ReadMsg extends Thread {
public void run() {
InputStream is = null
BufferedReader br = null
String msg = null
OutputStream os = null
PrintStream ps = null

// reader object from server
try {
is = client.getInputStream()
br = new BufferedReader(new InputStreamReader(is, 'utf-8'))

// After the client connection is successful, first send a nickname in the past
os = client.getOutputStream()
ps = new PrintStream(os)
ps.println(nameInput.getText())
} catch (Exception ex1) {
// ex1.printStackTrace()
}

while (true) {
try {
while ((msg = br.readLine()) != null) {
ta.append(msg + ' ')
/ / Key points: call this method to achieve the effect of the title
ta.setSelectionStart(ta.getText().length())
}
} catch (Exception ex) {
// ex.printStackTrace()
}
}
}
}