0% found this document useful (0 votes)
438 views3 pages

Javafx Tableview

This document discusses connecting to a database, building a table view dynamically from the database query results, and adding interactivity to the table view. It connects to a database, executes a SQL query to retrieve results, dynamically adds table columns based on the result metadata, populates the table with the query data, and renders the table. It also discusses taking a snapshot of the rendered table and saving it to a PDF file. Event handling is added to the table cells to display information on click.

Uploaded by

Kaka Kaka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
438 views3 pages

Javafx Tableview

This document discusses connecting to a database, building a table view dynamically from the database query results, and adding interactivity to the table view. It connects to a database, executes a SQL query to retrieve results, dynamically adds table columns based on the result metadata, populates the table with the query data, and renders the table. It also discusses taking a snapshot of the rendered table and saving it to a PDF file. Event handling is added to the table cells to display information on click.

Uploaded by

Kaka Kaka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

rs.

last();
Integer size = rs.getRow();
rs.beforeFirst();
PreparedStatement myStm = conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY

TableView<String> table = new TableView<>();


table.setId("Tbl");
TableColumn<String, String> TVehicle = new
TableColumn<>("Vehicle");
TableColumn<String, String> TYear = new
TableColumn<>("Year");
TableColumn<String, String> TSector = new
TableColumn<>("Sector");
TableColumn<String, String> TRegistration = new
TableColumn<>("Registration");
TableColumn<String, String> TConsumption = new
TableColumn<>("Consumption");
TableColumn<String, String> TKilometers = new
TableColumn<>("Kilometers");
TableColumn<String, String> TData = new
TableColumn<>("Date of input");
table.getColumns().addAll(TVehicle,TYear,TSector,
TRegistration, TConsumption, TKilometers, TData );

//MAIN EXECUTOR
public static void main(String[] args) {
launch(args);
}

//CONNECTION DATABASE
public void buildData(){
Connection c ;
data = FXCollections.observableArrayList();
try{
c = DBConnect.connect();
//SQL FOR SELECTING ALL OF CUSTOMER
String SQL = "SELECT * from CUSTOMer";
//ResultSet
ResultSet rs = c.createStatement().executeQuery(SQL);

/**********************************
* TABLE COLUMN ADDED DYNAMICALLY *
**********************************/
for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++){
//We are using non property style for making dynamic table
final int j = i;
TableColumn col = new
TableColumn(rs.getMetaData().getColumnName(i+1));
col.setCellValueFactory(new
Callback<CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){

public ObservableValue<String>
call(CellDataFeatures<ObservableList, String> param) {

return new
SimpleStringProperty(param.getValue().get(j).toString());
}
});

tableview.getColumns().addAll(col);
System.out.println("Column ["+i+"] ");
}

/********************************
* Data added to ObservableList *
********************************/
while(rs.next()){
//Iterate Row
ObservableList<String> row = FXCollections.observableArrayList();
for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++){
//Iterate Column
row.add(rs.getString(i));
}
System.out.println("Row [1] added "+row );
data.add(row);

//FINALLY ADDED TO TableView


tableview.setItems(data);
}catch(Exception e){
e.printStackTrace();
System.out.println("Error on Building Data");
}
}

@Override
public void start(Stage stage) throws Exception {
//TableView
tableview = new TableView();
buildData();

//Main Scene
Scene scene = new Scene(tableview);

stage.setScene(scene);
stage.show();
}

PDDocument doc = new PDDocument();


PDPage page = new PDPage();

doc.save("Database.pdf");

WritableImage snapshot = Tables_a.snapshot(new


SnapshotParameters(), null);

doc.addPage(page);

//PDPageContentStream content = new PDPageContentStream(doc,


page);
doc.close();
System.out.println(System.getProperty("user.dir"));

for (final XYChart.Data<String, Number> gra : ScatterChart.Series<String, Number>


data ) {

gra.getNode().addEventHandler(MouseEvent.MOUSE_CLICKED, new
EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
Label lbl = new Label();
lbl.setText(data.getXValue());
}
});

You might also like