English 中文(简体)
经常呼吁将Text(Text)列入多读的互传节目
原标题:Frequent calls to setText() in multithreaded Swing program

I have a Swing program where work is continuously being done in a non-Swing thread. It often needs to update a JTextPane -- frequently many times per second. I realize that setText() needs to be called from back inside the event-dispatching thread, but I cant figure out how to make this happen smoothly.

以下最低完整的例子与我用管道InputStream/PipedOutputStream乳房接手的情况一样接近,但这似乎只是每秒更新一次。 我不相信花这么久的时间。

import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class TextTest extends JFrame {
    private JTextPane out = new JTextPane();
    private PipedInputStream pIn = new PipedInputStream();
    private PrintWriter pOut;

    public TextTest() {
        try {
            pOut = new PrintWriter(new PipedOutputStream(pIn));
        }
        catch (IOException e) {System.err.println("can t init stream");}

        add(new JScrollPane(out));
        setSize(500, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        // Start a loop to print to the stream continuously
        new Thread() {
            public void run() {
                for (int i = 0; true; i++) {
                    pOut.println(i);
                }
            }
        }.start();

        // Start a timer to display the text in the stream every 10 ms
        new Timer(10, new ActionListener() {
            public void actionPerformed (ActionEvent evt) {
                try {
                    if (pIn.available() > 0) {
                        byte[] buffer = new byte[pIn.available()];
                        pIn.read(buffer);
                        out.setText(out.getText() + new String(buffer));
                    }
                }
                catch (IOException e) {System.err.println("can t read stream");}
            }
        }).start();
    }

    public static void main(String[] args) {
        new TextTest();
    }
}

我是否执行这一错误? 我是否对如何从厄立特里亚国防军之外不断更新JTextPane的说法完全错误?

最佳回答

setText( “方法已经安全,尽管大多数使用方法并不可行。 请参看。 如何使用更多的信息

页: 1 值得注意的另一件事是:http://download.oracle.com/javase/6/docs/api/javax/swing/timer.html” rel=“nofollow”>javax.swing.timer在电子数据处理上执行。 我的改动如下:

import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.text.DefaultCaret;

public class TextTest extends JFrame {

    private JTextArea out = new JTextArea();
    private PipedInputStream pIn = new PipedInputStream();
    private PrintWriter pOut;

    public TextTest() {
        try {
            pOut = new PrintWriter(new PipedOutputStream(pIn));
        } catch (IOException e) {
            System.err.println("can t init stream");
        }

        DefaultCaret caret = (DefaultCaret) out.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

        add(new JScrollPane(out));
        setSize(300, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        // Start a loop to print to the stream continuously
        new Thread() {

            public void run() {
                for (int i = 0; true; i++) {
                    pOut.println(i);
                }
            }
        }.start();

        // Start a timer to display the text in the stream every 10 ms
        new Timer(10, new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                try {
                    out.append(String.valueOf((char) pIn.read()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public static void main(String[] args) {
        new TextTest();
    }
}
问题回答

但这似乎只是每秒更新一次。 我不相信花这么久的时间。

System.out.println(pIn.available());

我在《时代者行动守则》中添加了上述声明。 直到缓冲带达到1024英寸。 因此,我gues想你们需要改变缓冲规模。

而且,你不应使用集束炸弹。 每当你们作出改变时,都无法重启文件。

您可以使用:

out.replaceSelection(new String(buffer) );

或者更常见的做法是使用:

Document doc = textPane.getDocument();
doc.insertString("...", doc.getLength(), null);

不用说,“String(String)”方法已经变得安全,但“替代选择”方法却是安全的。

Edit:

仅仅尝试在投入流中玩弄10个缓冲器,冲积了烟流,而且拖了任何变化,因此我猜测我不懂管道。

你们需要 flu清你的印刷作品的输出结果,而且,鉴于其很紧的路面,可以让更新的路面一度出现。

 pOut.println(i); 
 pOut.flush();
 try {
      sleep(10);
 } catch (InterruptedException e) {
 }

这将使流动更加顺利。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签