Membuat Login WIndow dengan Menu untuk Membuka Window yang lain

Saya memiliki sebuah scenario untuk :

  1. Menampilkan jendela login dengan menu, untuk menampilkan jendela lain yang berisi tampilan tabel
  2. Connection hanya diinisiasi sekali didalam jendela login. Selanjutnya connection di digunakan didalam jendela lain
1 Login untuk 2 Windows
1 Login untuk 2 Windows

 

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();

}

}

 

 

TableView dan Operasi Insert pada database

Layar aplikasi yang dapat menyimpan data didalam database adalah :

TableViewDatabase1

  1. Program diatas membuat koneksi, menjalankan perintah pemanggilan isi tabel, dan menyimpan hasilnya didalam result set.
  2. Sedangkan tombol append akan menampilkan perintah SQL yang digunakan untuk memasukkan data, dan menjalankannya pada object Statement

Adapun Kode program untuk menjalankan program diatas adalah :

package javafxtableviewdatabase1;

import java.lang.reflect.InvocationTargetException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

public class JavaFXTableViewDatabase1 extends Application {
public class Friend{
final private SimpleStringProperty friendNo;
final private SimpleStringProperty name;
final private SimpleStringProperty address;

Friend(String FriendNo, String Name, String Address) {
this.friendNo = new SimpleStringProperty(FriendNo);
this.name = new SimpleStringProperty(Name);
this.address = new SimpleStringProperty(Address);
}

public String getFriendNo() {
return friendNo.get();
}

public void setFriendNo(String FriendNo) {
this.friendNo.set(FriendNo);
}

public String getName() {
return name.get();
}

public void setName(String Name) {
this.name.set(Name);
}

public String getAddress() {
return address.get();
}

public void setAddress(String Address) {
this.address.set(Address);
}
}

TableView<Friend> tableFriend = new TableView<>();
ObservableList<Friend> listFriend = FXCollections.observableArrayList();
HBox hbox1 = new HBox();
HBox hbox2 = new HBox();

private void initiateSQL(Connection conn1, Statement stmt1, ResultSet rs1)
throws ClassNotFoundException,
InstantiationException,
IllegalAccessException,
SQLException{

}

@Override
public void start(Stage primaryStage) throws
ClassNotFoundException,
InstantiationException,
IllegalAccessException,
SQLException{

Connection conn1;
Statement stmt1, stmt2;
ResultSet rs1;
Class.forName(“org.apache.derby.jdbc.ClientDriver”).newInstance();
conn1 = DriverManager.getConnection(“jdbc:derby://localhost:1527/FriendsDB”,
“app”, “app”);

stmt1 = conn1.createStatement();

rs1 = stmt1.executeQuery(“select * from FRIEND”);

TableColumn colFriendNo = new TableColumn(“No”);
colFriendNo.setPrefWidth(80);
colFriendNo.setCellValueFactory(
new PropertyValueFactory<>(“friendNo”));

TableColumn colFriendName = new TableColumn(“Name”);
colFriendName.setPrefWidth(150);
colFriendName.setCellValueFactory(
new PropertyValueFactory<>(“name”));

TableColumn colAddress = new TableColumn(“Address”);
colAddress.setPrefWidth(150);
colAddress.setCellValueFactory(
new PropertyValueFactory<>(“address”));

tableFriend.setItems(listFriend);
tableFriend.getColumns().addAll(colFriendNo,
colFriendName, colAddress);

GridPane root = new GridPane();
root.getChildren().add(tableFriend);

final TextField textNo = new TextField();
textNo.setPrefWidth(colFriendNo.getPrefWidth()-30);
textNo.setPromptText(“No”);
final TextField textName = new TextField();
textName.setPrefWidth(colFriendName.getPrefWidth()-30);
textName.setPromptText(“Name”);
final TextField textAddress = new TextField();
textAddress.setPrefWidth(colAddress.getPrefWidth()-30);
textAddress.setPromptText(“Address”);
hbox1.getChildren().addAll(textNo, textName, textAddress);
hbox1.setPadding(new Insets(5, 0, 0, 5));
hbox1.setSpacing(5);

TextField textSQL = new TextField(“Generated SQL statemen (unexecuted)”);
textSQL.setPrefWidth(300);

Button buttonAppend = new Button(“Append”);
buttonAppend.setPrefWidth(80);
buttonAppend.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
listFriend.add(new Friend(
textNo.getText(),
textName.getText(),
textAddress.getText()));

textSQL.setText(“insert into FRIEND values(”
+textNo.getText()+”, ‘”+textName.getText()+”‘, ‘”
+textAddress.getText()+”‘)”);
try {
stmt1.execute(textSQL.getText());
} catch (SQLException ex) {
Logger.getLogger(JavaFXTableViewDatabase1.class.getName()).log(Level.SEVERE, null, ex);
}

textNo.clear();
textName.clear();
textAddress.clear();
}
});

