Advertisement
mahmoodn

please-wait-dialog

Apr 21st, 2017
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.08 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. /**
  8.  *
  9.  * @author ThinkPad
  10.  */
  11. import javax.swing.JFileChooser;
  12. import javax.swing.filechooser.FileNameExtensionFilter;
  13. import javax.swing.SwingWorker;
  14. import javax.swing.JDialog;
  15. import javax.swing.JPanel;
  16. import javax.swing.JLabel;
  17. import java.awt.BorderLayout;
  18. import java.io.File;  
  19.  
  20. public class TheFrame extends javax.swing.JFrame {
  21.  
  22.     /**
  23.      * Creates new form TheFrame
  24.      */
  25.     public TheFrame() {
  26.         initComponents();
  27.     }
  28.  
  29.     /**
  30.      * This method is called from within the constructor to initialize the form.
  31.      * WARNING: Do NOT modify this code. The content of this method is always
  32.      * regenerated by the Form Editor.
  33.      */
  34.     @SuppressWarnings("unchecked")
  35.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  36.     private void initComponents() {
  37.  
  38.         OpenSongFile = new javax.swing.JButton();
  39.         jScrollPane1 = new javax.swing.JScrollPane();
  40.         ReadInfo = new javax.swing.JTextArea();
  41.  
  42.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  43.  
  44.         OpenSongFile.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
  45.         OpenSongFile.setText("Open Song File");
  46.         OpenSongFile.addActionListener(new java.awt.event.ActionListener() {
  47.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  48.                 OpenSongFileActionPerformed(evt);
  49.             }
  50.         });
  51.  
  52.         ReadInfo.setColumns(20);
  53.         ReadInfo.setRows(5);
  54.         ReadInfo.setToolTipText("");
  55.         jScrollPane1.setViewportView(ReadInfo);
  56.  
  57.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  58.         getContentPane().setLayout(layout);
  59.         layout.setHorizontalGroup(
  60.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  61.             .addGroup(layout.createSequentialGroup()
  62.                 .addGap(39, 39, 39)
  63.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  64.                     .addComponent(jScrollPane1)
  65.                     .addComponent(OpenSongFile, javax.swing.GroupLayout.DEFAULT_SIZE, 584, Short.MAX_VALUE))
  66.                 .addContainerGap(124, Short.MAX_VALUE))
  67.         );
  68.         layout.setVerticalGroup(
  69.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  70.             .addGroup(layout.createSequentialGroup()
  71.                 .addGap(34, 34, 34)
  72.                 .addComponent(OpenSongFile, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
  73.                 .addGap(18, 18, 18)
  74.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
  75.                 .addContainerGap(67, Short.MAX_VALUE))
  76.         );
  77.  
  78.         pack();
  79.     }// </editor-fold>                        
  80.  
  81.     private void OpenSongFileActionPerformed(java.awt.event.ActionEvent evt) {                                              
  82.         // TODO add your handling code here:
  83.         JFileChooser fileChooser = new JFileChooser();
  84.         fileChooser.setAcceptAllFileFilterUsed(false);
  85.         FileNameExtensionFilter filter = new FileNameExtensionFilter("MP3 files", "mp3");
  86.         fileChooser.addChoosableFileFilter(filter);
  87.         fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
  88.         int result = fileChooser.showOpenDialog(this);
  89.         if (result != JFileChooser.APPROVE_OPTION) {
  90.             //ReadInfo.setText("No song file has been selected");
  91.             System.out.println("OpenSongFile canceled by user");
  92.             return;
  93.         }
  94.         final JDialog loading = new JDialog(this);
  95.         JPanel p1 = new JPanel(new BorderLayout());
  96.         p1.add(new JLabel("Please wait..."), BorderLayout.CENTER);
  97.         loading.setUndecorated(true);
  98.         loading.getContentPane().add(p1);
  99.         loading.pack();
  100.         loading.setLocationRelativeTo(this);
  101.         loading.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
  102.         loading.setModal(true);
  103.         SwingWorker<String, Void> worker = new SwingWorker<String, Void>() {
  104.             @Override
  105.             protected String doInBackground() throws InterruptedException {
  106.                 for (int i = 0; i < 10000; i++)
  107.                     for (int j = 0; j < 10000; j++)
  108.                         ;
  109.                 return "hello";
  110.             }
  111.             @Override
  112.             protected void done() {
  113.                 loading.dispose();
  114.             }
  115.         };
  116.         worker.execute();
  117.         loading.setVisible(true);
  118.         try {
  119.             worker.get();
  120.         } catch (Exception e1) {
  121.             e1.printStackTrace();
  122.         }
  123.  
  124.         File selectedFile = fileChooser.getSelectedFile();
  125.         ReadInfo.setText("Selected file: " + selectedFile.getAbsolutePath());
  126.         //System.out.println();
  127.  
  128.  
  129.     }                                            
  130.  
  131.     /**
  132.      * @param args the command line arguments
  133.      */
  134.     public static void main(String args[]) {
  135.         /* Set the Nimbus look and feel */
  136.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  137.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  138.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  139.          */
  140.         try {
  141.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  142.                 if ("Nimbus".equals(info.getName())) {
  143.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  144.                     break;
  145.                 }
  146.             }
  147.         } catch (ClassNotFoundException ex) {
  148.             java.util.logging.Logger.getLogger(TheFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  149.         } catch (InstantiationException ex) {
  150.             java.util.logging.Logger.getLogger(TheFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  151.         } catch (IllegalAccessException ex) {
  152.             java.util.logging.Logger.getLogger(TheFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  153.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  154.             java.util.logging.Logger.getLogger(TheFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  155.         }
  156.         //</editor-fold>
  157.  
  158.         /* Create and display the form */
  159.         java.awt.EventQueue.invokeLater(new Runnable() {
  160.             public void run() {
  161.                 new TheFrame().setVisible(true);
  162.             }
  163.         });
  164.     }
  165.  
  166.     // Variables declaration - do not modify                    
  167.     private javax.swing.JButton OpenSongFile;
  168.     private javax.swing.JTextArea ReadInfo;
  169.     private javax.swing.JScrollPane jScrollPane1;
  170.     // End of variables declaration                  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement