在我的应用程序中添加进度条以跟踪任务移动

时间:2017-08-19 04:01:55

标签: java swing awt swingworker

// Can anyone please help

import java.awt.Color;

public class MoveTools {
    private static String ROOT_DIR = "C:/Users/Amitesh/Desktop/copy/";

    // private static String newpath = "C:/Users/Amitesh/Desktop/move/";

    private static String newpath;
    static long timeTaken;
    static int i = 0;

    JProgressBar progressBar;
    JFrame frame;
    JTextField textField;
    JTextField textField_1;
    JButton btnMove;
    JLabel lblSelectExcelFile;
    JLabel lblMoveTo;
    JButton btnBrowse;
    JButton btnLocation;
    JLabel lblA;
    JLabel lblB;
    JLabel lblSuccess;

    public static void main(String[] args) {
        MoveTools window = new MoveTools();
        window.frame.setVisible(true);
    }

    public MoveTools() {
        initialize();
    }

    public void initialize() {
        frame = new JFrame("File Move Tools");
        frame.getContentPane().setBackground(Color.LIGHT_GRAY);
        frame.getContentPane().setForeground(Color.GREEN);
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setResizable(false);

        btnMove = new JButton("Move");

        ButtonListener listener = new ButtonListener();
        btnMove.addActionListener(listener);

        Color cr1 = new Color(79, 116, 202);
        btnMove.setBackground(cr1);
        btnMove.setToolTipText("Click here to move");
        btnMove.setBounds(163, 185, 89, 23);
        frame.getContentPane().add(btnMove);

        lblSelectExcelFile = new JLabel("Select CSV File");
        lblSelectExcelFile.setBounds(25, 23, 112, 23);
        frame.getContentPane().add(lblSelectExcelFile);

        lblMoveTo = new JLabel(" Move To.....");
        lblMoveTo.setBounds(25, 67, 68, 23);
        frame.getContentPane().add(lblMoveTo);

        progressBar = new JProgressBar(0, 100);
        progressBar.setBounds(70, 136, 296, 23);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
        progressBar.setForeground(Color.blue);
        progressBar.setBackground(Color.white);
        frame.getContentPane().add(progressBar);

        textField = new JTextField();
        textField.setBounds(144, 24, 163, 20);
        textField.setToolTipText("Select CSV File");
        textField.setEditable(false);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        btnBrowse = new JButton("Browse");
        Color cr = new Color(128, 171, 206);
        btnBrowse.setBackground(cr);
        btnBrowse.setBounds(303, 23, 92, 23);
        frame.getContentPane().add(btnBrowse);
        btnBrowse.addActionListener(listener);

        textField_1 = new JTextField();
        textField_1.setBounds(144, 68, 163, 20);
        textField_1.setToolTipText("Select Move To Location");
        textField_1.setEditable(false);
        frame.getContentPane().add(textField_1);
        textField_1.setColumns(10);

        btnLocation = new JButton("Location");
        btnLocation.setBackground(cr);
        btnLocation.setBounds(303, 67, 92, 23);
        frame.getContentPane().add(btnLocation);
        btnLocation.addActionListener(listener);

        lblA = new JLabel();
        lblA.setForeground(Color.RED);
        lblA.setBounds(144, 43, 180, 14);
        frame.getContentPane().add(lblA);

        lblB = new JLabel();
        lblB.setForeground(Color.RED);
        lblB.setBounds(144, 88, 180, 14);
        frame.getContentPane().add(lblB);

        lblSuccess = new JLabel();
        lblSuccess.setForeground(new Color(0, 51, 153));
        lblSuccess.setBounds(70, 121, 241, 14);
        frame.getContentPane().add(lblSuccess);

        Image icon = Toolkit.getDefaultToolkit().getImage("./img/move.png");
        frame.setIconImage(icon);
    }