while (rs1.next()) {

listFriend.add(
new Friend(Integer.toString(rs1.getInt(1)),
rs1.getString(2),
rs1.getString(3)));
}

hbox2.getChildren().addAll(buttonAppend, textSQL);
hbox2.setPadding(new Insets(5, 0, 0, 5));
hbox2.setSpacing(5);

VBox vbox1 = new VBox();
vbox1.setSpacing(5);
vbox1.setPadding(new Insets(5, 0, 0, 5));
vbox1.getChildren().addAll(tableFriend, hbox1);
VBox vbox2 = new VBox();
vbox2.setSpacing(5);

vbox2.setPadding(new Insets(5, 0, 0, 5));
vbox2.getChildren().add(hbox2);

root.add(vbox1, 1, 1);
root.add(vbox2, 1, 2);

Scene scene = new Scene(root, 400, 600);

primaryStage.setTitle(“FriendsQ”);
primaryStage.setScene(scene);
primaryStage.show();
}

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

}

&&&

TableView dengan Penambahan Data dan SQL untuk Penyimpanannya

Kita akan membuat program untuk memasukkan nomor, nama, dan alamat. Tombol Append digunakan untuk menambah data. Sama seperti program-program yang menggunakan TableView, kita menggunakan ObservableList untuk menyimpan data yang ditampilkan pada TableView. Tampilan program tersebut adalah sebagai berikut :

TableViewTambahData

Pada aplikasi tersebut diatas kita menggunakan beberapa object baru :

  • GridPane adalah tempat meletakkan object-object dimana kita mengatur kolom dan baris dari peletakkan object
  • vbox dan hbox untuk pengelompokan object secara vertikal dan horizontal

Hal lain yang baru dilakukan adalah membuat perintah sql yang akan dieksekusi pada object Statement. Sekrang program belum melakukan koneksi ke database sama sekali. Kode Program yang dibuat adalah sebagai berikut (pernyataan import dan package tidak disertakan) :

