1

i am learning how to create a dialog box in javafx and i wrote a code which is giving me a problem. the error is in the createLoginDialog method.

Error reads "No enclosing instance of type TryDialogBox is accessible. Must qualify the allocation with an enclosing instance of type TryDialogBox (e.g. x.new A() where x is an instance of TryDialogBox)."

public class TryDialogBox extends Application {
    static Stage LOGIN_DIALOG;
    static int dx = 1;
    static int dy = 1;

    private static Stage createLoginDialog(Stage parent, boolean modal) {
        if (LOGIN_DIALOG != null) {
            LOGIN_DIALOG.close();
        }
        return new MyDialog(parent, modal, "welcom");
    }

    public void start(final Stage stage) {
        stage.setTitle("developing dialog");
        Group root = new Group();
        Scene scene = new Scene(root, 433, 312, Color.WHEAT);

        MenuBar menuBar = new MenuBar();
        menuBar.prefWidthProperty().bind(stage.widthProperty());

        Menu menu = new Menu("home");

        // add
        MenuItem newItem = new MenuItem("change password", null);
        newItem.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                if (LOGIN_DIALOG == null) {
                    LOGIN_DIALOG = createLoginDialog(stage, true);
                }
                LOGIN_DIALOG.sizeToScene();
                LOGIN_DIALOG.show();
            }
        });

        menu.getItems().add(newItem);

        // add separator
        menu.getItems().add(new SeparatorMenuItem());

        // add non mdal menu item
        ToggleGroup modalGroup = new ToggleGroup();
        RadioMenuItem nonModalItem = RadioMenuItemBuilder.create()
                .toggleGroup(modalGroup).text("non modal").selected(true)
                .build();
        nonModalItem.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                LOGIN_DIALOG = createLoginDialog(stage, false);
            }
        });
        menu.getItems().add(nonModalItem);
        // add modal
        RadioMenuItem modalItem = RadioMenuItemBuilder.create()
                .toggleGroup(modalGroup).text("modal").selected(true).build();
        modalItem.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                LOGIN_DIALOG = createLoginDialog(stage, true);
            }
        });
        menu.getItems().add(modalItem);

        // add sep
        menu.getItems().add(new SeparatorMenuItem());

        // add exit
        MenuItem exitItem = new MenuItem("Exit", null);
        exitItem.setMnemonicParsing(true);
        exitItem.setAccelerator(new KeyCodeCombination(KeyCode.X,
                KeyCombination.CONTROL_DOWN));
        exitItem.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                Platform.exit();
            }
        });
        menu.getItems().add(exitItem);

        // add menu
        menuBar.getMenus().add(menu);

        root.getChildren().add(menuBar);
        stage.setScene(scene);
        stage.show();

    }

    class MyDialog extends Stage {
        public MyDialog(Stage owner, boolean modality, String title) {
            super();
            initOwner(owner);
            Modality m = modality ? Modality.APPLICATION_MODAL : Modality.NONE;
            initModality(m);
            setOpacity(.90);
            setTitle(title);
            Group root = new Group();
            Scene scene = new Scene(root, 250, 150, Color.WHITE);
            setScene(scene);

            GridPane gridpane = new GridPane();
            gridpane.setPadding(new Insets(5));
            gridpane.setHgap(5);
            gridpane.setVgap(5);

            Label mainLabel = new Label("enter username & password");
            gridpane.add(mainLabel, 1, 0, 2, 1);

            Label userNamelbl = new Label("username");
            gridpane.add(userNamelbl, 0, 1);

            Label passwordlbl = new Label("password");
            gridpane.add(passwordlbl, 0, 2);

            // username textfield
            final TextField userNameFld = new TextField("Admin");
            gridpane.add(userNameFld, 1, 1);

            // password
            final PasswordField passwordFld = new PasswordField();
            passwordFld.setText("dwossap");
            gridpane.add(passwordFld, 1, 2);

            Button login = new Button("change");
            login.setOnAction(new EventHandler<ActionEvent>() {
                public void handle(ActionEvent event) {
                    close();
                }
            });

            gridpane.add(login, 1, 3);
            GridPane.setHalignment(login, HPos.RIGHT);
            root.getChildren().add(gridpane);

        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Knut Arne Vedaa
  • 15,372
  • 11
  • 48
  • 59
euoles
  • 46
  • 2
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 03 '16 at 23:52

1 Answers1

0

Three possible solutions (pick one):

  • Remove "static" from method createLoginDialog

  • Add "static" to class MyDialog

  • Move class MyDialog outside of class TryDialogBox

Knut Arne Vedaa
  • 15,372
  • 11
  • 48
  • 59