Search code examples
eclipseclassjavafxbind

Bind a Main Class variable to Control Class event in javaFX Eclipse Application


The Control class object is created in Main class. The button event of Control changes the value of Main class variable. I want to show the new value as text of same button. The variable value is changed but not shown in Button. I used bind in Main class to bind it's variable to Control class button event. The Main class is below:

package application;

import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Parent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;

public class Main extends Application {

Control controller = new Control();
public static int total_time = 60;
public void start(Stage primaryStage) {
    try 
    {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("/test.fxml"));
        AnchorPane root = (AnchorPane) loader.load();
        
        primaryStage.setTitle("TEST Bind Another Class Variable");
        Scene scene = new Scene(root);
         
  //scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);                       
        primaryStage.show();
        controller = (Control) loader.getController();
        
        controller.btnDemo.textProperty().bind(new 
       SimpleIntegerProperty(total_time).asString());
        
    } catch(Exception e) {
        e.printStackTrace();
    }
}

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

The Control class is : package application;

 import javafx.event.ActionEvent;
 import javafx.event.Event;
 import javafx.fxml.FXML;
 import javafx.stage.Stage;
 import javafx.scene.Scene;
 import javafx.scene.layout.BorderPane;
 import javafx.scene.text.Text;
 import javafx.scene.layout.AnchorPane;
 import javafx.scene.control.Button;
 import javafx.scene.control.TextField;
 import javafx.scene.control.Label;

  public class Control {
  @FXML TextField txtMain;
  @FXML Button btnDemo;
  @FXML
    public void demoBtnHandler(ActionEvent event) 
   {        
    if((Main.total_time+=10) > 60)Main.total_time = 10;
    System.out.print("Button Clicked, Time = ");System.out.println(Main.total_time);
    }
   }

The FXML file is:

  <?import javafx.scene.control.Button?>
  <?import javafx.scene.control.Label?>
  <?import javafx.scene.control.TextField?>
  <?import javafx.scene.layout.AnchorPane?>

 <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="- 
 Infinity" prefHeight="630.0" prefWidth="925.0" xmlns="http://javafx.com/javafx/19" 
 xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Control">
 <children>
   <Button fx:id="btnDemo" layoutX="412.0" layoutY="339.0" mnemonicParsing="false" 
   onAction="#demoBtnHandler" text="DEMO" />
  <TextField id="tf" fx:id="txtMain" layoutX="412.0" layoutY="298.0" prefHeight="26.0" 
  prefWidth="50.0" />
  <Label layoutX="484.0" layoutY="306.0" text="enter roll no. " />
  </children>
  </AnchorPane>

I could not resolve. Please run this code at your end.


Solution

  • I modified the whole code according to MVC as shown below and accessed the Button Events also as suggested by one viewer. The issue got resolved,

      import javafx.application.Application;
      import javafx.stage.Stage;
      import javafx.scene.Scene;
      import javafx.scene.layout.BorderPane;
    
    
       public class Main {
    
    
       public static void main(String[] args) {
        //launch(args);
        Application.launch(Spm.class);
       }
       }
    

    In spm class, created objects of all other classes as shown below. All it worked well.

     public class Spm extends Application {
     static serialPortClass spm_device = new serialPortClass();
    
    static Control controller = new Control();
    static PatientDB patientDataBase = new PatientDB();
    static UartCom uartcom = new UartCom();
    static NewPatientController patientcontrol = new NewPatientController(null, null, null, null, null, null, null, null, null, null); 
    
    public Spm() {
        // TODO Auto-generated constructor stub
    }
    @Override
    public void start(Stage primaryStage) {
        try 
        {   
          //Parent root = FXMLLoader.load(getClass().getResource("/main.fxml"));
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("Abc.fxml"));
            AnchorPane root = (AnchorPane) loader.load();
            
            primaryStage.setTitle("ABC_XYZ");
            Scene scene = new Scene(root);
            //scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);                       
            primaryStage.show();
            controller = (Control) loader.getController();  
    
            System.out.println("The Loader Path : "+ loader.getLocation().toURI());
    
          } catch(Exception e) {
            e.printStackTrace();
        }
        }
        }