public class JavaFXTableViewTambahData extends Application {
public class Friend{
final private SimpleStringProperty friendNo;
final private SimpleStringProperty name;
final private SimpleStringProperty address;

Friend(String FriendNo, String Name, String Address) {
this.friendNo = new SimpleStringProperty(FriendNo);
this.name = new SimpleStringProperty(Name);
this.address = new SimpleStringProperty(Address);
}

public String getFriendNo() {
return friendNo.get();
}

public void setFriendNo(String FriendNo) {
this.friendNo.set(FriendNo);
}

public String getName() {
return name.get();
}

public void setName(String Name) {
this.name.set(Name);
}

public String getAddress() {
return address.get();
}

public void setAddress(String Address) {
this.address.set(Address);
}

}

TableView<Friend> tableFriend = new TableView<>();
ObservableList<Friend> listFriend = FXCollections.observableArrayList();
HBox hbox1 = new HBox();
HBox hbox2 = new HBox();

@Override
public void start(Stage primaryStage) {
tableFriend.setEditable(true);

TableColumn colFriendNo = new TableColumn(“No”);
colFriendNo.setPrefWidth(80);
colFriendNo.setCellValueFactory(
new PropertyValueFactory<>(“friendNo”));

TableColumn colFriendName = new TableColumn(“Name”);
colFriendName.setPrefWidth(150);
colFriendName.setCellValueFactory(
new PropertyValueFactory<>(“name”));

TableColumn colAddress = new TableColumn(“Address”);
colAddress.setPrefWidth(150);
colAddress.setCellValueFactory(
new PropertyValueFactory<>(“address”));

tableFriend.setItems(listFriend);
tableFriend.getColumns().addAll(colFriendNo,
colFriendName, colAddress);

GridPane root = new GridPane();
root.getChildren().add(tableFriend);

final TextField textNo = new TextField();
textNo.setPrefWidth(colFriendNo.getPrefWidth()-30);
textNo.setPromptText(“No”);
final TextField textName = new TextField();
textName.setPrefWidth(colFriendName.getPrefWidth()-30);
textName.setPromptText(“Name”);
final TextField textAddress = new TextField();
textAddress.setPrefWidth(colAddress.getPrefWidth()-30);
textAddress.setPromptText(“Address”);
hbox1.getChildren().addAll(textNo, textName, textAddress);
hbox1.setPadding(new Insets(5, 0, 0, 5));
hbox1.setSpacing(5);

TextField textSQL = new TextField(“Generated SQL statemen (unexecuted)”);
textSQL.setPrefWidth(300);

Button buttonAppend = new Button(“Append”);
buttonAppend.setPrefWidth(80);
buttonAppend.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
listFriend.add(new Friend(
textNo.getText(),
textName.getText(),
textAddress.getText()));
textSQL.setText(“insert into friends values(‘”
+textNo.getText()+”‘, ‘”+textName.getText()+”‘, ‘”
+textAddress.getText()+”‘)”);
textNo.clear();
textName.clear();
textAddress.clear();
}
});

hbox2.getChildren().addAll(buttonAppend, textSQL);
hbox2.setPadding(new Insets(5, 0, 0, 5));
hbox2.setSpacing(5);

VBox vbox1 = new VBox();
vbox1.setSpacing(5);
vbox1.setPadding(new Insets(5, 0, 0, 5));
vbox1.getChildren().addAll(tableFriend, hbox1);
VBox vbox2 = new VBox();
vbox2.setSpacing(5);

vbox2.setPadding(new Insets(5, 0, 0, 5));
vbox2.getChildren().add(hbox2);

root.add(vbox1, 1, 1);
root.add(vbox2, 1, 2);

Scene scene = new Scene(root, 400, 600);

primaryStage.setTitle(“FriendsQ”);
primaryStage.setScene(scene);
primaryStage.show();
}

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

&&&

Program TableView dengan Database Derby

Saya akan menggunakan object TableView untuk menampilkan hasil query ‘select CUSTOMER_ID, NAME from CUSTOMER’

TableViewDenganDatabase

Maka yang kita lakukan pada dasarnya adalah :

  1. Membuat inner class yang berisi object-object SimpleStringProperty yang digunakan oleh kolom-kolom didalam object TableView. Innser class yang dibuat adalah Customer.
  2. Terhubung dan memperoleh hasil query database.
  3. Membuat object-object TableColumn, dan meletakkannya didalam object TableView
  4. Memasukkan hasil query kedalam object ObservabelList dalam hal ini customerData.
  5. Nyatakan bahwa object TableView berisi object ObservableList
  6. Letakkan object TableView kedalam object StackPane.

Kode program yang dibuat adalah

package javafxtableview;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javafx.application.Application;
import static javafx.application.Application.launch;
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 android
*/

