Joining JProgressBar and JTextArea (batch running console) in Eclipse

Question

Hello I am facing few problem in combining JtextArea with JProgressBar . JtextArea is showing the console of batch running script. here is my code:-

private void initialize() {
        frmPdfPublisher = new JFrame();
        frmPdfPublisher.setIconImage(new ImageIcon("C:\\Users\\Admin\\Desktop\\imageedit_1_6449501097.png").getImage());
        frmPdfPublisher.setTitle("PDF Publisher");
        frmPdfPublisher.setBounds(100, 100, 483, 540);
        frmPdfPublisher.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmPdfPublisher.getContentPane().setLayout(null);


        TextArea textArea = new TextArea();
        textArea.setBounds(10, 280, 447, 146);
        frmPdfPublisher.getContentPane().add(textArea);
        textArea.setEditable(false);


        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setBounds(10, 280, 447, 146);
        frmPdfPublisher.getContentPane().add(scrollPane);


        JProgressBar progressBar = new JProgressBar();
        progressBar.setBounds(10, 254, 447, 14);
        frmPdfPublisher.getContentPane().add(progressBar);
        progressBar.setVisible(true);



        JButton btnTransform = new JButton("Transform");
        btnTransform.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                String cmds[] = {"C:\\Users/Admin/Desktop/test1.bat"};
                Runtime runtime = Runtime.getRuntime();
                Process process = runtime.exec(cmds);
                process.getOutputStream().close();
                java.io.InputStream inputStream = process.getInputStream() ;
                InputStreamReader inputstreamreader = new InputStreamReader(inputStream);
                BufferedReader bufferedrReader = new BufferedReader(inputstreamreader);
                String strLine = "";
                while ((strLine = bufferedrReader.readLine()) != null) {
                    System.out.println(strLine);
                    String newline = "\n";
                    textArea.append(strLine+newline);
                    textArea.setCaretPosition(textArea.SCROLLBARS_VERTICAL_ONLY);
                    progressBar.setString(strLine);



            }
            } catch (IOException IoException) {
                IoException.printStackTrace();
            }
            }

        });

I want to show the running script in JprogressBar as well. I am using Eclipse Neon.2

EDIT:-

I had added few steps , but it is not showing perfect loading and is getting completed as soon the console is stopped.

Here is the edited code:-

JButton btnTransform = new JButton("Transform");
        btnTransform.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                try {
                String cmds[] = {"C:\\Users/Admin/Desktop/test1.bat"};
                Runtime runtime = Runtime.getRuntime();
                Process process = runtime.exec(cmds);
                process.getOutputStream().close();
                java.io.InputStream inputStream = process.getInputStream() ;
                InputStreamReader inputstreamreader = new InputStreamReader(inputStream);
                BufferedReader bufferedrReader = new BufferedReader(inputstreamreader);
                String strLine = "";
                int n=progressBar.getValue();
                if(n<100){
                    n++;
                    progressBar.setValue(100);
                }
                else{
                    timer.cancel();
                }
                while ((strLine = bufferedrReader.readLine()) != null) {
                    System.out.println(strLine);
                    String newline = "\n";
                    textArea.append(strLine+newline);
                    textArea.setCaretPosition(textArea.SCROLLBARS_VERTICAL_ONLY);
                    progressBar.setStringPainted(true);
                    progressBar.dispatchEvent(e);   
                    progressBar.getValue();
                    progressBar.updateUI();

            }
            } catch (IOException IoException) {
                IoException.printStackTrace();
            }
            }

Kindly rectify me , what am I missing?


Show source
| java   | eclipse   | swing   | windowbuilder   | jprogressbar   2017-01-03 10:01 1 Answers

Answers to Joining JProgressBar and JTextArea (batch running console) in Eclipse ( 1 )

  1. 2017-01-03 12:01

    Right now, your exec() call is blocking the event dispatch thread. You'll need to run the commands in the background like they show here. If you want to see the the progress of each line in the batch file as it runs, you'll need to read the batch file line-by-line, execute each line separately and publish() the result like they show here. If you call setProgress() after each line in the background, a listening progress bar to get updated, like they show here.

    I want my progress bar to work according to the batch file run time execution process and n completion 100%.

    You'll need to read all the lines to know how many there are. Then you can set the progress like they show here:

    setProgress(i * (100 / n));
    

    where i is the current line number being executed and n is the total number of lines. For example, if there's ten lines, the progress bar will grow by one tenth after each call.

Leave a reply to - Joining JProgressBar and JTextArea (batch running console) in Eclipse

◀ Go back