Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

<indenifier> expected error

Posted on 2004-04-11
7
Medium Priority
?
273 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 200 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 300 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Accepted Solution

by:
Thunder_scream earned 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

927 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