PROGRAMMING

  minte9
MEMORYApp




R Q
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

On Action

p338 Add ActionApp main class with button action listener method.
 
/**
 * FXML on action method is defined in ...
 * Scene Builer 'on Action'
 */

package com.minte9.javafx.on_action;
import javafx.fxml.FXML;

public class ActionApp {
    public static void main(String[] args) {
        ActionAppGui.main(args);
    }

    @FXML void buttonPressed() { // Look Here
        System.out.println("button pressed");
    }
}

Launch

Add ActionAppGui class that loads fxml file.
 
/**
 * Gui class with button.xml loader
 */

package com.minte9.javafx.on_action;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class ActionAppGui extends Application {

    public static void main(String[] args) {
        launch(args);
    }
    
    @Override public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("button.fxml"));

        stage.setTitle("Button Event FXML");
        stage.setScene(new Scene(root));
        stage.show();
    }
}

Scene Builder

Set controller and button method (buttonPressed).
 
Containers / Add VBox 
Controllers / Add Button
Controller / ControllerClass ... com.minte9.javafx.on_action.ActionApp

Button / Inspector / Code / On Action ... buttonPressed

Preview / Show Preview in Window
View / Show sample Controller Skeleton

Save ... button.fxml

Instance

Each value for a JavaFX object is used to set object's instance variables.
 
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" 
   minHeight="-Infinity" minWidth="-Infinity" 
   prefHeight="400.0" prefWidth="600.0" 
   xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" 
   fx:controller="com.minte9.javafx.on_action.ActionApp">

   <children>
      <Button mnemonicParsing="false" onAction="#buttonPressed" text="Button" />
   </children>
</VBox>

Questions    
Last update: 385 days ago
Change Listener