将JTextArea设置为“不可编辑"时,如何设置自动滚动? [英] How to set autoscrolling in a JTextArea while it is set to NOT editable?

查看:653
本文介绍了将JTextArea设置为“不可编辑"时,如何设置自动滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了答案获得在JTextArea上自动滚动的功能.但是似乎只有在相同JTextArea的setEditable属性设置为TRUE时,它才有效.当然,我使用的是JScrollPane.

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.

我正在将聊天应用程序开发为一个大学项目.显示消息的区域是完全相同的JTextArea.我在上面链接的第一个答案中使用了第二组代码.但是当setEditable设置为FALSE时,我需要它来使其工作.

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.

即使在JTextArea&的setAutoScrolls属性之后,它也无法工作JScrollPane设置为TRUE.

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

请帮助.谢谢.

推荐答案

我已经使用此答案来获得自动滚动功能 JTextArea.但是似乎只有在setEditable时它才起作用 同一JTextArea的属性设置为TRUE.当然,我用 JScrollPane

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

  • 我没有发现setEditable(false)和/或setEnabled(false)及其可能的组合有任何问题

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

      (陷入黑暗)(排除 EventDispatchThread ,当 setText时, append 等未在EDT上完成,例如包裹在invokeLater()

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

      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();
                  }
              });
          }
      }
      

      这篇关于将JTextArea设置为“不可编辑"时,如何设置自动滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