In Netbeans, I created a GUI project which uses the frame frontend that I can add components and double click to edit their events. I am referring to the window which has "Source", "Design" and "History" tabs.
Here are the components and relations:
1- A Button which opens a file chooser.
2- a TextArea to show the result of the file chooser. If user selects a file, it will show the file name in the TextArea; otherwise it will write "canceled by user".
3- Meanwhile, if user selects a file, I want to open a "please wait" dialog witch SwingWorker
and do some works in the background.
Problem is that, when the user selects a file, I don't see the please wait dialog!! The full code generated by Netbeans is available at pastebin. Part of the code is shown here:
private void OpenSongFileActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser fileChooser = new JFileChooser();
fileChooser.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter("MP3 files", "mp3");
fileChooser.addChoosableFileFilter(filter);
fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
int result = fileChooser.showOpenDialog(this);
if (result != JFileChooser.APPROVE_OPTION) {
//ReadInfo.setText("No song has been selected");
System.out.println("OpenSongFile canceled by user");
return;
}
final JDialog loading = new JDialog(this);
JPanel p1 = new JPanel(new BorderLayout());
p1.add(new JLabel("Please wait..."), BorderLayout.CENTER);
loading.setUndecorated(true);
loading.getContentPane().add(p1);
loading.pack();
loading.setLocationRelativeTo(this);
loading.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
loading.setModal(true);
SwingWorker<String, Void> worker = new SwingWorker<String, Void>() {
@Override
protected String doInBackground() throws InterruptedException {
for (int i = 0; i < 10000; i++)
for (int j = 0; j < 10000; j++)
;
return "hello";
}
@Override
protected void done() {
loading.dispose();
}
};
worker.execute();
loading.setVisible(true);
try {
worker.get();
} catch (Exception e1) {
e1.printStackTrace();
}
File selectedFile = fileChooser.getSelectedFile();
ReadInfo.setText("Selected file: " + selectedFile.getAbsolutePath());
}
P.S: I used the SwingWorker code explained here.