user1368970
user1368970

Reputation: 555

Compiling Errors while creating a GUI in Java

My program opens a file, allows you to view or edit it, then save it again. So far, I only have the GUI layout.

So, when I try to compile my program, I get the following errors:

    FileChooser.java:26: error: <identifier> expected
         fileChooser = newJFileChooser();

I get that^^^ error four times, once for each component I initialized.

    FileChooser.java:39: error: invalid method declaration; return type required
         public FileChooserGUI() {

I get this^^^ error when I try to create my constructor.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;

public class FileChooser extends JFrame { //JFrame is a container class in Swing

//declare all components and other variables
private JTextArea textArea; //b
private JButton openFileButton; //c
private JButton saveFileButton;
private JButton clearButton;

//Declare and initialize a JFileChooser object using JFileChooser's
//default constructor.
private JFileChooser fileChooser;
fileChooser = newJFileChooser();

//Declare and initialize a FileNameExtensionFilter object for .txt files.
private FileFilter fileFilter;
fileFilter = new FileNameExtensionFilter("text file", "txt");

//Set the JFileChooser object's current file filter by calling it's
//setFileFilter method and passing it the filter you created in e.
fileChooser.setFileFilter(fileFilter);


//constructor - initialize all components, add them to the container, 
//create listener objects, register them to listen for events
public JFileChooserLabGUI() {

    //call superclass constructor
    super("File Chooser");


    //initialize all components
    textArea = new JTextArea();
    openFileButton = new JButton("Open");
    saveFileButton = new JButton("Save");
    clearButton = new JButton("Clear");

    //create and register the listener object with
    //sources of events (the JButtons in this example)
    listener = new JFileChooserListener();
    openFileButton.addActionListener(listener);
    saveFileButton.addActionListener(listener);
    clearButton.addActionListener(listener);


    //arrange components in the window
    //Create a layout manager object and set this window's
    //layout manager to it
    textPanel = new JPanel();
    textPanel.setLayout(new GridLayout(1,2));
    textPanel.add(textArea);

    buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(1,3,15,0));
    buttonPanel.add(openFileButton);
    buttonPanel.add(saveFileButton);
    buttonPanel.add(clearButton);
    this.setLayout(new GridLayout(3,1));



    //Add components to the container
    this.add(textPanel);
    this.add(buttonPanel);      


}//end of constructor
}//end of class

I don't have the listener class finished yet, but I wouldn't think that had anything to do with just displaying the GUI to make sure I have the layout correct?

I've noticed when I declare and initialize in the same line I don't get those four errors. Am I initializing something wrong?

Upvotes: 0

Views: 779

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1503539

You're trying to declare a constructor of type JFileChooserLabGUI but the class is declared as FileChooser:

public class FileChooser extends JFrame {
    public JFileChooserLabGUI() {
        ...
    }
}

EDIT: As noted, there are additional problems too. For example, you can't have a simple assignment statement outside a method/constructor:

fileChooser = newJFileChooser();

You'll want to put that in your constructor, or as part of the variable declaration.

... and that's presumably meant to be new JFileChooser(), rather than a call to a newJFileChooser() method. So for example, if you're making it part of the declaration:

private JFileChooser fileChooser = new JFileChooser();

Do the same for the FileFilter, and move this line:

fileChooser.setFileFilter(fileFilter);

into your constructor.

In general - and particularly if you're relatively new to a language - it's a really good idea to build your code frequently. That way you've got a better chance of understanding the errors as you find them, as you'll only have just written the incorrect code. Oh, and write unit tests as you go too.

Upvotes: 1

prashantsunkari
prashantsunkari

Reputation: 959

There are couple of things I think that needs to be corrected

  1. Constructor name is not same as class name.

  2. With respect variable assignment outside class methods:

    • You have to either declare and initialize the variable at same time.
    • or (better way) declare and initialize in constructor. Checkout Java Variable declaration
  3. Also, there is small syntactical error. There should be space between new and constructor name.

    public FileChooser () {
       //call superclass constructor
       super("File Chooser");
       fileChooser=fileChooser = new JFileChooser();
       fileFilter=new FileNameExtensionFilter("text file", "txt");
       fileChooser.setFileFilter(fileFilter);
    
      ...
       }
    

Hope this helps!

Upvotes: 1

Related Questions