-1

Here I have written the following code. I can't run the program, and the error mentioned below keeps appearing. I tried many probable solutions but in vain.

import java.beans.EventHandler;
import java.io.File;
import javafx.application.Application;
import javafx.collections.*;
import javafx.event.ActionEvent;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.canvas.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.scene.text.*;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class FileChooser_1 extends Application {

// launch the application
    public void start(Stage stage) {

        try {

            // title
            stage.setTitle("Filechooser");

            //File chooser create
            FileChooser file_chooser = new FileChooser();

            // define Label
            Label lab = new Label("select file");

            // Button new
            Button b = new Button("open dialog");

            // create Event Handler
            EventHandler<ActionEvent> eve
                = new EventHandler<ActionEvent>() {

                public void handle(ActionEvent e) {

                    // get file
                    File file = file_chooser.showOpenDialog(stage);

                    if (file != null) {

                        lab.setText(file.getAbsolutePath()
                            + " selected");
                    }
                }
            };

            b.setOnAction(event);

            // create Button
            Button b1 = new Button("save");

            // Event Handler
            EventHandler<ActionEvent> eve1
                = new EventHandler<ActionEvent>() {

                public void handle(ActionEvent e) {

                    // get file
                    File file = file_chooser.showSaveDialog(stage);

                    if (file != null) {
                        lab.setText(file.getAbsolutePath()
                            + " selected");
                    }
                }
            };

            b1.setOnAction(eve1);

            // VBox
            VBox vbox = new VBox(30, label, button, button1);

            // set Alignment
            vbox.setAlignment(Pos.CENTER);

            // create scene
            Scene scene = new Scene(vbox, 800, 500);

            // scene
            stage.setScene(scene);

            stage.show();
        } catch (Exception e) {

            System.out.println(e.getMessage());
        }
    }

// Main Method
    public static void main(String args[]) {
        launch(args);
    }
}

I am getting the following error:

Error: Unable to initialize main class FileChooser_1
Caused by: java.lang.NoClassDefFoundError: Stage

It will be really nice if you can help me with this.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
shaishav shah
  • 23
  • 1
  • 7
  • 1
    `java.beans.EventHandler`should be [`javafx.event.EventHandler`](https://openjfx.io/javadoc/17/javafx.base/javafx/event/EventHandler.html). – jewelsea Feb 27 '22 at 19:24
  • 1
    Only import classes you use. When logging errors, log the full stack trace `exception.printStackTrace()`. Add the full stack trace to your question formatted as code. – jewelsea Feb 27 '22 at 19:28
  • Edit the question, add the command used to run the program, formatted as code. – jewelsea Feb 27 '22 at 19:31
  • Can you explain how you use the r.Java-file which you have tagged your question with? That is for Android development, not JavaFX. – jewelsea Feb 27 '22 at 19:36

1 Answers1

4

With some attention to detail, your code works. In particular, especially when just starting out,

  • Use Java naming conventions.

  • Use meaningful names; for example, instead of Button b, try Button openButton.

  • When using detailed comments, keep them up to date; note how meaningful names make some comments superfluous.

  • Use constants for consistency.

  • As @jewelsea notes, your program imports java.beans.EventHandler; it should import javafx.event.EventHandler.

  • As @jewelsea notes, "Only import classes you use."

  • Let the layout do the work.

  • I can't explain the error in your question; I see errors related to the incorrect import for EventHandler. If you're using an IDE, it may be reporting errors from a different compilation unit. When in doubt, do a clean build, move the code to a new file, or move to a different development environment, e.g. the command line. As a concrete example, this simple VersionCheck illustrates both a minimal ant script, invoked as ant run, and a simple shell script, invoked as .run.sh:

      #!/bin/sh
      JFX="--module-path /Users/Shared/javafx-sdk-17.0.1/lib --add-modules ALL-MODULE-PATH"
      javac $JFX *.java && java $JFX VersionCheck
    

image

import javafx.event.EventHandler;
import java.io.File;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class FileChooser1 extends Application {

    private static final int PADDING = 32;

    @Override
    public void start(Stage stage) {
        // title
        stage.setTitle("FileChooser");
        //File chooser create
        FileChooser fileChooser = new FileChooser();
        // define Label
        Label label = new Label("Select a file to open or save:");
        // open Button
        Button openButton = new Button("Open");
        // open Event Handler
        EventHandler<ActionEvent> openHandler = new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                // get file name
                File file = fileChooser.showOpenDialog(stage);
                if (file != null) {
                    label.setText(file.getName() + " selected");
                }
            }
        };
        openButton.setOnAction(openHandler);
        // create save button
        Button saveButton = new Button("Save");
        // save Event Handler
        EventHandler<ActionEvent> saveHandler = new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                // save file
                File file = fileChooser.showSaveDialog(stage);
                if (file != null) {
                    label.setText(file.getName() + " selected");
                }
            }
        };
        saveButton.setOnAction(saveHandler);
        // VBox
        VBox vBox = new VBox(PADDING, label, openButton, saveButton);
        // set Alignment
        vBox.setAlignment(Pos.CENTER);
        vBox.setPadding(new Insets(PADDING));
        // create scene
        Scene scene = new Scene(vBox);
        // scene
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String args[]) {
        launch(args);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045