public class JavaFXTableView extends Application {

public static class Customer{
private final SimpleStringProperty customerId;
private final SimpleStringProperty name;

Customer(String customerId, String name) {
this.name = new SimpleStringProperty(name);
this.customerId = new SimpleStringProperty(customerId);
}

public String getName() {
return name.get();
}

public void setName(String name1) {
name.set(name1);
}

public String getCustomerId() {
return customerId.get();
}

public void setCustomerId(String customerId1) {
customerId.set(customerId1);
}
}
private final TableView<Customer> tableView1 = new TableView<>();
private final ObservableList<Customer> customerData =
FXCollections.observableArrayList();

@Override
public void start(Stage primaryStage) throws
ClassNotFoundException,
InstantiationException,
IllegalAccessException,
SQLException {
Connection conn1;
Statement stmt1;
ResultSet rs1;

Class.forName(“org.apache.derby.jdbc.ClientDriver”).newInstance();
conn1 = DriverManager.getConnection(“jdbc:derby://localhost:1527/sample”,
“app”, “app”);

stmt1 = conn1.createStatement();
rs1 = stmt1.executeQuery(“select CUSTOMER_ID, NAME ”
+ “from CUSTOMER”);
while(rs1.next()){
customerData.add(new Customer(rs1.getString(1), rs1.getString(2)));
}
//Membuat kolom pertama
TableColumn customerIdCol = new TableColumn(“Customer Id”);
customerIdCol.setMinWidth(50);
customerIdCol.setCellValueFactory(
new PropertyValueFactory<>(“customerId”));
TableColumn nameCol = new TableColumn(“Name”);
nameCol.setMinWidth(250);
nameCol.setCellValueFactory(
new PropertyValueFactory<>(“name”));
//Membuat kolom kedua
tableView1.setItems(customerData);
tableView1.getColumns().addAll(customerIdCol, nameCol);

StackPane root = new StackPane();
root.getChildren().add(tableView1);
Scene scene = new Scene(root, 300, 300);

primaryStage.setTitle(“TableView dengan Database”);
primaryStage.setScene(scene);
primaryStage.show();
}

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

}

&&&

Program ListView dengan Menggunakan Database Derby

Saya akan menampilkan nama Customer, yang terletak pada kolom ke empat dari resultset hasil query ‘select * from CUSTOMER’ :

ListViewDenganDatabase

Karena aplikasi yang saya buat berkomunikasi dengan Derby maka layanan Java DB harus saya hidupkan :

  1. Klik tab ‘Services’.
  2. Sorot ‘Java DB’.
  3. Klik Kanan.
  4. Pilih menu ‘Start Server’.
  5. Tanpa layanan Java DB program tidak akan berjalan.

Kita akan menggunakan koneksi database. Sehingga kita harus memiliki :

  • Connection (Untuk koneksi database), Statement(Untuk membuat pernyataan SQL), dan ResultSet (Cursor yang berisi masukan dari tabel.
  • Kita memiliki daftar string. Daftar string tersebut merupakan sebuah ObservableList, yaitu datfar atau array yang dapat digunakan oleh object-object lain seperti ListView dan TableView. (ObservableList<String> list1 = FXCollections.observableArrayList();)
  • Kita lakukan looping untuk mengisi daftar tersebut.
  • Yang terakhir adalah menyatakan bahwa isi dari object ListView adalah object dari ObservableList.
  • Selanjutnya object ListView diletakkan didalam object StackPane (root) untuk ditampilkan.

Berikut ini adalah potongan program dari aplikasi diatas :

@Override
public void start(Stage primaryStage) throws  ClassNotFoundException, InstantiationException,
IllegalAccessException, SQLException {
Connection conn1;
Statement stmt1;
ResultSet rs1;
ObservableList<String> list1 = FXCollections.observableArrayList();
Class.forName(“org.apache.derby.jdbc.ClientDriver”).newInstance();
conn1 = DriverManager.getConnection(“jdbc:derby://localhost:1527/sample”,
“app”, “app”);
stmt1 = conn1.createStatement();
rs1 = stmt1.executeQuery(“select * from CUSTOMER”);
while (rs1.next()) {
list1.add(rs1.getString(4));
}
ListView listView1=new ListView(list1);
StackPane root = new StackPane();
root.getChildren().add(listView1);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle(“ListView dengan Database”);
primaryStage.setScene(scene);
primaryStage.show();
}

&&&