处理时,JTextArea无法滚动或访问文本

时间:2016-12-02 00:49:28

标签: java jtextarea

我的JTextArea附加了文本,然后用

更新
jTexaArea.update(jTextArea.getGraphics());

但是在追加时我无法选择或编辑文字也无法滚动。 NetBeans系统输出允许这些功能,如何将它们包含在我的程序中?

1 个答案:

答案 0 :(得分:1)

如果由JButton点击事件触发,则附加文本时,UI会在事件结束前无响应。这是因为事件调度线程在继续执行下一个事件之前等待事件完成。

如果您的要求是在处理事件时处理文本区域,您可以选择以下方法:

  1. 使用SwingWorker
  2. 在JTextArea中实现Lazy附加文本
  3. 以下是一个示例延迟附加示例:

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.SwingWorker;
    
    import java.awt.Font;
    
    import javax.swing.JScrollPane;
    
    import java.awt.GridLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.Date;
    import java.util.List;
    
    import javax.swing.Timer;
    import javax.swing.text.DefaultCaret;
    
    public class LazyAppender extends JFrame implements ActionListener  {
    
        /**
         * Demonstrates two different ways of appending text into TextArea
         */
        private static final long serialVersionUID = 1L;
        JTextArea textArea1;
        JTextArea textArea2;
    
        public LazyAppender() {
            initUI();
        }
    
        public final void initUI() {
            GridLayout experimentLayout = new GridLayout(2,2);
            JPanel panel = new JPanel();
            getContentPane().add(panel);
    
            panel.setLayout(experimentLayout);
    
            //Button 1
            JButton button1 = new JButton("Button 1");
            button1.setToolTipText("Append JtextArea1 without using Swing Timer");  
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {
    
                    textArea1.append(getText());
                    textArea1.update(textArea1.getGraphics());
                }
            });
    
    
    
            //Button 2
            JButton button2 = new JButton("Button 2");
            button2.setToolTipText("Lazy Append JtextArea2 using Swing Timer");
    
            Timer timer2 = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    textArea2.append(getText());                    
                }
            });
            timer2.setRepeats(false); //Execute only once
    
    
            button2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    //Start timer, this will release the button immediately
                    timer2.start();
                }
            });
    
    
            //TextArea 1
            textArea1 = new JTextArea("This is an editable JtextArea1. " +
                    "A text area is a \"plain\" text component, " +
                    "which means that although it can display text " +
                    "in any font, all of the text is in the same font."
                    );
            textArea1.setFont(new Font("Serif", Font.ITALIC, 16));
            textArea1.setLineWrap(true);
            textArea1.setWrapStyleWord(true);
    
            //ScrollPane 1
            JScrollPane areaScrollPane1 = new JScrollPane(textArea1);
            areaScrollPane1.setViewportView(textArea1);
    
    
            //TextArea 2
            textArea2 = new JTextArea("This is an editable JtextArea2. " +
                    "A text area is a \"plain\" text component, " +
                    "which means that although it can display text " +
                    "in any font, all of the text is in the same font."
                    );
            textArea2.setFont(new Font("Serif", Font.ITALIC, 16));
            textArea2.setLineWrap(true);
            textArea2.setWrapStyleWord(true);
    
            //ScrollPane 2
            JScrollPane areaScrollPane2 = new JScrollPane(textArea2);
            areaScrollPane2.setViewportView(textArea2);
    
    
            //Add Components into Panel
            panel.add(button1);
            panel.add(button2);
            panel.add(areaScrollPane1);
            panel.add(areaScrollPane2);
    
    
            setTitle("Text Area Append Verify");
            setSize(300, 200);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    
    
        public void actionPerformed(ActionEvent e) {
    
        }
    
        public String getText(){
            String stringToAppend = "This is your life. Do what you want and do it often." +
                    "If you don't like something, change it." +
                    "If you don't like your job, quit." +
                    "If you don't have enough time, stop watching TV." +
                    "If you are looking for the love of your life, stop; "+
                    "they will be waiting "+ 
                    "for you when you start doing things you love." +
                    "Stop over-analysing, life is simple." +
                    "All emotions are beautiful." +
                    "When you eat, appreciate every last bite." +
                    "Life is simple." +
                    "Open your heart, mind and arms to new things and people, "+ 
                    "we are united in our differences." +
                    "Ask the next person you see what their passion is and "+
                    "share your inspiring dream with them." +
                    "Travel often; getting lost will help you find yourself." +
                    "Some opportunities only come once, seize them." +
                    "Life is about the people you meet and the things you create "+
                    "with them, so go out and start creating." +
                    "Life is short, live your dream and wear your passion." +
                    "~ Holstee Manifesto, The Wedding Day ";
            //Simulated delay
            long start = new Date().getTime();
            while(new Date().getTime() - start < 1000L){}
            return stringToAppend;
        }
    
    
        public static void main(String[] args) {
    
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    LazyAppender ex = new LazyAppender();
                    ex.setVisible(true);
                }
            });
    
    
        }
    }
    

    另外,请查看此问题JTextArea thread safe?,深入讨论在多线程环境中对JTextArea的安全性是否安全。