使用FileReader的SwingWorker [英] SwingWorker with FileReader

查看:140
本文介绍了使用FileReader的SwingWorker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于使用FileReader应用SwingWorker的问题,我的观点是我需要使用SwingWorker实现FileReader以使我的UI显示文件中的文本,这是我的代码

I have problem about applied SwingWorker with FileReader and my point is I need to implement FileReader with SwingWorker to make my UI Show the text from the file and this is my code

class Read1 extends SwingWorker<String,String>{
    protected Void doInBackground() throws Exception{
        FileReader read = new FileReader("msg.txt");
        BufferedReader in = new BufferedReader(read);
        String s;
        s=in.readLine();
        System.out.println(s);
        return s;
    }
   protected void done()
    {
      try{
       String show;
       show=get();
       textArea.append(show);}catch(Exception e){}}

 public static void main(String args[]) {
   Read1 r = new Form().new Read1();
   r.execute();

但它不会在UI textarea上附加任何内容

However it does not append anything on the UI textarea

有人有解决方案吗?谢谢

anyone have solution? Thank you

推荐答案

对我来说工作得很好:

public class Reader extends SwingWorker<List<String>, String> {

    protected List<String> doInBackground() throws Exception {

        ArrayList<String> lstText = new ArrayList<String>(25);

        BufferedReader in = null;
        try {

        FileReader read = new FileReader("Scanner.txt");
        in = new BufferedReader(read);
        String s = null;

        while ((s = in.readLine()) != null) {

            lstText.add(s);
            publish(s);

        }

        } finally {


            try {

                in.close();

            } catch (Exception e) {
            }
        }

        return lstText;

    }

    @Override
    protected void process(List<String> chunks) {

        for (String text : chunks) {

            fldText.append(text + "\n");

        }

    }

    @Override
    protected void done() {
    }
}

这篇关于使用FileReader的SwingWorker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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