Saya memiliki sebuah scenario untuk :
- Menampilkan jendela login dengan menu, untuk menampilkan jendela lain yang berisi tampilan tabel
- Connection hanya diinisiasi sekali didalam jendela login. Selanjutnya connection di digunakan didalam jendela lain
Berikut ini adalah kode program untuk jendela login yang bernama JavaFXTampilan.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 javafxtampilan;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.TextField;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.VBox;
/**
*
* @author user
*/
public class JavaFXTampilan extends Application {
MenampilkanCustomer menampilkanCustomer;
Connection conn1;
VBox vBox;
BorderPane borderPane;
Menu menuFile;
Menu menuTampilkan;
MenuBar menuBar;
MenuItem itemLogin;
MenuItem itemLogout;
MenuItem itemCustomer;
MenuItem itemDiscountCode;
MenuItem itemMicroMarket;
MenuItem itemProduct;
MenuItem itemProductCode;
MenuItem itemPurchaseCode;
MenuItem itemPurchaseOrder;
TextField textUser;
PasswordField passwordField;
//Stage stageCustomer;
private void initializeMenu(){
itemLogin = new MenuItem(“Login”);
itemLogout = new MenuItem(“Logout”);
itemCustomer = new MenuItem(“Display Custormer”);
itemCustomer.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
menampilkanCustomer = new MenampilkanCustomer();
menampilkanCustomer.conn = conn1;
//stageCustomer = new Stage();
try {
//menampilkanCustomer.start(stageCustomer);
//atau
menampilkanCustomer.start(new Stage());
//new Stage() : -> Membuat object stage;
} catch (ClassNotFoundException ex) {
Logger.getLogger(JavaFXTampilan.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(JavaFXTampilan.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(JavaFXTampilan.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(JavaFXTampilan.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
itemDiscountCode = new MenuItem(“Display Discount Code”);
itemMicroMarket = new MenuItem(“Display Micro Market”) ;
itemProduct = new MenuItem(“Product”) ;
itemProductCode = new MenuItem(“Product Code”) ;
itemPurchaseCode = new MenuItem(“Purchase Code”) ;
itemPurchaseOrder = new MenuItem(“Purchase Order”) ;
menuFile = new Menu(“File”);
menuFile.getItems().addAll(
itemLogin,
itemLogout);
menuTampilkan = new Menu(“Tampilkan Tabel”);
menuTampilkan.getItems().addAll(
itemCustomer,
itemDiscountCode,
itemMicroMarket,
itemProduct,
itemProductCode,
itemPurchaseCode,
itemPurchaseOrder);
menuBar = new MenuBar();
menuBar.getMenus().addAll(menuFile, menuTampilkan);
}
private void initializeBorderPane(){
borderPane = new BorderPane();
borderPane.setTop(menuBar);
}
@Override
public void start(Stage primaryStage) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class.forName(“org.apache.derby.jdbc.ClientDriver”).newInstance();
textUser = new TextField();
passwordField = new PasswordField();
vBox = new VBox();
Button btn = new Button();
btn.setText(“Login”);
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
conn1 = DriverManager.getConnection(“jdbc:derby://localhost:1527/sample”,
textUser.getText(),
passwordField.getText());}
catch (SQLException ex) {
Logger.getLogger(JavaFXTampilan.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
initializeMenu();
initializeBorderPane();
vBox.getChildren().add(0, textUser);
vBox.getChildren().add(1, passwordField);
vBox.getChildren().add(2, btn);
borderPane.setCenter(vBox);
Scene scene = new Scene(borderPane, 300, 500);
primaryStage.setTitle(“Tampilan Tabel!”);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Selanjutnya jendela untuk menampilkan tabel Customer adalah
/*
* 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 javafxtampilan;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* @author user
*/
public class MenampilkanCustomer extends Application{
public static class Customer{
private final SimpleStringProperty customerId;
private final SimpleStringProperty discountCode;
private final SimpleStringProperty zip;
private final SimpleStringProperty name;
Customer(String customerId, String discountCode, String zip, String name) {
this.customerId = new SimpleStringProperty(customerId);
this.discountCode = new SimpleStringProperty(discountCode);
this.zip = new SimpleStringProperty(zip);
this.name = new SimpleStringProperty(name);
}
public String getCustomerId() {
return customerId.get();
}
public void setCustomerId(String customerId) {
this.customerId.set(customerId);
}
public String getDiscountCode() {
return discountCode.get();
}
public void setDiscountCode(String discountCode) {
this.discountCode.set(discountCode);
}
public String getZip() {
return zip.get();
}
public void setZip(String zip) {
this.zip.set(zip);
}
public String getName() {
return name.get();
}
public void setName(String name) {
this.name.set(name);
}
}
private final TableView<Customer> tableViewCustomer = new TableView<>();
private final ObservableList<Customer> customerData =
FXCollections.observableArrayList();
public Connection conn;
@Override
public void start(Stage primaryStage) throws
ClassNotFoundException,
InstantiationException,
IllegalAccessException,
SQLException {
Statement stmt1;
ResultSet rs1;
stmt1 = conn.createStatement();
rs1 = stmt1.executeQuery(“select * from CUSTOMER”);
while (rs1.next()){
customerData.add(new Customer(rs1.getString(1), rs1.getString(2),
rs1.getString(3), rs1.getString(4)));
}
/*Membuat Kolom-1 : customerId*/
TableColumn customerIdCol = new TableColumn(“Customer Id”);
customerIdCol.setMinWidth(50);
customerIdCol.setCellValueFactory(
new PropertyValueFactory<>(“customerId”));
/*Membuat Kolom-2 : discountCode*/
TableColumn discountCodeCol = new TableColumn(“Discount Code”);
discountCodeCol.setMinWidth(50);
discountCodeCol.setCellValueFactory(
new PropertyValueFactory<>(“discountCode”));
/*Membuat Kolom-3 : zip*/
TableColumn zipCol = new TableColumn(“Zip”);
zipCol.setMinWidth(50);
zipCol.setCellValueFactory(
new PropertyValueFactory<>(“zip”));
/*Membuat Kolom-4 : name*/
TableColumn nameCol = new TableColumn(“Name”);
nameCol.setMinWidth(50);
nameCol.setCellValueFactory(
new PropertyValueFactory<>(“name”));
/*Membuat tampilan table : tableViewCustomer*/
tableViewCustomer.setItems(customerData);
tableViewCustomer.getColumns().addAll(
customerIdCol,
discountCodeCol,
zipCol,
nameCol
);
StackPane root = new StackPane();
root.getChildren().add(tableViewCustomer);
Scene scene = new Scene(root, 300, 300);
primaryStage.setTitle(“TableView : Tampilan Customer”);
primaryStage.setScene(scene);
primaryStage.show();
}
}