Solved

<indenifier> expected error

Posted on 2004-04-11
7
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Industry Leaders: 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!

Question has a verified solution.

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

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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

634 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