Note that there are some explanatory texts on larger screens.

plurals
  1. POCompiling Errors while creating a GUI in Java
    primarykey
    data
    text
    <p>My program opens a file, allows you to view or edit it, then save it again. So far, I only have the GUI layout.</p> <p>So, when I try to compile my program, I get the following errors:</p> <pre><code> FileChooser.java:26: error: &lt;identifier&gt; expected fileChooser = newJFileChooser(); </code></pre> <p>I get that^^^ error four times, once for each component I initialized. </p> <pre><code> FileChooser.java:39: error: invalid method declaration; return type required public FileChooserGUI() { </code></pre> <p>I get this^^^ error when I try to create my constructor.</p> <pre><code>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 </code></pre> <p>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?</p> <p>I've noticed when I declare and initialize in the same line I don't get those four errors. Am I initializing something wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload