Sunday, May 20, 2012

Create GridPane using FXML

javafx.scene.layout.GridPane lays out its children within a flexible grid of rows and columns.

GridPane using FXML


Modify Sample.fxml and Sample.java from the auto generated files described in the article "JavaFX FXML Application".

Sample.fxml
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.textfield.*?>

<GridPane fx:controller="javafxml.Sample" xmlns:fx="http://javafx.com/fxml" 
    prefHeight="200" prefWidth="320"
    alignment="center" hgap="10" vgap="10">

        <Text text="java-Buddy"
            GridPane.columnIndex="0" GridPane.rowIndex="0"
            GridPane.columnSpan="2"/>
        
        <Label text="Who are you?"
            GridPane.columnIndex="0" GridPane.rowIndex="1"/>
            
        <TextField id="textfield" fx:id="textfield"
            GridPane.columnIndex="1" GridPane.rowIndex="1"/>
 
        <Button id="button"  text="Click Me!" 
            onAction="#handleButtonAction" fx:id="button"
            GridPane.columnIndex="0" GridPane.rowIndex="2"/>
 
        <Label id="label" fx:id="label"
            GridPane.columnIndex="1" GridPane.rowIndex="2"/>
        

</GridPane>


Sample.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javafxml;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class Sample implements Initializable {
    
    @FXML
    private Label label;
    @FXML
    private TextField textfield;
    
    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello  " + textfield.getText());
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
}


Related:
- Create BorderPane using FXML
- Create FlowPane using FXML
- Create AnchorPane using FXML


No comments:

Post a Comment