Solved

<indenifier> expected error

Posted on 2004-04-11
7
250 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 2

Accepted Solution

by:
Thunder_scream earned 75 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thunder_scream you are right boy that was right in the face.
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now