Saturday, December 16, 2017

Membuat Perintah SQL dengan FXML

  1. Buat Project FXML
  2. Edit berkas FXML
  3. Sesuaikan controller dengan berkas FXML
Berkas FXML yang saya buat adalah :
<?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);
    }
   
}

Wednesday, December 13, 2017

Kontrol dengan javafx

Berikut ini adalah FXMLDocument.fxml

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane"  xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxmlcontrol.FXMLDocumentController">
    <children>
        <VBox fx:id="vbx" >
        <padding><Insets top="25" right="25" bottom="25" left="25"/></padding>
            <children>
                <HBox id = "hbxName">
                    <children>
                        <Label fx:id = "lblName" minWidth="100" text="Name"/>
                        <TextField fx:id = "txfName" minWidth="200"/>
                    </children>                   
                </HBox>
                <HBox id = "hbxAddress">
                    <padding><Insets top="5" />
                        </padding>
                    <children>
                        <Label fx:id = "lblAddress" minWidth="100" text="Alamat"/>
                        <TextField fx:id = "txfAddress" minWidth="200"/>
                    </children>                   
                </HBox>
                <HBox id = "hbxButton" spacing = "100">
                    <padding><Insets top="10" />
                        </padding>
                    <children>                       
                        <Button fx:id= "btnSave" minWidth="100"
                            text="Save"/>
                       
                        <Button fx:id= "btnCancel" minWidth="100"                           
                            text="Cancel"/>
                    </children>                   
                </HBox>               
            </children>
        </VBox>
    </children>
</AnchorPane>

Selanjutnya kita memiliki FXMLDocumentController.java :
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javafxmlcontrol;

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;


public class FXMLDocumentController implements Initializable {
   
   
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
       
    }   
   
}

Yang terakhir adalah file aplikasi :

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javafxmlcontrol;

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


/**
 *
 * @author FASILKOM
 */
public class JavaFXMLControl 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("Control Exercise");
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
   
}