Wednesday, November 30, 2016

Membuat Aplikasi untuk Memasukkan Anggota Organization : Membuat Class MemberEntriScreen 2

Disini saya akan memodifikasi program Sebelumnya sehingga dapat memasukkan data kedalam database. Perubahan yang dilakukan adalah menambah class ButtonHandler yang nerupakan implementasi EventHandler :

/**
 *
 * @author Joko Adianto
 * Released under GPLv3
 */

package javafxorganization;

import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class MemberEntriScreen {
    String strSQL;
    Statement stmt;
    Group root;
    Stage stage;
    Scene scene;
    Connection conApp;
  
    Label lblId;
    TextField txfId;
    HBox hbxId;
  
    Label lblName;
    TextField txfName;
    HBox hbxName;
  
    Label lblAddress;
    TextField txfAddress;
    HBox hbxAddress;
    
    Button btnSave;
    Button btnCancel;
    HBox hbxButton;
  
    VBox vbxMember;
    
    ButtonHandler bh;
    
    class ButtonHandler implements EventHandler<ActionEvent>{
        
        @Override
        public void handle(ActionEvent t) {
            //BUTTON SAVE
            if (t.getSource().equals(btnSave)){
                strSQL = "insert into member values("+
                        "'"+txfId.getText()+
                        "', '" +txfName.getText()+
                        "', '" +txfAddress.getText()+
                        "')";
                txfAddress.setText(strSQL);
                try {
                    stmt.execute(strSQL);
                }
                catch (SQLException ex){
                }
            }
            //BUTTON CANCEL
            else if(t.getSource().equals(btnCancel)){
              txfId.setText("");
              txfName.setText("");
              txfAddress.setText("");
            }
        }
      
    }
  
    {
        lblId = new Label("Member Id");
        lblId.setMinWidth(80);
        txfId = new TextField();
        txfId.setMinWidth(120);
        hbxId = new HBox();
        hbxId.getChildren().addAll(lblId, txfId);
      
  
        lblName = new Label("Name");
        lblName.setMinWidth(80);
        txfName = new TextField();
        txfName.setMinWidth(120);
        hbxName = new HBox();
        hbxName.getChildren().addAll(lblName, txfName);
  
        lblAddress = new Label("Address");
        lblAddress.setMinWidth(80);
        txfAddress = new TextField();
        txfAddress.setMinWidth(120);
        hbxAddress = new HBox();
        hbxAddress.getChildren().addAll(lblAddress, txfAddress);
        
        bh = new ButtonHandler();
        btnSave = new Button("Save");
        btnSave.setMinWidth(200);
        btnSave.setOnAction(bh);
        
        btnCancel = new Button("Cancel");
        btnCancel.setMinWidth(200);
        btnCancel.setOnAction(bh);
        hbxButton = new HBox();
        hbxButton.getChildren().addAll(btnSave, btnCancel);
        
        vbxMember = new VBox();
        vbxMember.getChildren().addAll(hbxId, 
                hbxName, 
                hbxAddress,
                hbxButton);
    }
  
    MemberEntriScreen(Stage stage, Connection conApp) throws SQLException{
        this.stage = stage;       
        this.conApp = conApp;
        stmt = this.conApp.createStatement();        
        root = new Group();
        root.getChildren().addAll(vbxMember);
        scene = new Scene(root, 800, 600, Color.BEIGE);
        this.stage.setScene(scene);
        this.stage.setTitle("Entri Data Member");
        this.stage.show();
    }
  
}

No comments:

Post a Comment