2014-03-13 27 views
1

我已使用this答案以獲取JTextArea上的自動滾動功能。但它似乎只在同一個JTextArea的setEditable屬性設置爲TRUE時才起作用。當然,我使用JScrollPane。如何在JTextArea中設置爲不可編輯時自動滾動?

我正在開發一個聊天應用程序作爲大學項目。消息顯示的區域與JTextArea完全相同。我在上面的鏈接中使用了第一組答案中的第二組代碼。但是我需要它在setEditable設置爲FALSE時使其工作。

即使在JTextArea的setAutoScrolls屬性& JScrollPane設置爲TRUE後,它也不會工作。

請幫忙。感謝名單。

回答

3

我已使用此答案獲取自動滾動功能的 JTextArea。但它似乎只在同一個JTextArea的setEditable 屬性設置爲TRUE時才起作用。當然,我使用的是 JScrollPane的

  • 我沒有看到有任何問題的setEditable(false)和/或setEnabled(false)與他們可能的組合

  • (野生射進黑暗),不包括與問題EventDispatchThread ,當setText, append等沒有在EDT上完成時,例如裹成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(); 
      } 
     }); 
    } 
} 
0

試試這個小例子:

JPanel myPanel=new JPanel(); 
myPanel.setBorder(new TitledBorder(new EtchedBorder(), "My Application")); 

JTextAea textArea = new JTextArea(16, 58); 
textArea.setEditable(false); // set textArea to non editable 
JScrollPane scrollPane = new JScrollPane(textArea); 
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 

myPanel.add(scrollPane); 

是否行得通?

相關問題