    private class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == btnBrowse) {
                JFileChooser filechooser = new JFileChooser();
                filechooser.setDialogTitle("Select CSV File");
                filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "CSV Files", "csv");
                filechooser.setFileFilter(filter);
                filechooser.setAcceptAllFileFilterUsed(false);
                int rlValue = filechooser.showOpenDialog(null);
                if (rlValue == JFileChooser.APPROVE_OPTION) {
                    textField.setText(filechooser.getSelectedFile().toString());
                }
                lblA.setText(null);
            }

            else if (e.getSource() == btnLocation) {
                JFileChooser filechooser = new JFileChooser();
                filechooser.setDialogTitle("Select Location");
                filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                filechooser.setAcceptAllFileFilterUsed(false);
                int rlValue = filechooser.showOpenDialog(null);
                if (rlValue == JFileChooser.APPROVE_OPTION) {
                    textField_1.setText(filechooser.getSelectedFile()
                            .toString());
                }
                lblB.setText(null);
            } else if (e.getSource() == btnMove) {

                if (textField.getText().isEmpty()) {
                    lblA.setText("Please Select CSV File!");
                }

                else if (textField_1.getText().isEmpty()) {
                    lblB.setText("Please Select Location!");
                } else {
                    String csvFileLocation = textField.getText();
                    newpath = textField_1.getText();

                    MoveTools service = new MoveTools();
                    service.moveDir(csvFileLocation);
                    // service.moveDir(service.extract(csvFileLocation));

                    // Thread Execution
                    Work w = new Work();
                    w.start();
                }
            }
        }
    }

    // Read CSV File
    public List<String> extract(String fileName) throws IOException {
        List<String> paths = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(new File(
                fileName)))) {
            String line = null;
            String formattedPath = "";
            while ((line = br.readLine()) != null) {
                String s = line.split(",,,")[1];
                if (s.startsWith("Path")) {
                    String data = s.substring(s.indexOf("{") + 1,
                            s.indexOf("}"));
                    for (String splitData : data.split(";")) {
                        splitData = splitData
                                .substring(splitData.indexOf("\\") + 1,
                                        splitData.length());
                        formattedPath = splitData.replace("\\", "/");
                        paths.add(formattedPath);
                    }
                }
            }
        }
        System.out.println("Total No of path Found : " + paths.size());
        return paths;
    }

    // Logic To Move Directory

    public void moveDir(String fileName) {
        long timeIn = 0;
        timeIn = System.currentTimeMillis();
        boolean success = false;

        int count = 0;
        Path path = null;
        String actualPath = "";
        try {
            List<String> paths = extract(fileName);
            for (String s : paths) {
                count++;
                actualPath = ROOT_DIR + s;
                path = Paths.get(actualPath).toAbsolutePath();

                // System.out.println("path: " + path);

                // source file location

                String sPath = path.toString();
                // System.out.println("SPath: " + sPath);
                File sourceFile = new File(sPath);

                String destPath = newpath + s;
                // System.out.println("DestPath: " + destPath);
                File destFile = new File(destPath);

                String ParentDir = destFile.getParent();

                File destDir = new File(ParentDir);
                //FileUtils.moveFileToDirectory(sourceFile, destDir, true);
                FileUtils.copyFileToDirectory(sourceFile, destDir, true);
            }
        } catch (IOException e) {
            System.out.println("Error Message : " + e.getLocalizedMessage());

            // Create Log Details
            RandomAccessFile raf;
            try {
                raf = new RandomAccessFile("./FileNotFound.log", "rw");

                String msg = e.getMessage() + "; ";
                byte[] mgs1 = msg.getBytes();
                raf.seek(raf.length());
                raf.write(mgs1);
                raf.close();
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        long timeOut = System.currentTimeMillis();
        timeTaken = timeOut - timeIn;
        System.out.println("Time Taken: " + timeTaken);

        System.out.println("Total Directory created : " + count);
    }

    class Work extends Thread {
        public void run() {
            progressBar.setMaximum((int) MoveTools.timeTaken);

            while (i <= MoveTools.timeTaken) {
                progressBar.paintImmediately(0, 0, 200, 25);
                progressBar.setValue(i);
                i++;
            }
            lblSuccess.setText("File Moved Successfully in " + timeTaken
                    / 1000.0 + " Seconds!");
        }
    }
}

在这个程序中,我创建了进度条,但是在程序将所有文件从一个文件夹复制到另一个文件夹后启动。任何人都可以检查并给我一个适当的解决方案。

2 个答案:

答案 0 :(得分:2)

出于您的目的,流程是错误的,您应该输入以下代码:

                MoveTools service = new MoveTools();
                service.moveDir(csvFileLocation);

在你的线程的run()方法内。根据经验,需要长时间执行的工作(这是您需要显示进度的主要原因)应该放在线程的run()方法中。

在moveDir()工作中,它应该能够将进度报告给线程并让它相应地更新进度UI。例如,您可以添加一个侦听器接口,如:

public class MoveTools {
    public static interface Listener {
        void onProgress(String message, int currentProgress);
    }

    private Listener listener = null;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

在完成工作时,MoveTools可以向其监听器报告进度:

    if (listener != null) {
        listener.onProgress("Working...", 50);
    }

然后调用线程可以设置监听器并更新进度条(现在请注意,工作需要接受MoveTools作为参数,因为它需要将监听器附加到它以获知正在进行的进度):

    public Work(MoveTools moveTools) {
        moveTools.setListener(new MoveTools.Listener() {                
            @Override
            public void onProgress(String message, int currentProgress) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        progressBar.setString(message);
                        progressBar.setValue(currentProgress);
                    }
                });
            }
        });
    }

注意我使用SwingUtilities.invokeLater()更新了progressBar,因为你应该总是在事件调度线程(EDT)中修改GUI,而工作线程不是EDT。

答案 1 :(得分:1)

你有线程问题:

您的moveDir方法在 AWT 事件调度线程上运行,因此保留它。当它保持它时,不会执行任何屏幕更新(通过事件发送)。

此外,您的Work课程无法更新 GUI ,因为它正在另一个主题上运行。要访问 GUI ,您必须在 EDT 上发送活动。要做到这一点,有SwingUtilities.invokeLater()的便捷方法。这会为{strong> EDT 的任务队列添加Runnable

另见Java Tutorial on Concurency in Swing