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();