the code will show you what im trying to do, I hope you understand

im new to programming but know the basics of java and im trying to add Object Oriented Programming to my apps so its not procedural
thanks , if you need more info to understand just ask me.

   package myApp;

    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    public class Main extends Application {

        @Override
        public void start(Stage myStage) throws Exception

        {

            VBox root = new VBox();

            Scene scene = new Scene(root);

            myStage.setScene(scene);

            myStage.show();

            Button btn = new Button("Click Me");

            root.getChildren().add(btn);

            bank yo = new bank();

            btn.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {

                    bank.customer();

                }
            });

        }

        public static void main(String[] args) {
            Application.launch(args);

        }
    }

package myApp;

import javafx.scene.control.Button;

public class bank

{

    public static void customer()

    {

        btn.setText("yo");

    }

}

Dani AI

Generated

the issue is scope and coupling: bank.customer() tries to change a Button it does not own. As hinted, keep UI controls in the view and expose state from your model/service. In JavaFX the clean way is to use properties and binding. Let the model publish a StringProperty; the button binds to it. Clicking the button calls a model method that updates the property, and the UI updates automatically. No FXML needed.

// Main.java
package myApp;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {
  @Override
  public void start(Stage stage) {
    Bank bank = new Bank();                // model/service

    Button btn = new Button();
    btn.textProperty().bind(bank.buttonTextProperty());  // view <- model

    btn.setOnAction(e -> bank.customer()); // controller -> model

    VBox root = new VBox(btn);
    stage.setScene(new Scene(root, 300, 200));
    stage.show();
  }

  public static void main(String[] args) { launch(args); }
}
// Bank.java
package myApp;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.application.Platform;

public class Bank {
  private final StringProperty buttonText = new SimpleStringProperty("Click Me");

  public void customer() {
    buttonText.set("yo");
    // If this were called from a background thread, do:
    // Platform.runLater(() -> buttonText.set("yo"));
  }

  public StringProperty buttonTextProperty() { return buttonText; }
}

Two final tips: avoid mixing static and instance usage (your original customer() was static, but you also created an instance you never used). And resist passing the Button into Bank; binding keeps your model independent of the UI, which aligns with the MVC guidance from .

Recommended Answers

All 2 Replies

just to clarify the code is not working

In OO each object has a public interface and a private implementation. No object has access to any other object's private implementation. This massively simplifies the overall architecture by restricting the number of ways that objects can interact. What you are doing here violates that; the bank object is trying to fiddle with part of the GUI's implementation.

The standard architectural solution to this is called MVC. There's loads of stuff on the web about it, but here's a good starting explanation. Google for more.

When you've read that, note that in JavaFX the model and the controller are very often combined in one class, but the model is always separate and independent.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.