<indenifier> expected error

<indenifier> expected error comes up for this line of code below:

 jf.getContentPane().add(new TrainerFileChooser(jf));




import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

/** A simple demo of a JFileChooser in action. */
public class TrainerFileChooser extends JPanel {

    /** Constructor */
    public TrainerFileChooser() {
        final JFrame f;
        final JFileChooser fc = new JFileChooser();
        JFrame jf = new JFrame("Trainer FileChooser");
        AudioAccessory aa = new AudioAccessory();
        fc.setAccessory(aa);
        fc.setFileFilter(new javax.swing.filechooser.FileFilter () {
            public boolean accept(File f) {
                if (f.isDirectory()) {
                return true;
                }
                String name = f.getName();
                if (name.endsWith(".wav")) {
                return true;
                }
                return false;
                }
                public String getDescription() {
                return ".wav";
                }
            });
       
       
       
        int returnVal = fc.showOpenDialog(f);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();                
        }
    }

   
        JFrame jf = new JFrame("Trainer FileChooser");
        jf.getContentPane().add(new TrainerFileChooser(jf));
        jf.pack();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
 
}
Drop_of_RainAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Thunder_screamConnect With a Mentor Commented:
Hi Drop_of_Rain,

if I'm seeing right..might be because

>  public TrainerFileChooser() {   >>>you are not defining anything here..as well

  jf.getContentPane().add(new TrainerFileChooser(jf));  <<you cant have jf unless u have e.g
  public TrainerFileChooser(JFrame frame)

Cheers!
0
 
objectsConnect With a Mentor Commented:
that code needs to be in a method. it looks like it is meant to be in main():

public static void main(String[] args)
{
        JFrame jf = new JFrame("Trainer FileChooser");
        jf.getContentPane().add(new TrainerFileChooser(jf));
        jf.pack();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
}
0
 
monkesdbConnect With a Mentor Commented:
Hi Drop_of_Rain,
you must place code in a method. yuou could put it in a static block, but i'm not sure that what you want!

        JFrame jf = new JFrame("Trainer FileChooser");
static {
        jf.getContentPane().add(new TrainerFileChooser(jf));
        jf.pack();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
}

Cheers!
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Drop_of_RainAuthor Commented:
I think I was suppose to use this one not the other one, does this look right? This one was to replace the other one, this one uses showOpenDialog so I didn't need a frame.


import java.io.*;
import javax.swing.*;
public class LoadFile {

  File file;

  public LoadFile() {

  }
  public File load() {

   try {
    file = new File(System.getProperty("user.dir"));
    JFileChooser fc = new JFileChooser(file);
    AudioAccessory aa = new AudioAccessory();
    fc.setAccessory(aa);
    fc.setFileFilter(new javax.swing.filechooser.FileFilter () {
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }
            String name = f.getName();
            if (name.endsWith(".wav")) {
                return true;
            }
            return false;
        }
        public String getDescription() {
            return ".wav";
        }
      });

      if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
          // createAudioInputStream(fc.getSelectedFile(), true);
          file = fc.getSelectedFile();

      }
    } catch (SecurityException ex) {
        //JavaSound.showInfoDialog();
        ex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    System.out.println(file.getName());
    return file;
  }
}
0
 
Drop_of_RainAuthor Commented:
monkesdb yes I think you are right, I do have a note about this. I then abandoned this one but got mixed up again and forgot.

0
 
Drop_of_RainAuthor Commented:
Thunder_scream you are right boy that was right in the face.
0
 
Drop_of_RainAuthor Commented:
objects, it was a main at the start. I am so new to this I am always making little errors.

Thanks everyone for all the support.
Christopher
0
All Courses

From novice to tech pro — start learning today.