Results 1 to 5 of 5
  1. #1
    carnado2008 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default Update JLabel during loop automatically

    Hello hi...i have a problem and i hope someone in this forums will help me to solve my problem...before i explain in more detail about my problem i will give some briefing about my project and how i suddenly face the problem....i have created a simple mp3 to wav converter, and during my program convert a file, it should show to the user current converting file's name...my program give it's user convert multiple files...and what i want is every time the program start to convert a new file in multiple files selected, it will update JLabel to show what current converting file...the complete code like below. I hope someone will show me some light :

    Java Code:
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.filechooser.FileFilter;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    
    import java.awt.*;
    import java.awt.image.*;
    
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    import java.io.File;
    import javax.swing.JLabel;
    
    import javazoom.jl.converter.Converter;
    
    public class ConvertMp3ToWav extends JFrame implements ActionListener,Runnable
    {
        JButton mainButton=new JButton ("Click Here To Select MP3 Files To Convert");
        JTextArea myLabel=new JTextArea("\n\tAll converted file has name like original \n\tfile with added .wav at the end and located in the same \n\tdirectory with original mp3 file."
                + "\n\n\tYou can convert multiple files in a time.");
        JMenu menuHelp=new JMenu("Help");
        JMenuItem menuDlm=new JMenuItem("About");
        JMenuBar menuBar=new JMenuBar();
        JLabel informPanel=new JLabel("STATUS : No File To Convert",SwingConstants.CENTER);
        JPanel testingPanel=new JPanel();
        
        File selectedFile;
    
        public ConvertMp3ToWav()
        {
            super("Convert MP3 To Wav");
            setLayout(new BorderLayout());
    
            menuHelp.add(menuDlm);
            menuBar.add(menuHelp);
            mainButton.addActionListener(this);
            myLabel.setEditable(false);
            myLabel.setSize(300, 400);
            testingPanel.add(informPanel);
            add(testingPanel,BorderLayout.SOUTH);
            add(myLabel,BorderLayout.CENTER);
            add(mainButton,BorderLayout.NORTH);
    
            setJMenuBar(menuBar);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setResizable(false);
            setSize(500,200);
            setLocationRelativeTo(null);
            setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource()==mainButton)
            {
                JFileChooser jfc=new JFileChooser();
                jfc.setMultiSelectionEnabled(true);
                jfc.setFileFilter(new TypeOfFile());
                jfc.setDialogTitle("Select File To Convert");
                int a=jfc.showDialog(this, "Convert");
                if(a==JFileChooser.APPROVE_OPTION)
                {
                    File [] groupOfFiles=jfc.getSelectedFiles();
                    int numberOfFiles=groupOfFiles.length;
                    for(int i=0;i<numberOfFiles;i++)
                    {
                        selectedFile=groupOfFiles[i];
                        if(selectedFile.getName().toLowerCase().endsWith(".mp3"))
                        {
                            Thread anis=new Thread(this);
                            anis.start();
                            try
                            {
                                Converter myConverter = new Converter();
                                myConverter.convert(selectedFile.getPath(), selectedFile.getPath()+".wav");
                            }
                            catch(Exception error)
                            {
                                JOptionPane.showMessageDialog(null, "Converting Process Fail", "ERROR", JOptionPane.ERROR_MESSAGE);
                            }
                        }
                        else
                        {
                            JOptionPane.showMessageDialog(null, selectedFile.getName()+" not a MP3 File", "ERROR", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                    JOptionPane.showMessageDialog(null, "CONVERTING PROCESS COMPLETE", "ERROR", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    
        public void updateGui()
        {
            informPanel.setText("Please Wait : "+selectedFile.getName()+" still in converting...");
            testingPanel.revalidate();
            validate();
        }
    
        public void run()
        {
            informPanel.setText("Please Wait : "+selectedFile.getName()+" still in converting...");
        }
    
        //Class TypeOfFile
        class TypeOfFile extends FileFilter
        {
            //Type of file that should be display in JFileChooser will be set here
            //We choose to display only directory and mp3 file
            public boolean accept(File f)
            {
                return f.getName().toLowerCase().endsWith(".mp3")||f.isDirectory();
            }
    
            //Set description for the type of file that should be display
            public String getDescription()
            {
                return ".mp3 files";
            }
        }
    
        public static void main(String[]args)
        {
            ConvertMp3ToWav myObject=new ConvertMp3ToWav();
        }
    }
    Last edited by Fubarable; 01-18-2011 at 05:21 PM. Reason: mod edit: code tags added, advert removed.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    carnado2008 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Ok...like you can see in run() method which i created to automatic update a JLabel with new file name get from selectedFile.getName() in informPanel.setText("Please Wait : "+selectedFile.getName()+" still in converting..."); (with informPanel as it's object name) everytime this program loop in for(int i=0;i<numberOfFiles;i++) and start a Thread object with anis as it's object name at anis.start();...the problem is why the JLabel don't update with the new file name...but it will wait the program end and just update with the last file name in the above looping ? i'm sorry if you don't understand what i try to tell you...

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    You're blocking the EDT. Or updating the JLabel off of the EDT. Or something.

    Recommended reading:
    Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

  5. #5
    carnado2008 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks...you just teach something new...thank you very much...

Similar Threads

  1. Problem about JLabel not update in JPanel
    By fantasyme in forum Java Applets
    Replies: 11
    Last Post: 10-16-2011, 07:00 AM
  2. update automatically an xml file
    By freddy000 in forum New To Java
    Replies: 2
    Last Post: 12-30-2010, 02:26 PM
  3. Jlabel update problem
    By fantasyme in forum AWT / Swing
    Replies: 3
    Last Post: 04-14-2010, 05:10 AM
  4. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  5. Automatically set the path
    By mlibot in forum New To Java
    Replies: 9
    Last Post: 01-26-2010, 08:18 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •