Results 1 to 5 of 5
- 01-18-2011, 01:27 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
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.
-
You forgot to tell us something: what is the problem you are having with this code?
- 01-19-2011, 11:01 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
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...
- 01-19-2011, 11:12 AM #4
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
- 01-22-2011, 02:55 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Problem about JLabel not update in JPanel
By fantasyme in forum Java AppletsReplies: 11Last Post: 10-16-2011, 07:00 AM -
update automatically an xml file
By freddy000 in forum New To JavaReplies: 2Last Post: 12-30-2010, 02:26 PM -
Jlabel update problem
By fantasyme in forum AWT / SwingReplies: 3Last Post: 04-14-2010, 05:10 AM -
Adding a JLabel to a JPanel - jlabel not showing
By Bongeh in forum New To JavaReplies: 17Last Post: 04-06-2010, 11:02 PM -
Automatically set the path
By mlibot in forum New To JavaReplies: 9Last Post: 01-26-2010, 08:18 AM


LinkBack URL
About LinkBacks

Bookmarks