CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2007
    Posts
    18

    JFileChooser & EDT

    Hey again,

    My project consists of one Applet and an MP3Player class.
    I'm using a JFileChooser to play an mp3 song. The problem is that in my
    applet I have 2 buttons: start + stop buttons ... I must add an
    ActionListener to those buttons, but it is not working with inner classes.
    Any other way to make it ?

    The start button calls the function "play()" located in the MP3Player class
    The stop button calls the function "stop()" located in the MP3Player class

    Code:
      boolean openFile(){
    
       JFileChooser fc = new JFileChooser ();
       fc.setDialogTitle ("Open File");
       fc.setFileSelectionMode ( JFileChooser.FILES_ONLY);
       fc.setCurrentDirectory (new File ("."));
       fc.setFileFilter(fMp3Filter);
       int result = fc.showOpenDialog (this);
       if (result == JFileChooser.CANCEL_OPTION) {
       return true;
       }
       else
       if (result == JFileChooser.APPROVE_OPTION) {
       fFile = fc.getSelectedFile ();
       nowplaying.setText(fFile.getName());
       MP3Player mp3=new MP3Player(fc.getSelectedFile().getAbsolutePath());
    
       start.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent evt) {
       if(evt.getSource() == start)
       mp3.play();
       }});
    
       stop.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent evt) {
       if(evt.getSource() == stop)
       mp3.stop();
       }});
    
     ....
    Error given : "MediaPlayer.java": Error #: 480 : local variable mp3 is accessed from within inner class; needs to be declared final at line 95, column 4
    Last edited by tima; February 18th, 2007 at 07:05 AM.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: JFileChooser & ActionListener

    Error given : "MediaPlayer.java": Error #: 480 : local variable mp3 is accessed from within inner class; needs to be declared final at line 95, column 4
    The problem is that the variable mp3 is a local variable and so is only in existence whilst the method by being executed. Your inner classes can be run at any time so they can't reference the mp3 variable as it may not exist when the inner class is executed. One way to solve this problem is by doing as the error message suggests - add the final modifier to the variable declaration: eg
    Code:
    final MP3Player mp3=new MP3Player(fc.getSelectedFile().getAbsolutePath());

  3. #3
    Join Date
    Jan 2007
    Posts
    18

    Re: JFileChooser & ActionListener

    Yea I got it ! Thx for ur explanation keang .. It worked well
    Most appreciated

  4. #4
    Join Date
    Jan 2007
    Posts
    18

    Re: JFileChooser & ActionListener

    How am I supposed to use the Thread Event Dispatch Event to call
    from an Applet the function play() located in the MP3Player class ?
    It is giving me hard times to do it especially that I'm getting the songs from a JFileChooser ....


    Code:
      boolean openFile(){
    
     [ ... ] 
    // JFileChooser - open file
    
    if (result == JFileChooser.APPROVE_OPTION) {
       fFile = fc.getSelectedFile ();
       nowplaying.setText(fFile.getName());
    
     
       final String c=fc.getSelectedFile().getAbsolutePath();
    
       // .. Thread EDT .. 
       Runnable code = new Runnable() {
       final MP3Player mp3=new MP3Player(c);
        public void run() {
          mp3.play();
        }
      };
      if (SwingUtilities.isEventDispatchThread()) {
        code.run();
      } else {
        SwingUtilities.invokeLater(code);
      } 
    Thx in advance for ur appreciated help

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: JFileChooser & ActionListener

    How am I supposed to use the Thread Event Dispatch Event to call
    from an Applet the function play() located in the MP3Player class ?It is giving me hard times to do it especially that I'm getting the songs from a JFileChooser
    Firstly are you really sure you want to use the Event Dispatch Thread to do this and secondly unless you are specific about what the problem is how do you expect us to offer you to right advice.

    With regard to the event dispatch thread, if you use it to create and play the MP3Player your GUI will effectively lock up until play completes, unless of course the MP3Player creates its own background thread to play the song.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured