- Yang penting untuk diperhatikan adalah : Definisi berkas *xml
- Membuat class sebagai enkapsulasi properti dari data yang ditampilkan
- Modifikasi class controller dari berkas fxml
- Semua program dihalaman ini dapat digunakan sesuai dengan lisensi GPL3
Definisi berkas xml : FXMLCustomer.xml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.collections.FXCollections?>
<GridPane id="AnchorPane"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="fxmltableview.FXMLTableViewController">
<TableView fx:id = "tbl1" >
<columns>
<TableColumn fx:id="tbc1" text="Name" minWidth="300" >
<cellValueFactory>
<javafx.scene.control.cell.PropertyValueFactory
property="sspName" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="tbc2" text="Discount Code" minWidth="200" >
<cellValueFactory>
<javafx.scene.control.cell.PropertyValueFactory
property="sspDiscountCode" />
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
</FXCollections>
</items>
</TableView>
</GridPane>
Membuat class untuk enkapsulasi properti : Customer.java
package fxmltableview;
import javafx.beans.property.SimpleStringProperty;
/**
*
* @author Joko adianto
*/
public class Customer {
private final SimpleStringProperty sspName = new SimpleStringProperty("");
private final SimpleStringProperty sspDiscountCode = new SimpleStringProperty("");
public Customer(){
this("","");
}
public Customer(String strName, String strDiscountCode){
setSspName(strName);
setSspDiscountCode(strDiscountCode);
}
public void setSspName(String strName){
sspName.set(strName);
}
public String getSspName(){
return sspName.get();
}
public void setSspDiscountCode(String strDiscountCode){
sspDiscountCode.set(strDiscountCode);
}
public String getSspDiscountCode(){
return sspDiscountCode.get();
}
}
Membuat Class Controller : FXMLTableViewController.java
package fxmltableview;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Joko Adianto
*/
public class FXMLTableViewController implements Initializable {
Connection con;
Statement stm;
ResultSet rst;
@FXML
private TableView<Customer> tbl1;
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
ObservableList<Customer> customerList = tbl1.getItems();
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
con = DriverManager.getConnection("jdbc:derby://localhost:1527/sample",
"app", "app");
stm = con.createStatement();
rst = stm.executeQuery("select * from Customer");
while(rst.next()){
customerList.add(new Customer(rst.getString("NAME"),
rst.getString("DISCOUNT_CODE")));
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(FXMLTableViewController.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(FXMLTableViewController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Yang terakhir membuat class pemanggil : JavaFXApplication1.java
package fxmltableview;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author Multimedia1_Pengawas
*/
public class JavaFXApplication1 extends Application {
@Override
public void start(Stage stage) throws Exception {
try{
Parent root = FXMLLoader.load(getClass().getResource("FXMLTableView.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Table View : Joko Adianto");
stage.show();
}
catch(Exception e){
stage.setTitle(e.getMessage());
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
&&&&