How can I populate my table view javafx from mysql database - javafx

I keep on searching for an answer but still I can't find the right way to do it (or probably the mistakes in my codes). I am using javafx with scenebuilder. mySQL is the database that I am using. help please :(
package cedproject;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import java.sql.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
/**
* FXML Controller class
*
* #author My Lightstream
*/
public class FXMLController implements Initializable {
public java.sql.PreparedStatement ps;
public java.sql.ResultSet rs;
#FXML
private AnchorPane donationWindow;
//#FXML
//private TableView<?> tblView;
#FXML
TableView<userdata> tblView= new TableView<>();
#FXML
private TableColumn<?, ?> colOR;
#FXML
private TableColumn<?, ?> colAmount;
#FXML
private Button btnNew;
#FXML
private TextField txtSearch;
private static java.sql.Connection con;
private static Statement stat;
private PreparedStatement prep;
private ObservableList<userdata> data;
#FXML
private Button btnLoad;
#FXML
private Group gp;
// </userdata>
/**
#FXML
private Button btnNew;
#FXML
private AnchorPane donationWindow;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
try{
ps = new Connect().connectDatabase();
ps.execute();
tblView.getItems().setAll(this.data);
}catch(Exception e){
}
lagay();
}
public void lagay(){
try{
String query = "select * from receipt";
tblView.getItems().clear();
ps = con.prepareStatement("select * from receipt");
rs = ps.executeQuery(query);
}catch(Exception e){
System.out.println("mali ka shunga");
}
ObservableList<userdata> data = FXCollections.observableArrayList();
try{
while(rs.next()){
data.add(new userdata(
rs.getInt("ORNUM"),
rs.getInt("AMOUNT")
));}
}catch(Exception e){}
}
public void tableView()throws Exception{
tblView.getItems().clear();
rs = ps.executeQuery("SELECT ORNUM,AMOUNT FROM RECEIPT");
ObservableList<userdata> data = FXCollections.observableArrayList();
TableColumn column1 = new TableColumn("ORNUM");
column1.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<>("ornum"));
TableColumn column2 = new TableColumn("Amount");
column2.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<>("amount"));
tblView.getColumns().addAll(column1,column2);
tblView.getChildrenUnmodifiable();
}
#FXML
public void handleButtonAction2(ActionEvent event) {
}
#FXML
public void activated(MouseEvent event) {
}
private void onClicked(ActionEvent event) throws SQLException {
/* tblView.getItems().clear();
rs = ps.executeQuery("SELECT ORNUM,AMOUNT FROM RECEIPT");
ObservableList<userdata> data = FXCollections.observableArrayList();
TableColumn column1 = new TableColumn("ORNUM");
column1.setCellValueFactory(new PropertyValueFactory<>("ORNUM"));
TableColumn column2 = new TableColumn("Amount");
column2.setCellValueFactory(new PropertyValueFactory<>("Amount"));
tblView.getColumns().addAll(column1,column2);*/
ObservableList<userdata> data = FXCollections.observableArrayList();
try{ String query = "select * from receipt";
tblView.getItems().clear();
ps = con.prepareStatement(query);
rs = ps.executeQuery("SELECT * from receipt");
while(rs.next()){
tblView.getItems().add(new userdata(
rs.getInt("ORNUM"),
rs.getInt("AMOUNT")
));
tblView.setItems(data);
}
ps.close();
rs.close();
}catch(Exception e){
}
}
#FXML
private void onClicked(MouseEvent event) throws Exception {
System.out.println("gawin mo");
ObservableList<userdata> data = FXCollections.observableArrayList();
System.out.println("ginawa");
tableView();
try{
String query = "select * from receipt";
tblView.getItems().clear();
ps = new Connect().connectDatabase();
ps = new Connect().connectDatabase();
rs = ps.executeQuery(query);
while(rs.next()){
tblView.getItems().add(new userdata(
rs.getInt("ORNUM"),
rs.getInt("AMOUNT")
));
tblView.setItems(data);
}
}catch(Exception e){
System.out.print("asdqweasd");
}
}
/**
*
* #param event
*/
/* #FXML
public void konekplis(SortEvent<C> event) {
}*/
}
package cedproject;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
/**
*enter code here
* #author My Lightstream
*/
public class userdata {
public SimpleIntegerProperty Ornum;
public SimpleIntegerProperty Amount;
public userdata(Integer ornum, Integer amount) {
this.Ornum = new SimpleIntegerProperty(ornum);
this.Amount = new SimpleIntegerProperty(amount);
}
userdata(String string) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
userdata(String string, String string0, String string1, String string2, String string3, String string4) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public Integer getOrnum() {
return Ornum.get();
}
public Integer getAmount() {
return Amount.get();
}
public void setOrnum(Integer ornum) {
this.Ornum.set(ornum);
}
public void setAmount(Integer amount) {
this.Amount.set(amount);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" fx:id="donationWindow" onDragDetected="#activated" prefHeight="482.0" prefWidth="696.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cedproject.FXMLController">
<stylesheets>
<URL value="#fxml.css" />
</stylesheets>
<children>
<AnchorPane layoutY="8.0" prefHeight="482.0" prefWidth="696.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<TableView fx:id="tblView" layoutX="21.0" layoutY="87.0" prefHeight="181.0" prefWidth="655.0">
<columns>
<TableColumn fx:id="colOR" prefWidth="326.0" text="OR Number" />
<TableColumn fx:id="colAmount" prefWidth="325.0" text="Amount" />
</columns>
</TableView>
<Button fx:id="btnNew" layoutX="500.0" layoutY="35.0" mnemonicParsing="false" onAction="#handleButtonAction2" prefHeight="25.0" prefWidth="78.0" text="NEW" />
<Button layoutX="587.0" layoutY="35.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="78.0" text="EDIT" />
<TextField fx:id="txtSearch" layoutX="40.0" layoutY="35.0" prefHeight="25.0" prefWidth="204.0" promptText="Search" />
<TextArea editable="false" layoutX="19.0" layoutY="336.0" prefHeight="132.0" prefWidth="655.0">
<effect>
<DropShadow height="14.0" radius="6.5" width="14.0" />
</effect>
</TextArea>
<Label layoutX="26.0" layoutY="305.0" prefHeight="17.0" prefWidth="68.0" text="Description:" />
<Button fx:id="btnLoad" layoutX="424.0" layoutY="35.0" mnemonicParsing="false" onMouseClicked="#onClicked" prefHeight="25.0" prefWidth="68.0" text="Load" />
<Group fx:id="gp" />
</children>
<cursor>
<Cursor fx:constant="DEFAULT" />
</cursor>
</AnchorPane>
</children>
</AnchorPane>
Please help :(

Related

How to add TableView footer in JavaFx TableView

I am stuck on how to add table footer or column footer in JavaFX TableView. I am looking to add a TableView which will show purchased items with quantities and sells price in columns and total items count and total sum at the TableView footer. I looked at various resources, but could not find footer property associated with TableView. Any idea how to do it?
Model Class
package javafxapplication8;
public class TestModel {
private String itemName = null;
private int pricePerUnit = 0;
private double quantity = 0.0;
private double amount = 0.0;
public TestModel() {
}
public TestModel(String argitemName, int argpricePerUnit, double argquantity, double argamount) {
itemName = argitemName;
pricePerUnit = argpricePerUnit;
quantity = argquantity;
amount = argamount;
}
public void setItemName(String argitemName) {
itemName = argitemName;
}
public void setPricePerUnit(int argpricePerUnit) {
pricePerUnit = argpricePerUnit;
}
public void setQuantity(double argquantity) {
quantity = argquantity;
}
public void setAmount(double argamount) {
amount = argamount;
}
public String getItemName() {
return itemName;
}
public int getPricePerUnit() {
return pricePerUnit;
}
public double getQuantity() {
return quantity;
}
public double getAmount() {
return amount;
}
#Override
public String toString() {
return this.itemName + "" + this.pricePerUnit + "" + this.quantity + "" + this.amount;
}
}
XML Code
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<AnchorPane id="AnchorPane" fx:id="anchor" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication8.TVCTestModel">
<children>
<VBox prefHeight="564.0" prefWidth="857.0">
<children>
<HBox alignment="BOTTOM_LEFT" prefHeight="100.0" prefWidth="1613.0" spacing="20.0" />
<BorderPane prefHeight="695.0" prefWidth="1618.0">
<center>
<VBox prefHeight="544.0" prefWidth="772.0">
<children>
<HBox prefHeight="65.0" prefWidth="1618.0" />
<HBox prefHeight="426.0" prefWidth="857.0">
<children>
<HBox alignment="CENTER" prefHeight="225.0" prefWidth="857.0">
<children>
<TableView fx:id="myTableView" prefHeight="419.0" prefWidth="816.0">
<columns>
<TableColumn fx:id="itemName" prefWidth="200.0" text="Item Name" />
<TableColumn fx:id="pricePerUnit" prefWidth="200.0" text="Price Per Unit" />
<TableColumn fx:id="quantity" prefWidth="200.0" text="Quantity" />
<TableColumn fx:id="amount" prefWidth="200.0" text="Amount" />
</columns>
</TableView>
</children>
</HBox>
</children>
</HBox>
</children>
</VBox>
</center>
<bottom>
</bottom>
</BorderPane>
</children>
</VBox>
</children>
</AnchorPane>
Controller Class
package javafxapplication8;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
public class TVCTestModel implements Initializable {
#FXML
private TableColumn<TestModel, String> itemName;
#FXML
private TableColumn<TestModel, Integer> pricePerUnit;
#FXML
private TableColumn<TestModel, Double> quantity;
#FXML
private TableColumn<TestModel, Double> amount;
#FXML
private TableView<TestModel> myTableView;
public ObservableList<TestModel> objList = FXCollections.observableArrayList();
#FXML
private AnchorPane anchor;
private static TestModel curTestModel;
#Override
public void initialize(URL url, ResourceBundle rb) {
this.itemName.setCellValueFactory(new PropertyValueFactory<>("itemName"));
this.pricePerUnit.setCellValueFactory(new PropertyValueFactory<>("pricePerUnit"));
this.quantity.setCellValueFactory(new PropertyValueFactory<>("quantity"));
this.amount.setCellValueFactory(new PropertyValueFactory<>("amount"));
objList.add(new TestModel("Item 1", 10, 4, 400));
objList.add(new TestModel("Item 2", 20, 5, 1000));
objList.add(new TestModel("Item 3", 30, 6, 1800));
objList.add(new TestModel("Item 4", 400, 7, 2800));
System.out.println(objList.size());
myTableView.setItems(objList);
}
}
MainMethod Class
package javafxapplication8;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JavaFXApplication8 extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("TVCTestModel.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException ex) {
Logger.getLogger(JavaFXApplication8.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
launch(args);
}
}
Your updated question was reopened, and the image provided suggests that, instead of a footer, you want two summary fields. As your table is not editable, a simple approximation is illustrated here—add two labels to the view, and iterate the table's model in the controller to establish the localized result:
TVCTestModel.fxml:
…
<bottom>
<HBox alignment="CENTER_RIGHT" style="-fx-spacing: 5px;">
<children>
<Label fx:id="labelQ"/>
<Label fx:id="labelA"/>
</children>
</HBox>
</bottom>
…
TVCTestModel.java
#FXML private Label labelQ;
#FXML private Label labelA;
#Override
public void initialize(URL url, ResourceBundle rb) {
…
double sumQuantity = 0;
double sumAmout = 0;
for (TestModel o : objList) {
sumQuantity += o.getQuantity();
sumAmout += o.getAmount();
}
labelQ.setText("Quantity: "
+ NumberFormat.getNumberInstance().format(sumQuantity));
labelA.setText("Amount: "
+ NumberFormat.getCurrencyInstance().format(sumAmout));
}
If you later decide to make your table editable, as shown here, you should consider these modifications:
Migrate to observable properties in your model class, as shown here.
Create your ObservableList model with an extractor, as shown here and here; your extractor would include properties for quantity and amount; your controller could then update the summary field in a ListChangeListener.

JavaFX with fxml TableView updating some columns but not others [duplicate]

This question already has answers here:
The table cells are empty in my tableview. JavaFX + Scenebuilder
(2 answers)
Closed 4 years ago.
In my application Some of the columns in my table are populating like they should be the columns labeled Name Priority and BurstTime have the correct values being added but for some reason the other two ProcessID and State do not update with the values I am adding to my ArrayList.
It looks like my code should be working can anybody see something I have missed?
Here is my controller
package application;
import java.net.URL;
import java.util.ResourceBundle;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
public class Controller implements Initializable {
private ArrayList<String> buf = new ArrayList<>();
protected ArrayList<PCB> array = new ArrayList<>();
protected ArrayList<Process> arrayP = new ArrayList<>();
ObservableList<Process> processData = FXCollections.observableArrayList();
#FXML
private Button SubmitButton;
#FXML
private Button LoadButton;
#FXML
private TextArea textArea;
#FXML
private TextField inputBox;
#FXML
private TableView<Process> ProcessTable;
#FXML
private TableColumn<Process, String> processIDP;
#FXML
private TableColumn<Process, String> processTypeP;
#FXML
private TableColumn<Process, String> priorityCodeP;
#FXML
private TableColumn<Process, String> burstTimeP;
#FXML
private TableColumn<Process, String> StatusCodeP;
#Override
public void initialize(URL url, ResourceBundle rb) {
processIDP.setCellValueFactory(new PropertyValueFactory<Process, String>("processIDP"));
processTypeP.setCellValueFactory(new PropertyValueFactory<Process, String>("processTypeP"));
priorityCodeP.setCellValueFactory(new PropertyValueFactory<Process, String>("priorityCodeP"));
burstTimeP.setCellValueFactory(new PropertyValueFactory<Process, String>("burstTimeP"));
StatusCodeP.setCellValueFactory(new PropertyValueFactory<Process, String>("StatusCodeP"));
Process p1 = new Process();
p1.setprocessIDP("22");
p1.setProcessTypeP ("Apname");
p1.setPriorityCodeP("1");
p1.setBurstTimeP ("13");
p1.setstatusCodeP("Tada");
arrayP.add(p1);
ProcessTable.getItems().addAll(arrayP.get(0));
ProcessTable.setItems(FXCollections.observableArrayList(arrayP));
Process p2 = new Process();
p2.setprocessIDP("24");
p2.setProcessTypeP ("Bpname");
p2.setBurstTimeP ("15");
p2.setPriorityCodeP("2");
arrayP.add(p2);
ProcessTable.getItems().addAll(arrayP.get(1));
// edit existing cell ?
arrayP.get(1).setPriorityCodeP("8");
arrayP.get(1).setstatusCodeP("This");
arrayP.get(1).setprocessIDP("TEST");
}
public ObservableList<Process> getProcessData() {
return processData;
}
#FXML
private TextField LoadProgram;
#FXML
private void handleButtonAction() {
textArea.appendText(inputBox.getText() + "\n");
StringTokenizer st1 = new StringTokenizer(inputBox.getText(), " ");
switch(st1.nextToken()) {
// case "proc": proc(); break;
case "mem": textArea.appendText("Memory: " + String.valueOf(Memory.getUsedMemory()) + "/" + String.valueOf(Memory.getTotalMemory()) + "\n"); break;
// case "exe": exe(); break;
// case "reset": reset(); break;
case "load": buf.add(inputBox.getText()) ;
// edit existing cell ?
arrayP.get(1).setPriorityCodeP("9");
ProcessTable.refresh();
break;
case "exit": System.exit(0); break;
case "clear": textArea.clear(); break;
default: break;
}
}
#FXML
private void handleLoadAction() {
File infile = new File("files/" + LoadProgram.getText() + ".txt");
if (infile.exists() == true ) {
textArea.appendText("Loading " + LoadProgram.getText() + "\n");
}
//call to read data here
else {
textArea.appendText("No Program named " + LoadProgram.getText() + " found \n");
}}
public class textLine {
private String infile;
private String cmd, value;
private Scanner input;
public void parseFile(String filename) {
this.infile = "files/" + filename + ".txt";
parseFile();
}
public void addbuf(String textline) {
buf.add(textline);
}
private void parseFile() {
buf.clear();
try {
File file = new File(infile);
if (file.exists() == true)
input = new Scanner(file);
while (input.hasNext()) {
buf.add(input.next());
}
} catch (Exception e) {
e.printStackTrace();
}
input.close();
}
}
}
Here is my class for Procsess
package application;
public class Process {
String processTypeP = "";
String priorityCodeP = "0";
int lineCodeP = 0;
String burstTimeP = "0";
String processIDP = "0";
String StatusCodeP = "0";
public Process (){}
public String getProcessTypeP() {
return processTypeP;
}
public void setProcessTypeP(String processTypeP) {
this.processTypeP = processTypeP;
}
public String getPriorityCodeP() {
return priorityCodeP;
}
public void setPriorityCodeP(String priorityCodeP) {
this.priorityCodeP = priorityCodeP;
}
public int getLineCodeP() {
return lineCodeP;
}
public void setLineCodeP(int lineCodeP) {
this.lineCodeP = lineCodeP;
}
public String getBurstTimeP() {
return burstTimeP;
}
public void setBurstTimeP(String burstTimeP) {
this.burstTimeP = burstTimeP;
}
public String getprocessIDP() {
return processIDP;
}
public void setprocessIDP(String processIDP) {
this.processIDP = processIDP;
}
public String getstatusCodeP() {
return StatusCodeP;
}
public void setstatusCodeP(String StatusCodeP) {
this.StatusCodeP = StatusCodeP;
}
}
Here is my main application
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/Main.fxml"));
// Parent root = FXMLLoader.load(getClass().getResource("/Main.fxml"));
AnchorPane root = (AnchorPane) loader.load(Main.class.getResource("/application/Main.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
and my fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<children>
<TabPane layoutX="4.0" layoutY="4.0" prefHeight="700.0" prefWidth="900.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Processes">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TextField fx:id="inputBox" layoutX="14.0" layoutY="54.0" />
<Button fx:id="SubmitButton" layoutX="109.0" layoutY="94.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Submit" />
<TextArea fx:id="textArea" layoutX="197.0" layoutY="14.0" prefHeight="105.0" prefWidth="493.0" />
<TableView fx:id="ProcessTable" layoutX="36.0" layoutY="157.0" prefHeight="430.0" prefWidth="654.0">
<columns>
<TableColumn fx:id="processIDP" prefWidth="75.0" text="ProccessID" />
<TableColumn fx:id="processTypeP" prefWidth="101.0" text="Name" />
<TableColumn fx:id="priorityCodeP" prefWidth="94.0" text="Priority" />
<TableColumn fx:id="StatusCodeP" prefWidth="119.0" text="State" />
<TableColumn fx:id="burstTimeP" prefWidth="100.0" text="BurstTime" />
</columns>
</TableView>
<TextField fx:id="LoadProgram" layoutX="712.0" layoutY="492.0" />
<Button fx:id="LoadProgramButton" layoutX="725.0" layoutY="531.0" mnemonicParsing="false" onAction="#handleLoadAction" text="Load External Program" />
</children></AnchorPane>
</content>
</Tab>
<Tab text="Scheduler">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab text="Memory">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
Try renaming your getter methods from getprocessIDP and getstatusCodeP to getProcessIDP and getStatusCodeP respectively (note the capital 'S' and 'P'). To avoid problems like this in the future it's a good habit to generate getters, setters and constructors with the IDE instead of doing it manually.

How to show last record in tableview?

Hello when I show the records with the button mostrar, all the records show, but when I am adding a new record with the button agregar the record doesn't show with that button, what I am doing wrong?. The method is good, but it seems that the last record is not visualized in the tableview this is my code, Please help.
The class
package application;
import javafx.collections.*;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
public class Mostraregistros implements Initializable {
ObservableList <cliente> data =FXCollections.observableArrayList();
#FXML TableView<cliente> tablacliente;
#FXML TableColumn<cliente, String> nombrescol;
#FXML TableColumn<cliente,String > apellidoscol;
#FXML TableColumn<cliente, Integer> clienteid;
#FXML private Button mtn;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
mtn.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Informacion");
alert.setHeaderText(null);
alert.setContentText("Mostrando Todos los Registros");
alert.showAndWait();
nombrescol.setCellValueFactory(new PropertyValueFactory <cliente, String>("nombres"));
apellidoscol.setCellValueFactory(new PropertyValueFactory <cliente, String>("apellidos"));
clienteid.setCellValueFactory(new PropertyValueFactory <cliente, Integer>("id_cliente"));
tablacliente.setItems(data);
}
});
Connection conn=null;{
try {
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
Statement mostrar=conn.createStatement();
ResultSet rs;
rs= mostrar.executeQuery("select * from cliente");
while ( rs.next() )
{
data.add(new cliente(
rs.getString("nombre"),
rs.getString("apellido"),
rs.getInt("id")
));
}
if(conn!=null)
System.out.println("conexion exitosa");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
fxml code
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.cell.*?>
<?import application.cliente.*?>
<?import application.Mostraregistros.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="634.0" prefWidth="626.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ConexionSQL">
<children>
<Pane layoutX="5.0" layoutY="1.0" prefHeight="581.0" prefWidth="977.0">
<children>
<TextField fx:id="nm" layoutX="125.0" layoutY="70.0" prefHeight="32.0" prefWidth="205.0" text="nombres" />
<TextField fx:id="ap" layoutX="125.0" layoutY="133.0" prefHeight="32.0" prefWidth="205.0" text="apellidos" />
<Label layoutX="27.0" layoutY="70.0" prefHeight="32.0" prefWidth="137.0" text="NOMBRES" />
<Button fx:id="btn" layoutX="21.0" layoutY="216.0" mnemonicParsing="false" onAction="#btn" prefHeight="43.0" prefWidth="84.0" text="AGREGAR" />
<Label layoutX="27.0" layoutY="141.0" text="APELLIDOS" />
<TableView fx:id="tablacliente" layoutX="346.0" layoutY="47.0" prefHeight="448.0" prefWidth="553.0">
<columns>
<TableColumn fx:id="clienteid" prefWidth="225.0" text="CLIENTE ID"/>
<TableColumn fx:id="nombrescol" prefWidth="206.0" text="NOMBRES"/>
<TableColumn fx:id="apellidoscol" prefWidth="161.0" text="APELLIDOS"/>
</columns>
</TableView>
<Button fx:id="mtn" layoutX="138.0" layoutY="216.0" mnemonicParsing="false" prefHeight="43.0" prefWidth="144.0" text="MOSTRAR REGISTROS" />
</children>
</Pane>
</children>
</AnchorPane>
this is the agregar button code and the insert code
package application;
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.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.input.MouseEvent;
public class ConexionSQL extends Mostraregistros{
#FXML private TextField nm;
#FXML private TextField ap;
#FXML private Button btn;
#FXML
private void btn(ActionEvent event) {
btn.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Informacion");
alert.setHeaderText(null);
alert.setContentText("Registro Insertado Exitosamente");
alert.showAndWait();
}
});
Connection conn=null;
try {
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
Statement insertar=conn.createStatement();
insertar.executeUpdate("insert into cliente (nombre, apellido) values ('"+nm.getText()+"', '"+ap.getText()+"')");
if(conn!=null)
System.out.println("conexion exitosa");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
this is the cliente class
package application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class cliente{
private StringProperty nombres;
private StringProperty apellidos;
private IntegerProperty id_cliente;
public cliente ( String nombres, String apellidos, Integer id_cliente) {
this.nombres= new SimpleStringProperty (nombres);
this.apellidos= new SimpleStringProperty ( apellidos);
this.id_cliente=new SimpleIntegerProperty (id_cliente);
}
public String getNombres() {
return nombres.get();
}
public void setNombres(String nombres) {
this.nombres=new SimpleStringProperty (nombres);
}
public String getApellidos() {
return apellidos.get();
}
public void setApellidos(String apellidos) {
this.apellidos=new SimpleStringProperty ( apellidos);
}
public Integer getId_cliente() {
return id_cliente.get();
}
public void setid_cliente(Integer id_cliente) {
this.id_cliente=new SimpleIntegerProperty (id_cliente);
}
}

JavaFX populating multiple tables in separate Tabs

I am trying to populate tables in separate tabs in TabPane.
I was able to populate only the one in first tab like this:
#FXML
public void initialize(URL url, ResourceBundle rb) {
//assert serviceChoiceBox1 != null : "fx:id=\"serviceChoiceBox1\" was not injected: check your FXML file 'ScheduleServicePane.fxml'.";
ServiceName.setCellValueFactory(new PropertyValueFactory<Service, String>("name"));
ServicePrice.setCellValueFactory(new PropertyValueFactory<Service, Double>("price"));
ServiceDuration.setCellValueFactory(new PropertyValueFactory<Service, Integer>("duration"));
TableView.setItems(List());
}
private ObservableList<Service> List(){
ObservableList<Service> service = FXCollections.observableArrayList(serviceRepository.list());
return service;
}
I have two more tabs with tables but somehow I am not able to fill them with prepared list of data that I have
I want to populate other tabs with similar data
Anyone can help?
I also read about separate Controllers for each tab but this seems to be to much in such a case.
I would use FilteredList in this case. This will allow you to create "sublist" of the original list. In this demo, I create a "type" field in the Service class. I then filter the original list based on the "type". You don't have to create a "type" field, but you will need to find something to filter on if you don't.
Main:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author blj0011
*/
public class JavaFXApplication184 extends Application
{
#Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Controller:
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.function.Predicate;
import javafx.collections.FXCollections;
import javafx.collections.transformation.FilteredList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
/**
*
* #author blj0011
*/
public class FXMLDocumentController implements Initializable
{
#FXML
private TabPane tpMain;
#FXML
private TableView<Service> tvTab1, tvTab2;
#FXML
private TableColumn<Service, String> tcServiceNameTab1, tcServicePriceTab1, tcServiceDurationTab1;
#FXML
private TableColumn<Service, String> tcServiceNameTab2, tcServicePriceTab2, tcServiceDurationTab2;
#FXML
private void handleButtonAction(ActionEvent event)
{
System.out.println("You clicked me!");
}
#Override
public void initialize(URL url, ResourceBundle rb)
{
tcServiceNameTab1.setCellValueFactory(new PropertyValueFactory("name"));
tcServicePriceTab1.setCellValueFactory(new PropertyValueFactory("price"));
tcServiceDurationTab1.setCellValueFactory(new PropertyValueFactory("duration"));
tcServiceNameTab2.setCellValueFactory(new PropertyValueFactory("name"));
tcServicePriceTab2.setCellValueFactory(new PropertyValueFactory("price"));
tcServiceDurationTab2.setCellValueFactory(new PropertyValueFactory("duration"));
//Add sanoke data
List<Service> services = new ArrayList();
services.add(new Service("Manicure 1", "40.0", "60", "Manicure"));
services.add(new Service("Manicure 2", "40.0", "60", "Manicure"));
services.add(new Service("Pedicure 1", "40.0", "60", "Pedicure"));
services.add(new Service("Pedicure 2", "40.0", "60", "Pedicure"));
FilteredList<Service> flTab1 = new FilteredList(FXCollections.observableArrayList(services), getManicures());
FilteredList<Service> flTab2 = new FilteredList(FXCollections.observableArrayList(services), getPedicures());
tvTab1.setItems(flTab1);
tvTab2.setItems(flTab2);
}
Predicate<Service> getManicures()
{
return p -> p.getType().equals("Manicure");
}
Predicate<Service> getPedicures()
{
return p -> p.getType().equals("Pedicure");
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="406.0" prefWidth="369.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="javafxapplication184.FXMLDocumentController">
<children>
<TabPane fx:id="tpMain" layoutX="56.0" layoutY="67.0" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab text="Manicure">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView fx:id="tvTab1" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="tcServiceNameTab1" prefWidth="75.0" text="Service" />
<TableColumn fx:id="tcServicePriceTab1" prefWidth="75.0" text="Price" />
<TableColumn fx:id="tcServiceDurationTab1" prefWidth="75.0" text="Duration" />
</columns>
</TableView>
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="Pedicure">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView fx:id="tvTab2" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="tcServiceNameTab2" prefWidth="75.0" text="Service" />
<TableColumn fx:id="tcServicePriceTab2" prefWidth="75.0" text="Price" />
<TableColumn fx:id="tcServiceDurationTab2" prefWidth="75.0" text="Duration" />
</columns>
</TableView>
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
Service:
/**
*
* #author blj0011
*/
public class Service
{
private String name;
private String price;
private String duration;
private String type;
public Service(String name, String price, String duration, String type)
{
this.name = name;
this.price = price;
this.duration = duration;
this.type = type;
}
/**
* #return the name
*/
public String getName()
{
return name;
}
/**
* #param name the name to set
*/
public void setName(String name)
{
this.name = name;
}
/**
* #return the price
*/
public String getPrice()
{
return price;
}
/**
* #param price the price to set
*/
public void setPrice(String price)
{
this.price = price;
}
/**
* #return the duration
*/
public String getDuration()
{
return duration;
}
/**
* #param duration the duration to set
*/
public void setDuration(String duration)
{
this.duration = duration;
}
/**
* #return the type
*/
public String getType()
{
return type;
}
/**
* #param type the type to set
*/
public void setType(String type)
{
this.type = type;
}
}

JavaFX, initialize setCellValueFactory for TableView with bean and bigDecimal, result -> NullPointerException

i'm new to JavaFX and i want to print some BigDecimal and Bean in TableColumn in my TableView.
The issue is that i get a NullPointerExceptionexception when i try to setItems of the TableView.
Just to be clear i'm using the fxml with scenario builder for the graphic (panel, tableView, etc.) and the java class for link the bean's data to the tableView. I'm using the ObservableList for get the change, and the Bean has the correct getter that return ObjectProperty<BigDecimal> or ObjectProperty<ContoBean>.
I don't know if what i'm using for setCellValueFactory is correct, i've tryed to follow the tutorial here, but in this tutorial the TableColumn are set only as String. I've read some example for do the the setCellValueFactory, but as far as i can see the best procedure is to avoid using PropertyValueFactory and trying to stick to lambda expressions (more safe and keep the data type intact using a cellFactory, correct me if i'm wrong).
My question are:
What's the best practies for use setCellValueFactory of TableColumn with BigDecimal and Bean?
The best way is to create the TableColumns with scenario builder and link them with the controller or create only the TableView, in scenario builder, and create each single TableColumn in the java controller?
If i'm using a Bean that contains other Beans, how the TableColumn will represent the information? Which variable will be used for view the information, all of the Bean's variable or only a single one?
MovimentoOverview.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="900.0" prefWidth="1400.0" stylesheets="#DarkTheme.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="it.evil.money.view.MovimentoOverviewController">
<children>
<SplitPane dividerPositions="0.12857142857142856" prefHeight="300.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<TreeView layoutX="-9.0" layoutY="14.0" prefHeight="891.0" prefWidth="200.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="5.0" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" styleClass="background">
<children>
<Label layoutX="14.0" layoutY="7.0" styleClass="label-header" text="Person Details" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="5.0">
<font>
<Font size="18.0" />
</font></Label>
<ToolBar layoutX="216.0" layoutY="258.0" prefHeight="40.0" styleClass="background" AnchorPane.bottomAnchor="10.0" AnchorPane.rightAnchor="10.0">
<items>
<Button mnemonicParsing="false" onAction="#handleNewMovimento" text="New.." />
<Button mnemonicParsing="false" onAction="#handleEditMovimento" text="Edit.." />
<Button alignment="CENTER_RIGHT" mnemonicParsing="false" onAction="#handleDeleteMovimento" text="Delete.." textAlignment="CENTER" />
</items>
</ToolBar>
<TableView layoutX="5.0" layoutY="55.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="100.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="60.0">
<columns>
<TableColumn fx:id="idColumn" prefWidth="75.0" text="ID" />
<TableColumn fx:id="contoColumn" prefWidth="75.0" text="Conto" />
<TableColumn fx:id="dataColumn" prefWidth="75.0" text="Data" />
<TableColumn fx:id="valutaColumn" prefWidth="75.0" text="Valuta" />
<TableColumn fx:id="importoColumn" prefWidth="75.0" text="Importo" />
<TableColumn fx:id="quotaTicketColumn" prefWidth="75.0" text="Quota Ticket" />
<TableColumn fx:id="causaleColumn" prefWidth="75.0" text="Causale" />
<TableColumn fx:id="noteColumn" prefWidth="75.0" text="Note" />
<TableColumn fx:id="tipoPagamentoColumn" prefWidth="75.0" text="Tipo Pagamento" />
<TableColumn fx:id="tipoMovimentoColumn" prefWidth="75.0" text="Tipo Movimento" />
<TableColumn fx:id="spesaInComuneColumn" prefWidth="75.0" text="Spesa in Comune" />
<TableColumn fx:id="spesaPerLavoroColumn" prefWidth="75.0" text="Spesa per Lavoro" />
<TableColumn fx:id="utenteColumn" prefWidth="75.0" text="Utente" />
<TableColumn fx:id="tagColumn" prefWidth="75.0" text="Tag" />
<TableColumn fx:id="bustaPagaColumn" prefWidth="75.0" text="Busta Paga" />
</columns>
</TableView>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
MovimentoOverviewController
package it.evil.money.view;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import java.math.BigDecimal;
import java.time.LocalDate;
import it.evil.money.MainApp;
import it.evil.money.model.BustaPagaBean;
import it.evil.money.model.CausaleBean;
import it.evil.money.model.ContoBean;
import it.evil.money.model.MovimentoBean;
import it.evil.money.model.TagBean;
import it.evil.money.model.TipoMovimentoBean;
import it.evil.money.model.TipoPagamentoBean;
import it.evil.money.model.UtenteBean;
import it.evil.money.util.DateUtil;
public class MovimentoOverviewController {
#FXML
private TableView<MovimentoBean> movimentoTable;
#FXML
private TableColumn<MovimentoBean, BigDecimal> idColumn;
#FXML
private TableColumn<MovimentoBean, ContoBean> contoColumn;
#FXML
private TableColumn<MovimentoBean, LocalDate> dataColumn;
#FXML
private TableColumn<MovimentoBean, String> valutaColumn;
#FXML
private TableColumn<MovimentoBean, BigDecimal> importoColumn;
#FXML
private TableColumn<MovimentoBean, BigDecimal> quotaTicketColumn;
#FXML
private TableColumn<MovimentoBean, CausaleBean> causaleColumn;
#FXML
private TableColumn<MovimentoBean, String> noteColumn;
#FXML
private TableColumn<MovimentoBean, TipoPagamentoBean> tipoPagamentoColumn;
#FXML
private TableColumn<MovimentoBean, TipoMovimentoBean> tipoMovimentoColumn;
#FXML
private TableColumn<MovimentoBean, Boolean> spesaInComuneColumn;
#FXML
private TableColumn<MovimentoBean, Boolean> spesaPerLavoroColumn;
#FXML
private TableColumn<MovimentoBean, BustaPagaBean> bustaPagaColumn;
#FXML
private TableColumn<MovimentoBean, UtenteBean> utenteColumn;
#FXML
private TableColumn<MovimentoBean, TagBean> tagColumn;
#FXML
private Label idLabel;
#FXML
private Label contoLabel;
#FXML
private Label dataLabel;
#FXML
private Label valutaLabel;
#FXML
private Label importoLabel;
// Reference to the main application.
private MainApp mainApp;
public MovimentoOverviewController() {
}
#FXML
private void initialize() {
idColumn.setCellValueFactory(cellData -> cellData.getValue().idProperty() );
contoColumn.setCellValueFactory(cellData -> cellData.getValue().contoProperty() );
dataColumn.setCellValueFactory(cellData -> cellData.getValue().dataProperty() );
valutaColumn.setCellValueFactory(cellData -> cellData.getValue().valutaProperty() );
importoColumn.setCellValueFactory(cellData -> cellData.getValue().importoProperty() );
quotaTicketColumn.setCellValueFactory(cellData -> cellData.getValue().quotaTicketProperty() );
causaleColumn.setCellValueFactory(cellData -> cellData.getValue().causaleProperty() );
noteColumn.setCellValueFactory(cellData -> cellData.getValue().noteProperty() );
tipoPagamentoColumn.setCellValueFactory(cellData -> cellData.getValue().tipoPagamentoProperty() );
tipoMovimentoColumn.setCellValueFactory(cellData -> cellData.getValue().tipoMovimentoProperty() );
spesaInComuneColumn.setCellValueFactory(cellData -> cellData.getValue().spesaInComuneProperty() );
spesaPerLavoroColumn.setCellValueFactory(cellData -> cellData.getValue().spesaPerLavoroProperty() );
bustaPagaColumn.setCellValueFactory(cellData -> cellData.getValue().bustaPagaProperty() );
utenteColumn.setCellValueFactory(cellData -> cellData.getValue().utenteProperty() );
tagColumn.setCellValueFactory(cellData -> cellData.getValue().tagProperty() );
}
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
movimentoTable.setItems(mainApp.getMovimentoData());
}
}
MainApp
package it.evil.money;
import java.io.IOException;
import it.evil.money.dao.DatabaseManager;
import it.evil.money.dao.MovimentoDao;
import it.evil.money.model.*;
import it.evil.money.view.*;
import javafx.application.Application;
import javafx.collections.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private ObservableList<MovimentoBean> movimentoData = FXCollections.observableArrayList();
private MovimentoDao movimentoDao;
public MainApp() {
movimentoDao = new MovimentoDao(DatabaseManager.getConnection());
// Instead of use a empty bean...
// movimentoData.add(new MovimentoBean());
// I get the data for db
movimentoData.addAll(this.movimentoDao.getAll());
this.movimentoDao.distruttore();
}
public ObservableList<MovimentoBean> getMovimentoData() {
return movimentoData;
}
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("AddressApp");
this.primaryStage.getIcons().add(new Image("file:resources/images/address_book_32.png"));
initRootLayout();
showMovimentoOverview();
}
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void showMovimentoOverview() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/MovimentoOverview.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
rootLayout.setCenter(personOverview);
MovimentoOverviewController controller = loader.getController();
controller.setMainApp(this);
} catch (IOException e) {
e.printStackTrace();
}
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
MovimentoBean
package it.evil.money.model;
import java.time.LocalDate;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import java.math.BigDecimal;
public class MovimentoBean {
private final ObjectProperty<BigDecimal> id;
private final ObjectProperty<ContoBean> conto;
private final ObjectProperty<LocalDate> data;
private final StringProperty valuta;
private final ObjectProperty<BigDecimal> importo;
private final ObjectProperty<BigDecimal> quotaTicket;
private final ObjectProperty<CausaleBean> causale;
private final StringProperty note;
private final ObjectProperty<TipoPagamentoBean> tipoPagamento;
private final ObjectProperty<TipoMovimentoBean> tipoMovimento;
private final BooleanProperty spesaInComune;
private final BooleanProperty spesaPerLavoro;
private final ObjectProperty<BustaPagaBean> bustaPaga;
private final ObjectProperty<UtenteBean> utente;
private final ObjectProperty<TagBean> tag;
public MovimentoBean() {
this(null, null, null, null, null, null, null, null, null, null, false, false, null, null, null);
}
public MovimentoBean(BigDecimal id, ContoBean conto, LocalDate data, String valuta, BigDecimal importo,
BigDecimal quotaTicket, CausaleBean causale, String note, TipoPagamentoBean tipoPagamento,
TipoMovimentoBean tipoMovimento, boolean spesaInComune, boolean spesaPerLavoro, BustaPagaBean bustaPaga,
UtenteBean utente, TagBean tag) {
this.id = new SimpleObjectProperty<BigDecimal>(id);
this.conto = new SimpleObjectProperty<ContoBean>(conto);
this.data = new SimpleObjectProperty<LocalDate>(data);
this.valuta = new SimpleStringProperty(valuta);
this.importo = new SimpleObjectProperty<BigDecimal>(importo);
this.quotaTicket = new SimpleObjectProperty<BigDecimal>(quotaTicket);
this.causale = new SimpleObjectProperty<CausaleBean>(causale);
this.note = new SimpleStringProperty(note);
this.tipoPagamento = new SimpleObjectProperty<TipoPagamentoBean>(tipoPagamento);
this.tipoMovimento = new SimpleObjectProperty<TipoMovimentoBean>(tipoMovimento);
this.spesaInComune = new SimpleBooleanProperty(spesaInComune);
this.spesaPerLavoro = new SimpleBooleanProperty(spesaPerLavoro);
this.bustaPaga = new SimpleObjectProperty<BustaPagaBean>(bustaPaga);
this.utente = new SimpleObjectProperty<UtenteBean>(utente);
this.tag = new SimpleObjectProperty<TagBean>(tag);
}
public BigDecimal getId(){
return this.id.get();
}
public void setId(BigDecimal id){
this.id.set(id);
}
public ObjectProperty<BigDecimal> idProperty(){
return id;
}
......
When i start the program i get this error:
Exception in Application start method
java.lang.reflect.InvocationTargetException
.......
Caused by: java.lang.NullPointerException
at it.evil.money.view.MovimentoOverviewController.setMainApp(MovimentoOverviewController.java )
at it.evil.money.MainApp.showMovimentoOverview(MainApp.java )
at it.evil.money.MainApp.start(MainApp.java )

Resources