复制过程中显示在 JTextArea 中 [英] Display in JTextArea when copying is in progress

查看:23
本文介绍了复制过程中显示在 JTextArea 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当复制正在进行时,我需要在 JTextArea 上显示所有 system.out.println 语句.我尝试给出 ta.append 而不是 println 语句,但它不会显示.请让我知道我该怎么做.

I need to have all the system.out.println statements to be displayed on the JTextArea when the copying is in progress. I tried giving ta.append instead of the println statements but it won't desplay. Please let me know how should I go about doing this.

public class copy {

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Copy c = new Copy();
                c.setTitle("Copy folders");
                c.setVisible(true);
            }
        });

        JPanel jp = new JPanel();

        TextArea ta = new JTextArea(5, 50);
        ta.setEditable(false);
        DefaultCaret caret = (DefaultCaret) ta.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        JScrollPane scrollPane = new JScrollPane(ta, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBounds(6, 625, 1035, 296);
        jp.add(scrollPane); //Adding to JPanel    
    }

    public Copy() {
        build();
    }

    public void build() {
        String source = "\\hostname\\d$\\somedirecotry";
        String detination = "\\C:\\foldername";
        File s = new File(source);
        File s2 = new File(detination);

        if (!s.exists()) {
            System.out.println("Directory does not exist.");
        } else if (!s2.exists()) {
            System.out.println("Directory is not accessible or Server is down");
        } else {
            try {
                copyFolder(s, s2);
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(0);
            }
        }
        System.out.println("Done");
    }

    public static void copyFolder(File src, File dest)
            throws IOException {
        if (src.isDirectory()) {
            //if directory not exists, create it
            if (!dest.exists()) {
                dest.mkdir();
                System.out.println("Directory copied from " + src + "  to " + dest);
            }

            //list all the directory contents
            String files[] = src.list();

            for (String file : files) {
                File srcFile = new File(src, file);
                File destFile = new File(dest, file);
                copyFolder(srcFile, destFile);
            }
        } else {
            //if file, then copy it
            InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest);

            byte[] buffer = new byte[1024];

            int length;
            //copy the file content in bytes 
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }

            in.close();
            out.close();
            System.out.println("File copied from " + src + " to " + dest);
        }
    }
}

推荐答案

Swing 是一个事件驱动的环境,这包括但不限于键盘、鼠标和绘制事件.

Swing is an event driven environment, this includes, but is not limited to, keyboard, mouse and paint events.

这些事件由事件调度线程传递.任何阻塞该线程的操作(例如但不限于循环、I/O 或 Thread#sleep)都将阻止(除其他外)开始处理绘制请求.这将使您的应用程序停止响应按键和鼠标事件,并使其看起来像挂起一样.

These events are delivered by the Event Dispatching Thread. Any operation (such as, but not limited to, loops, I/O or Thread#sleep) that blocks this thread will prevent (amongst other things) paint requests from begin processed. This will make your application stop responding to key and mouse events as well as make it look like its hung.

解决问题的最简单方法是将物理复制进程移至单独的线程.使用 SwingWorker 最容易实现这一点.

The easiest fix to your problem is to move the physical copy process to a separate thread. This is most easily accomplished by using a SwingWorker.

查看Swing 中的并发,在特定的事件调度线程工作线程和 SwingWorker

可以找到例子

这篇关于复制过程中显示在 JTextArea 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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