0

As part of a bigger project I am trying to make a thread that creates a new Jframa at Java swing and then execute a file copy from one folder to another. To do this I create a thread and at its constructor I build the GUI and from them main thread I start the execution of the thread. My problem is that the new JFrame is blank until the copy is finished and the it shows the labels,textboxes,etc. I cannot understand if I do something wrong so a little head would be appreciated

The frame class:

public class FileCopyFrame extends JFrame{
    private void BuildGUI(final String sourcePath,final String targetPath) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 665, 382);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        progressBar = new JProgressBar();
        progressBar.setBounds(10, 268, 631, 14);
        contentPane.add(progressBar);

        progressBar_all = new JProgressBar();
        progressBar_all.setBounds(10, 293, 631, 14);
        contentPane.add(progressBar_all);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 88, 631, 166);
        contentPane.add(scrollPane);

        textAreaAnnouncements = new JTextArea();
        scrollPane.setViewportView(textAreaAnnouncements);

        btnCancel = new JButton("Cancel");
        btnCancel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {

            }
        });
        btnCancel.setBounds(267, 318, 89, 23);
        contentPane.add(btnCancel);

        final JLabel lblSourcePath = new JLabel("Source Path:");
        lblSourcePath.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblSourcePath.setBounds(10, 35, 78, 14);
        contentPane.add(lblSourcePath);

        final JLabel lblTargetPath = new JLabel("Target Path:");
        lblTargetPath.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblTargetPath.setBounds(10, 60, 78, 17);
        contentPane.add(lblTargetPath);

        textFieldSrcPth = new JTextField();
        textFieldSrcPth.setBounds(96, 32, 545, 20);
        contentPane.add(textFieldSrcPth);
        textFieldSrcPth.setText(sourcePath);
        textFieldSrcPth.setColumns(10);

        textFieldTrgPth = new JTextField();
        textFieldTrgPth.setBounds(96, 57, 545, 20);
        contentPane.add(textFieldTrgPth);
        textFieldTrgPth.setText(targetPath);
        textFieldTrgPth.setColumns(10);

    }

    public FileCopyFrame(String source,String path) {
           BuildGUI(source,path);
    }
}

the thread class:

public class CopyThread extends Thread {
FileCopyFrame frame;
public CopyThread(ThreadOptions options){
        frame=new FileCopyFrame(destinationPath, destinationPath);
        frame.setVisible(true);
        current=frame.getProgressBarCurrent();
        All=frame.getProgressBarCurrentAll();
        this.textPane = frame.getTextAreaAnnouncements();
        setOptions(options);


    }


         public void run() {    
        try {
            Copy();     
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

At main thread:

CopyThread thread=new CopyThread(options);
thread.run();
JmRag
  • 1,443
  • 7
  • 19
  • 56
  • See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [How to use Swing Worker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) in particular. Don't use null layouts, don't use MouseListener on buttons, use an ActionListener. Don't use Thread.run, use Thread.start – MadProgrammer May 21 '14 at 20:26
  • @MadProgrammer I've already read these before posting here. I prefer to use thread rather than Swing Workers cause only one SwingWorker can run at time, while I want multiple threads concurrently. – JmRag May 21 '14 at 20:29
  • @JmRag, you can run multiple threads from the SwingWorker. – Abu Sulaiman May 21 '14 at 20:33
  • Nope, you can actually run up to 10 SwingWorkers concurrently, that's the point. You are violating the single threaded nature of Swing by doing what you are right now, hence the reason for suggesting a SwibgWorker – MadProgrammer May 21 '14 at 20:33
  • For a nice complex [example](http://stackoverflow.com/questions/13753562/adding-progress-bar-to-each-table-cell-for-file-progress-java/13755155#13755155) – MadProgrammer May 21 '14 at 20:38
  • Stop putting tags in the question title. – Lee Fogg May 21 '14 at 20:44

1 Answers1

2

thread.run();

This does NOT start a new Thread. This just executes the run() method. Since you are probably invoking this code from the Event Dispatch Thread (EDT), the code to create the GUI and do the copy is also executing on the EDT which means the GUI can't repaint itself until the copy finishes.

To create a new Thread you should be doing:

thread.start();
camickr
  • 321,443
  • 19
  • 166
  • 288