- Buat Project FXML
- Edit berkas FXML
- Sesuaikan controller dengan berkas FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1"
id="anpMultipleControl"
fx:controller="javafxml.FXMLDocumentController">
<children>
<VBox
style="-fx-background-color: #9999bb;">
<children>
<HBox fx:id = "hbxName">
<padding><Insets top="10" bottom="5"/>
</padding>
<children>
<Label fx:id = "lblName" minWidth = "100" text = "Name"/>
<TextField fx:id = "txfName" minWidth = "200"/>
</children>
</HBox>
<HBox fx:id = "hbxAddress">
<padding><Insets top="10" bottom="5"/>
</padding>
<children>
<Label fx:id = "lblAddress" minWidth = "100" text = "Address"/>
<TextField fx:id = "txfAddress" minWidth = "200"/>
</children>
</HBox>
<HBox fx:id = "hbxButton" spacing = "100">
<padding><Insets top="10" bottom="5"/>
</padding>
<children>
<Button fx:id = "btnSave" text = "Save" minWidth="100"
onAction = "#handleButtonSave"/>
<Button fx:id = "btnCancel" text ="Cancel" minWidth="100"
style="-fx-background-color: #3399ff;"
onAction = "#handleButtonCancel" />
</children>
</HBox>
</children>
</VBox>
</children>
</AnchorPane>
Selanjutnya controller disesuaikan dengan berkas FXML :
public class FXMLDocumentController implements Initializable {
@FXML
TextField txfName, txfAddress;
@FXML
private void handleButtonCancel(ActionEvent event) {
txfName.setText("");
txfAddress.setText("");
}
@FXML
private void handleButtonSave(ActionEvent event) {
String strSQL = "Insert into teman values('"+
txfName.getText().toString()+"', '"+
txfAddress.getText().toString()+"')";
System .out.println(strSQL);
txfName.setText("");
txfAddress.setText("");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
perhatikanlah bahwa string sql diterbitkan oleh method :
@FXML
private void handleButtonSave(ActionEvent event) {
String strSQL = "Insert into teman values('"+
txfName.getText().toString()+"', '"+
txfAddress.getText().toString()+"')";
System .out.println(strSQL);
txfName.setText("");
txfAddress.setText("");
}
Yang terakhir adalah file aplikasi :
package javafxml;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author LAB
*/
public class JavaFXML extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Multiple Control");
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}