Solved

<indenifier> expected error

Posted on 2004-04-11
7
251 Views
Last Modified: 2010-03-31
<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);
 
}
0
Comment
Question by:Drop_of_Rain
7 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 50 total points
ID: 10802805
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
 
LVL 3

Assisted Solution

by:monkesdb
monkesdb earned 75 total points
ID: 10802820
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
 

Author Comment

by:Drop_of_Rain
ID: 10802833
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 2

Accepted Solution

by:
Thunder_scream earned 75 total points
ID: 10802837
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
 

Author Comment

by:Drop_of_Rain
ID: 10802843
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
 

Author Comment

by:Drop_of_Rain
ID: 10802851
Thunder_scream you are right boy that was right in the face.
0
 

Author Comment

by:Drop_of_Rain
ID: 10802858
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

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error trying to install RTMT Win7 5 60
check java version using powershell 13 93
by zero exception 10 41
jboss wildfly 10.1 10 94
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question