javafx tableview empty table [duplicate] - javafx

I tried all to populate a TableView with data. The next code inserts a new row in table but the data not appear the table. I tried to find an explication for this without success.
Please help. I can't what is wrong.
In controller.java
#FXML private TableView<TempTableData> tempTable;
#FXML private TableColumn<TempTableData,String> columnTime;
#FXML private TableColumn<TempTableData,Float> columnTempOne;
#FXML private TableColumn<TempTableData,Float> columnTempTwo;
#FXML private TableColumn<TempTableData,Float> columnTempThree;
#FXML protected void initialize() {
columnTime = new TableColumn<TempTableData,String>();
columnTime.setCellValueFactory(
new PropertyValueFactory<TempTableData,String>("Time"));
columnTempOne = new TableColumn<TempTableData,Float>();
columnTempOne.setCellValueFactory(
new PropertyValueFactory<TempTableData,Float>("Temp 1"));
columnTempTwo = new TableColumn<TempTableData,Float>();
columnTempTwo.setCellValueFactory(
new PropertyValueFactory<TempTableData,Float>("Temp 2"));
columnTempThree = new TableColumn<TempTableData,Float>();
columnTempThree.setCellValueFactory(
new PropertyValueFactory<TempTableData,Float>("Temp 3"));
tempDataList = FXCollections.observableArrayList();
tempDataList.add(new TempTableData("0",3.0f, 4f, 5f));
tempTable.setItems(tempDataList);
}
TempTableData.java
public class TempTableData {
private final SimpleStringProperty time;
private final SimpleFloatProperty dataSensorOne;
private final SimpleFloatProperty dataSensorTwo;
private final SimpleFloatProperty dataSensorThree;
public TempTableData(String time, float dataSensorOne, float dataSensorTwo, float dataSensorThree){
this.time = new SimpleStringProperty(time);
this.dataSensorOne = new SimpleFloatProperty(dataSensorOne);
this.dataSensorTwo = new SimpleFloatProperty(dataSensorTwo);
this.dataSensorThree = new SimpleFloatProperty(dataSensorThree);
}
public String getTime() {
return time.get();
}
public void setTime(String time) {
this.time.set(time);
}
public float getDataSensorOne() {
return dataSensorOne.get();
}
public void setDataSensorOne(float dataSensorOne) {
this.dataSensorOne.set(dataSensorOne);
}
public float getDataSensorTwo() {
return dataSensorTwo.get();
}
public void setDataSensorTwo(float dataSensorTwo) {
this.dataSensorTwo.set(dataSensorTwo);
}
public float getDataSensorThree() {
return dataSensorThree.get();
}
public void setDataSensorThree(float dataSensorThree) {
this.dataSensorThree.set(dataSensorThree);
}
public String toString(){
String string = String.format("[time: %s | dataSensorOne: %f |dataSensorTwo: %f |dataSensorThree: %f ]",
time.get(), dataSensorOne.get(), dataSensorTwo.get(), dataSensorThree.get());
return string;
}
}

why you creating table columns again ? ,as they already created with #FXML annotation (injection!).
remove column instance creation lines from your code and everything will be fine
// remove these lines
columnTime = new TableColumn<TempTableData,String>();
columnTempTwo = new TableColumn<TempTableData,Float>();
columnTempThree = new TableColumn<TempTableData,Float>();

make sure your cell factory values are spelt exactly as the corresponding model class values.
e.g.
private final .... SimpleStringProperty time;
and
new ...... PropertyValueFactory("time"));

Related

Tableview in JavaFX is loading data twice after adding new data in Tableview

I'm working on an application for our warehouse. I've got a Tableview reading data form an SQL database. There's an Add button that allows to enter new products into the Tableview.
The Tableview loads just fine when started, but when I add a new product, the product adds just fine only the Tableview loads everything again. This gives double entries in the Tableview. Looking at the Database table in DBeaver, everything is correct (no double entries there and the new product is added).
In my previous version (before using JavaFx) I would add a method to the Add functionality which allows the Tableview to refresh itself. This worked fine. Now i'm working with JavaFX and this same principle is giving me this problem.
Here's the Load Method:
private void LoadPickTableMethod() {
String sql3 = "SELECT * FROM PICKLOCATIES";
try {
Connection con = ConnectDatabase.conDB();
ResultSet rs = con.createStatement().executeQuery(sql3);
while (rs.next()) {
picklocaties.add(new TableModel(rs.getString("ID"),
rs.getString("LOCATIE"),
rs.getString("ARTIKELCODE"),
rs.getString("OMSCHRIJVING"),
rs.getString("EENHEID"),
rs.getString("HAL"),
rs.getString("CODE"),
rs.getString("AANTAL"),
rs.getString("INITIAAL"),
rs.getString("MAX")));
}
} catch (SQLException ex) {
Logger.getLogger(ZoekenFXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
col_id2.setCellValueFactory(new PropertyValueFactory<>("Id"));
col_locatie2.setCellValueFactory(new PropertyValueFactory<>("Locatie"));
col_artikelcode2.setCellValueFactory(new PropertyValueFactory<>("Artikelcode"));
col_omschrijving2.setCellValueFactory(new PropertyValueFactory<>("Omschrijving"));
col_eenheid2.setCellValueFactory(new PropertyValueFactory<>("Eenheid"));
col_hal1.setCellValueFactory(new PropertyValueFactory<>("Hal"));
col_oescode.setCellValueFactory(new PropertyValueFactory<>("Code"));
col_initiaalP.setCellValueFactory(new PropertyValueFactory<>("Initiaal"));
col_aantalP.setCellValueFactory(new PropertyValueFactory<>("Aantal"));
col_max.setCellValueFactory(new PropertyValueFactory<>("Max"));
searchTable1.setItems(picklocaties);
col_id.setCellValueFactory(new PropertyValueFactory<>("Id"));
col_locatie.setCellValueFactory(new PropertyValueFactory<>("Locatie"));
col_artikelcode.setCellValueFactory(new PropertyValueFactory<>("Artikelcode"));
col_omschrijving.setCellValueFactory(new PropertyValueFactory<>("Omschrijving"));
col_eenheid.setCellValueFactory(new PropertyValueFactory<>("Eenheid"));
col_hal.setCellValueFactory(new PropertyValueFactory<>("Hal"));
col_oescode1.setCellValueFactory(new PropertyValueFactory<>("Code"));
col_initiaalP1.setCellValueFactory(new PropertyValueFactory<>("Initiaal"));
col_aantalP1.setCellValueFactory(new PropertyValueFactory<>("Aantal"));
col_max1.setCellValueFactory(new PropertyValueFactory<>("Max"));
searchTable.setItems(picklocaties);
}
This method is called at the beginning (initialize), but also after clicking the Add button.
The Add method:
#FXML
private void AddPick(ActionEvent event) {
try {
String sql = "INSERT INTO PICKLOCATIES"
+ "(artikelcode, locatie, omschrijving, eenheid,code,hal,aantal, initiaal, max)"
+ "VALUES (?,?,?,?,?,?,?,?,?)";
conn = ConnectDatabase.conDB();
pst = conn.prepareStatement(sql);
pst.setString(1, autocompleteTf.getText());
pst.setString(3, pickDescTxt.getText());
pst.setString(2, autocompleteLoc.getText());
pst.setString(4, pickEenheidTxt.getText());
pst.setString(5, oesTxt.getText());
pst.setString(6, autocompleteHal.getText());
pst.setString(7, pickAantalTxt.getText());
pst.setString(8, pickInitiaalTxt.getText());
pst.setString(9, maxAantalTxt.getText());
pst.executeUpdate();
Alert dialog = new Alert(Alert.AlertType.INFORMATION);
dialog.setContentText("Artikel Toegevoegd");
dialog.setHeaderText("Informatie");
dialog.showAndWait();
ClearAllFields();
LoadPickTableMethod();
} catch (Exception e) {
Logger.getLogger(LoginFXMLController.class.getName()).log(Level.SEVERE, null, e);
}
}
Screenshot of what happens:
Screenshot Tableview
The last row is the newly added product (which loads only once) but the above rows are duplicates. The first 4 rows are the original ones, and the 4 below that are duplicates.
The goal is to have the Tableview update itself with the newly added product and not to duplicate itself along with it.
Any help on this would be greatly appreciated.
Edit: as per request, minimal reproducable example:
public class HomeFXMLController implements Initializable {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
#FXML
private TableView<TableModel> searchTable1;
#FXML
private TableColumn<TableModel, String> col_id2;
#FXML
private TableColumn<TableModel, String> col_locatie2;
#FXML
private TableColumn<TableModel, String> col_artikelcode2;
#FXML
private TableColumn<TableModel, String> col_omschrijving2;
#FXML
private TableColumn<TableModel, String> col_eenheid2;
#FXML
private TableColumn<TableModel, String> col_hal1;
#FXML
private TableColumn<TableModel, String> col_oescode;
#FXML
private TableColumn<TableModel, String> col_aantalP;
#FXML
private TableColumn<TableModel, String> col_initiaalP;
#FXML
private TableColumn<TableModel, String> col_max;
#FXML
private JFXTextField pickEenheidTxt;
#FXML
private JFXTextArea pickDescTxt;
#FXML
private JFXTextField pickIdTxt;
#FXML
private JFXTextField oesTxt;
#FXML
private JFXButton pickAddBtn;
#FXML
private JFXTextField maxAantalTxt;
#FXML
private JFXTextField autocompleteTf;
#FXML
private JFXTextField autocompleteLoc;
#FXML
private JFXTextField autocompleteHal;
ObservableList<TableModel> picklocaties = FXCollections.observableArrayList();
FilteredList filtered = new FilteredList(picklocaties, e -> true);
#Override
public void initialize(URL url, ResourceBundle rb) {
LoadPickTableMethod();
#FXML //this bit is linked to pickAddBtn
private void AddPick(ActionEvent event) {
try {
String sql = "INSERT INTO PICKLOCATIES"
+ "(artikelcode, locatie, omschrijving, eenheid,code,hal,aantal, initiaal, max)"
+ "VALUES (?,?,?,?,?,?,?,?,?)";
conn = ConnectDatabase.conDB();
pst = conn.prepareStatement(sql);
pst.setString(1, autocompleteTf.getText());
pst.setString(3, pickDescTxt.getText());
pst.setString(2, autocompleteLoc.getText());
pst.setString(4, pickEenheidTxt.getText());
pst.setString(5, oesTxt.getText());
pst.setString(6, autocompleteHal.getText());
pst.setString(7, pickAantalTxt.getText());
pst.setString(8, pickInitiaalTxt.getText());
pst.setString(9, maxAantalTxt.getText());
pst.executeUpdate();
Alert dialog = new Alert(Alert.AlertType.INFORMATION);
dialog.setContentText("Artikel Toegevoegd");
dialog.setHeaderText("Informatie");
dialog.showAndWait();
ClearAllFields();
LoadPickTableMethod();
} catch (Exception e) {
Logger.getLogger(LoginFXMLController.class.getName()).log(Level.SEVERE, null, e);
}
}
private void LoadPickTableMethod() {
String sql3 = "SELECT * FROM PICKLOCATIES";
try {
Connection con = ConnectDatabase.conDB();
ResultSet rs = con.createStatement().executeQuery(sql3);
while (rs.next()) {
picklocaties.add(new TableModel(rs.getString("ID"),
rs.getString("LOCATIE"),
rs.getString("ARTIKELCODE"),
rs.getString("OMSCHRIJVING"),
rs.getString("EENHEID"),
rs.getString("HAL"),
rs.getString("CODE"),
rs.getString("AANTAL"),
rs.getString("INITIAAL"),
rs.getString("MAX")));
}
} catch (SQLException ex) {
Logger.getLogger(ZoekenFXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
col_id2.setCellValueFactory(new PropertyValueFactory<>("Id"));
col_locatie2.setCellValueFactory(new PropertyValueFactory<>("Locatie"));
col_artikelcode2.setCellValueFactory(new PropertyValueFactory<>("Artikelcode"));
col_omschrijving2.setCellValueFactory(new PropertyValueFactory<>("Omschrijving"));
col_eenheid2.setCellValueFactory(new PropertyValueFactory<>("Eenheid"));
col_hal1.setCellValueFactory(new PropertyValueFactory<>("Hal"));
col_oescode.setCellValueFactory(new PropertyValueFactory<>("Code"));
col_initiaalP.setCellValueFactory(new PropertyValueFactory<>("Initiaal"));
col_aantalP.setCellValueFactory(new PropertyValueFactory<>("Aantal"));
col_max.setCellValueFactory(new PropertyValueFactory<>("Max"));
searchTable1.setItems(picklocaties);
col_id.setCellValueFactory(new PropertyValueFactory<>("Id"));
col_locatie.setCellValueFactory(new PropertyValueFactory<>("Locatie"));
col_artikelcode.setCellValueFactory(new PropertyValueFactory<>("Artikelcode"));
col_omschrijving.setCellValueFactory(new PropertyValueFactory<>("Omschrijving"));
col_eenheid.setCellValueFactory(new PropertyValueFactory<>("Eenheid"));
col_hal.setCellValueFactory(new PropertyValueFactory<>("Hal"));
col_oescode1.setCellValueFactory(new PropertyValueFactory<>("Code"));
col_initiaalP1.setCellValueFactory(new PropertyValueFactory<>("Initiaal"));
col_aantalP1.setCellValueFactory(new PropertyValueFactory<>("Aantal"));
col_max1.setCellValueFactory(new PropertyValueFactory<>("Max"));
searchTable.setItems(picklocaties);
}
private void ClearAllFields() {
autocompleteTf.setText("");
pickDescTxt.setText("");
pickEenheidTxt.setText("");
autocompleteHal.setText("");
autocompleteLoc.setText("");
oesTxt.setText("");
pickAantalTxt.setText("");
pickInitiaalTxt.setText("");
maxAantalTxt.setText("");
}
}
The Table Model:
package voorraadsysteem;
public class TableModel {
public final String ID, Locatie, Artikelcode, Omschrijving,
Eenheid,
Hal, Code, Aantal, Initiaal, Max;
public TableModel(
String ID,
String Locatie,
String Artikelcode,
String Omschrijving,
String Eenheid,
String Hal,
String Code,
String Aantal,
String Initiaal,
String Max)
{
this.ID = ID;
this.Locatie = Locatie;
this.Artikelcode = Artikelcode;
this.Omschrijving = Omschrijving;
this.Eenheid = Eenheid;
this.Hal = Hal;
this.Code = Code;
this.Aantal = Aantal;
this.Initiaal = Initiaal;
this.Max = Max;
}
public String getID() {
return ID;
}
public String getLocatie() {
return Locatie;
}
public String getArtikelcode() {
return Artikelcode;
}
public String getOmschrijving() {
return Omschrijving;
}
public String getEenheid() {
return Eenheid;
}
public String getHal() {
return Hal;
}
public String getOescode() {
return Code;
}
public String getAantal() {
return Aantal;
}
public String getInitiaal() {
return Initiaal;
}
public String getMax(){
return Max;
}
}
My guess is it's because you initialize ObservableList picklocaties once when creating the class. Have you tried moving the initialization (=FXCollections.observableArrayList();) inside the AddPick method? This way an empty list will be generated everytime the method is called therefore adding the items in your DB only once.

Passing values between controllers by using the view object - JavaFX

When generating a multi view project with Gluon plugin for Ecplise, Netbeans or Intellij. It will be generating a view-class and a presenter-class. The view class looks like this for example:
public class ModelsView {
public View getView() {
try {
View view = FXMLLoader.load(ModelsView.class.getResource("models.fxml"));
return view;
} catch (IOException e) {
System.out.println("IOException: " + e);
return new View();
}
}
}
Is there some way I can access all the private fields in this controller models.fxml by just creating a code line like this:
View view = new ModelsView().getView(); // Here I am inside another controller
Can I access for example the getter method from the presenter-class by using the view object ? The view object is static as I can see it because there is no new before FXMLLoader.load(URL location)
public class ModelsPresenter {
#FXML
private View models;
#FXML
private #Getter TableView<String> tableView;
public void initialize() {
}
}
Heretic I am not using JavaFX from Gluon my configuration is jdk1.8.0_191 with derby in Netbeans 8.2 The code I am posting is my Model class PWData and the TableViewController just grabs the data from the derby DB The real important code is the initialize section of the TableViewController
public class PWData {
private final StringProperty ID;
private final StringProperty website;
private final StringProperty un;
private final StringProperty pw;
private final StringProperty sq;
private final StringProperty sans;
private final StringProperty notes;
public PWData(String ID,String website,String un,String pw,String sq,String sans,String notes) {
this.ID = new SimpleStringProperty(ID);
this.website = new SimpleStringProperty(website);
this.un = new SimpleStringProperty(un);
this.pw = new SimpleStringProperty(pw);
this.sq = new SimpleStringProperty(sq);
this.sans = new SimpleStringProperty(sans);
this.notes = new SimpleStringProperty(notes);
}
public String getID() {// 0
return ID.get();
}
public void setID(String ID){// 0
this.ID.set(ID);
}
public StringProperty IDProperty(){// 0
return ID;
}
public String getWebSite() {// 1
return website.get();
}
public void setwebsite(String website){// 1
this.website.set(website);
}
public StringProperty websiteProperty(){// 1
return website;
}
public String getUNName() {// 2
return un.get();
}
public void setun(String un){// 2
this.un.set(un);
}
public StringProperty unProperty(){// 2
return un;
}
public String getPW() {// 3
return pw.get();
}
public void setpw(String pw){// 3
this.pw.set(pw);
}
public StringProperty pwProperty(){// 3
return pw;
}
public String getSQName() {// 4
return sq.get();
}
public void setsq(String sq){// 4
this.sq.set(sq);
}
public StringProperty sqProperty(){// 4
return sq;
}
public String getSANS() {// 5
return sans.get();
}
public void setsans(String sans){// 5
this.sans.set(sans);
}
public StringProperty sansProperty(){// 5
return sans;
}
public String getNotes() {// 6
return notes.get();
}
public void setnotes(String notes){// 6
this.notes.set(notes);
}
public StringProperty notesProperty(){// 6
return notes;
}
public class TableViewController implements Initializable {
#FXML Pane tableviewPane,signinPane,detailviewPane;
#FXML private TableView<PWData> table;// NOTE CONSTRUCTION see Observable Value
#FXML private TableColumn<PWData,String> IDCol;
#FXML private TableColumn<PWData,String> websiteCol;
#FXML private TableColumn<PWData,String> unCol;
#FXML private TableColumn<PWData,String> pwCol;
String SQL_PWDataTable = "create table PWData ("
+ "ID int not null generated always as identity "
+ " (start with 100,increment by 1),"
+ "website varchar(50) not null, un varchar(40) not null, "
+ "pw varchar(40) not null, sq varchar(80) not null, "
+ "sans varchar(80) not null, notes varchar(260) not null,"
+ "primary key (ID) )";
String dbName="PWKeep";
public Stage stage;
String conURL = "jdbc:derby:C:/A_DerbyPWKeeper/DBName/" + dbName;
public static String strID;
KeyCode kc;
#FXML
private void onPress(KeyEvent ev) throws IOException{
kc = ev.getCode();
if(kc == KeyCode.ESCAPE){
onBack(null);
}
}
private void MakeTable() throws SQLException, IOException{
if (!tableExists( SignInController.con, "PWData")){
//System.out.println ("Creating table PWData");
SignInController.stmnt = SignInController.con.createStatement();
SignInController.stmnt.execute(SQL_PWDataTable );
SignInController.stmnt.close();
}else{
//System.out.println("PWData already created");
}
ReadFromDB();
}
// Does the table EXISTS
private static boolean tableExists ( Connection con, String table ) {
int numRows = 0;
try {
DatabaseMetaData dbmd = con.getMetaData();
// Note the args to getTables are case-sensitive!
ResultSet rs = dbmd.getTables( null, "APP", table.toUpperCase(), null);
while( rs.next()) ++numRows;
}catch(SQLException e){
String theError = e.getSQLState();
System.out.println("Can't query DB metadata: " + theError );
System.exit(1);
}
return numRows > 0;
}
private void ReadFromDB() throws SQLException{
SignInController.stmnt = SignInController.con.createStatement();
ObservableList<PWData> TableData = FXCollections.observableArrayList();
try (ResultSet rs = SignInController.stmnt.executeQuery("SELECT * FROM PWData") // Get all DB data
//int rowCount = 0;
) {
while (rs.next()){// Add data to observableArrayList TableData
//rowCount++;
TableData.add(new PWData(rs.getString("ID"),rs.getString("website")
,rs.getString("un"),rs.getString("pw"),rs.getString("sq"),rs.getString("sans"),rs.getString("notes")));
} //System.out.println("Row Count "+rowCount);// Useful for Printing for further development
PropertyValueFactory<PWData, String> IDCellValueFactory = new PropertyValueFactory<>("ID");
IDCol.setCellValueFactory(IDCellValueFactory);
PropertyValueFactory<PWData, String> WebSiteCellValueFactory = new PropertyValueFactory<>("website");
websiteCol.setCellValueFactory(WebSiteCellValueFactory);
PropertyValueFactory<PWData, String> UNCellValueFactory = new PropertyValueFactory<>("un");
unCol.setCellValueFactory(UNCellValueFactory);
PropertyValueFactory<PWData, String> PWCellValueFactory = new PropertyValueFactory<>("pw");
pwCol.setCellValueFactory(PWCellValueFactory);
Collections.sort(TableData, (p1, p2)-> p1.getWebSite().compareToIgnoreCase(p2.getWebSite()));
// Line of Code above Sorts websiteCol alpha
if(TableData.size() < 14) {// Format TableView to display Vertical ScrollBar
table.setPrefWidth(838);
}else {
table.setPrefWidth(855);
} table.setItems(TableData);
SignInController.stmnt.close();
}
}
#FXML
private void onBack(ActionEvent e) throws IOException{
stage = (Stage)tableviewPane.getScene().getWindow();
signinPane = FXMLLoader.load(getClass().getResource("signin.fxml"));
Scene scene = new Scene(signinPane);
scene.getStylesheets().add(getClass().getResource("pwkeeper.css").toExternalForm());
stage.setScene(scene);
stage.show();
stage.sizeToScene();
stage.centerOnScreen();
}
#FXML
private void onAdd(ActionEvent e) throws IOException{
stage = (Stage)tableviewPane.getScene().getWindow();
detailviewPane = FXMLLoader.load(getClass().getResource("detailview.fxml"));
Scene scene = new Scene(detailviewPane);
scene.getStylesheets().add(getClass().getResource("pwkeeper.css").toExternalForm());
stage.setScene(scene);
stage.show();
stage.sizeToScene();
stage.centerOnScreen();
}
private void MakeConn() throws SQLException, IOException{
SignInController.con = DriverManager.getConnection(conURL);
}
#FXML // This DROPS the MasterPW TABLE when the Reset Password is selected
private void onDrop(ActionEvent e) throws SQLException, IOException{
SignInController.stmnt = SignInController.con.createStatement();
SignInController.stmnt.executeUpdate("DROP TABLE MasterPW");
SignInController.stmnt.close();
onBack(null);
}
private void showTableDataDetails(PWData info) throws IOException{
if (info != null) {
info = (PWData) table.getSelectionModel().getSelectedItem();
strID = info.getID();
onAdd(null);
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
try {
MakeTable();
} catch (SQLException | IOException ex) {
Logger.getLogger(TableViewController.class.getName()).log(Level.SEVERE, null, ex);
}
table.getSelectionModel().selectedItemProperty().addListener((ObservableValue<? extends PWData>
observable,PWData oldValue, PWData newValue) -> {
try {
showTableDataDetails((PWData) newValue); // When a row of the table is Selected call
// Proper Construction // showTableDataDetails method
} catch (IOException ex) {
Logger.getLogger(TableViewController.class.getName()).log(Level.SEVERE, null, ex);
}
});
}
I am a little unsure of what you are trying to do but I would suggest you focus on the use of ObservableValue and Listeners You can download the entire code
At GitHub HERE

JavaFX TableView Cell color change depending on text value

I have a JavaFX desktop app with a TableView. I populate the data using a POJO named Orders which ultimately comes from a Firebird SQL database.
Image of what I have now
What I am looking to do is change the background fill color of each cell in the first column 'Status' depending on the text value. So if the text value is 'READY' then green, 'STARTED' will be yellow and 'DONE' will be gray.
Image of what I would like
Here is the code portion I use to populate the TableView:
`
#FXML private TableView<Orders> tblOrders;
#FXML private TableColumn<Orders, Integer> clmStatus;
#FXML private TableColumn<Orders, String> clmStartDateTime;
#FXML private TableColumn<Orders, String> clmShopOrder;
#FXML private TableColumn<Orders, String> clmRotation;
#FXML private TableColumn<Orders, String> clmGMIECode;
#FXML private TableColumn<Orders, String> clmSAPCode;
#FXML private TableColumn<Orders, Integer> clmLineName;
#FXML private TableColumn<Orders, Integer> clmOrderProductionNr;
private ObservableList<Orders> list;
public void initialize(URL location, ResourceBundle resources) {
populateTable();
}
private void populateTable() {
log.appLog("Populating table\r\n");
clmStatus.setCellValueFactory(new PropertyValueFactory<>("status"));
clmStartDateTime.setCellValueFactory(new PropertyValueFactory<>
("startDateTime"));
clmShopOrder.setCellValueFactory(new PropertyValueFactory<>("extra1"));
clmRotation.setCellValueFactory(new
PropertyValueFactory<("batchLotNr"));
clmGMIECode.setCellValueFactory(new PropertyValueFactory<>("wareNr"));
clmSAPCode.setCellValueFactory(new PropertyValueFactory<>
("serviceDescription"));
clmLineName.setCellValueFactory(new PropertyValueFactory<>
("productionLineNr"));
clmOrderProductionNr.setCellValueFactory(new PropertyValueFactory<>
("orderProductionNr"));
tblOrders.setItems(list);
}
`
Code sample of my Orders POJO:
`
public class Orders {
private final SimpleStringProperty status;
private final SimpleStringProperty startDateTime;
private final SimpleStringProperty extra1;
private final SimpleStringProperty batchLotNr;
private final SimpleStringProperty wareNr;
private final SimpleStringProperty serviceDescription;
private final SimpleStringProperty productionLineNr;
private final SimpleIntegerProperty orderProductionNr;
Orders(String status, String startDateTime, String extra1, String batchLotNr, String wareNr, String serviceDescription, String productionLineNr, int orderProductionNr) {
this.status = new SimpleStringProperty(status);
this.startDateTime = new SimpleStringProperty(startDateTime);
this.extra1 = new SimpleStringProperty(extra1);
this.batchLotNr = new SimpleStringProperty(batchLotNr);
this.wareNr = new SimpleStringProperty(wareNr);
this.serviceDescription = new SimpleStringProperty(serviceDescription);
this.productionLineNr = new SimpleStringProperty(productionLineNr);
this.orderProductionNr = new SimpleIntegerProperty((orderProductionNr));
}
public String getStatus() {
return status.get();
}
public String getStartDateTime() {return startDateTime.get(); }
public String getExtra1() {
return extra1.get();
}
public String getBatchLotNr() {
return batchLotNr.get();
}
public String getWareNr() {
return wareNr.get();
}
public String getServiceDescription() {
return serviceDescription.get();
}
public String getProductionLineNr() {
return productionLineNr.get();
}
int getOrderProductionNr() {return orderProductionNr.get();}
}
`
I have tried using a callback but I have never used callbacks before and don't properly understand how I can fit my needs into a callback. Any help will be important to my learning. Thanks SO.
You have to define a custom TableCell for your status column like this:
public class ColoredStatusTableCell extends TableCell<TableRow, Status> {
#Override
protected void updateItem(Status item, boolean empty) {
super.updateItem(item, empty);
if (empty || getTableRow() == null) {
setText(null);
setGraphic(null);
} else {
TableRow row = (TableRow) getTableRow().getItem();
setText(item.toString());
setStyle("-fx-background-color: " + row.getColorAsString());
// If the statis is changing dynamic you have to add the following:
row.statusProperty()
.addListener((observable, oldValue, newValue) ->
setStyle("-fx-background-color: " + row.getColorAsString()));
}
}
}
Where TableRow:
public class TableRow {
private ObjectProperty<Status> status;
private Map<Status, Color> statusColor;
public TableRow(Status status, Map<Status, Color> statusColor) {
this.status = new SimpleObjectProperty<>(status);
this.statusColor = statusColor;
}
public Status getStatus() {
return status.get();
}
public ObjectProperty<Status> statusProperty() {
return status;
}
public Color getStatusColor() {
return statusColor.get(status.get());
}
public String getColorAsString() {
return String.format("#%02X%02X%02X",
(int) (getStatusColor().getRed() * 255),
(int) (getStatusColor().getGreen() * 255),
(int) (getStatusColor().getBlue() * 255));
}
}
Status:
public enum Status {
READY, STARTED, DONE
}
and the controller:
public class TestController {
#FXML
private TableView<TableRow> table;
#FXML
private TableColumn<TableRow, Status> column;
private ObservableList<TableRow> data = FXCollections.observableArrayList();
#FXML
public void initialize() {
column.setCellValueFactory(data -> data.getValue().statusProperty());
column.setCellFactory(factory -> new ColoredStatusTableCell());
Map<Status, Color> statusColor = new HashMap<>();
statusColor.put(Status.READY, Color.GREEN);
statusColor.put(Status.STARTED, Color.YELLOW);
statusColor.put(Status.DONE, Color.GRAY);
TableRow ready = new TableRow(Status.READY, statusColor);
TableRow started = new TableRow(Status.STARTED, statusColor);
TableRow done = new TableRow(Status.DONE, statusColor);
data.addAll(ready, started, done);
table.setItems(data);
}
}
I chose to set the status as an enum because it is easier to handle it,
then I have used a map to each status-color combination, then in the cell you can set its background color to the matched color of the status.
If you want of course instead of Color.YELLOW and so on you can use a custom Color.rgb(red,green,blue)
I finally found the solution without having to use any extra classes, just a callback in my controller class with the help of this SO link:
StackOverFlow Link
`
private void populateTable() {
log.appLog("Populating table\r\n");
//clmStatus.setCellValueFactory(new PropertyValueFactory<>("status"));
clmStatus.setCellFactory(new Callback<TableColumn<Orders, String>,
TableCell<Orders, String>>()
{
#Override
public TableCell<Orders, String> call(
TableColumn<Orders, String> param) {
return new TableCell<Orders, String>() {
#Override
protected void updateItem(String item, boolean empty) {
if (!empty) {
int currentIndex = indexProperty()
.getValue() < 0 ? 0
: indexProperty().getValue();
String clmStatus = param
.getTableView().getItems()
.get(currentIndex).getStatus();
if (clmStatus.equals("READY")) {
setTextFill(Color.WHITE);
setStyle("-fx-font-weight: bold");
setStyle("-fx-background-color: green");
setText(clmStatus);
} else if (clmStatus.equals("STARTED")){
setTextFill(Color.BLACK);
setStyle("-fx-font-weight: bold");
setStyle("-fx-background-color: yellow");
setText(clmStatus);
} else if (clmStatus.equals("DONE")){
setTextFill(Color.BLACK);
setStyle("-fx-font-weight: bold");
setStyle("-fx-background-color: gray");
setText(clmStatus);
} else {
setTextFill(Color.WHITE);
setStyle("-fx-font-weight: bold");
setStyle("-fx-background-color: red");
setText(clmStatus);
}
}
}
};
}
});
clmStartDateTime.setCellValueFactory(new PropertyValueFactory<>("startDateTime"));
clmShopOrder.setCellValueFactory(new PropertyValueFactory<>("extra1"));
clmRotation.setCellValueFactory(new PropertyValueFactory<>("batchLotNr"));
clmGMIECode.setCellValueFactory(new PropertyValueFactory<>("wareNr"));
clmSAPCode.setCellValueFactory(new PropertyValueFactory<>("serviceDescription"));
clmLineName.setCellValueFactory(new PropertyValueFactory<>("productionLineNr"));
clmOrderProductionNr.setCellValueFactory(new PropertyValueFactory<>("orderProductionNr"));
tblOrders.setItems(list);
}
`
I don't have badge to comment, but wanted to add some details.
I wanted to format color of cell based on the boolean value which i have in my data set. I have reviewed this question and similar one provided already here:
Stackoverflow link - style based on another cell in row
What was missing in both for me is reseting style when there is no value as kleopatra mentioned.
This works for me:
public class TableCellColored extends TableCell<DimensionDtoFxBean, DimValVoFxBean> {
private static final String DEFAULT_STYLE_CLASS = "table-cell";
public TableCellColored() {
super();
}
#Override
protected void updateItem(DimValVoFxBean item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText("");
resetStyle();
return;
}
setText(Optional.ofNullable(item.getValue()).map(BigDecimal::toString).orElse(""));
Boolean conversionFlag = Optional.ofNullable(item.getConversionFlag()).orElse(true);
updateStyle(conversionFlag);
item.conversionFlagProperty()
.addListener((observable, oldValue, newValue) -> updateStyle(newValue));
}
private void updateStyle(Boolean conversionFlag) {
if (!conversionFlag) {
setStyle("-fx-background-color: red");
} else {
resetStyle();
}
}
private void resetStyle() {
setStyle("");
getStyleClass().addAll(TableCellColored.DEFAULT_STYLE_CLASS);
}
}
Since I have value object with value and boolean flag I can do it i seperate class and don't have add lambda in controller.
Deafult styling of cell is transparent so if we use style to change color, we have to reset it when there is no value.
Since direct styling has bigger priority than class it overrides default styling from css classes.
To be on the safe side I also apply DEFAULT_STYLE_CLASS. Value taken from TableCell class.
Without listener and styles reset I red was staying in table during scrolling. After few scrolls all cells where red. So listener and styles reset is the must have for me.

Preventing duplicate entries in ObservableList

I can't seem to get it right. I have a listbox with category items. I also have a data model that defines the ID, Name, Lastname, Category.. and an ObservableList that holds the data. I'm trying to update the existing object in case the user click on the same item in the listbox and change name, lastname.
Here is my code:
public class FXMLDocController implements Initializable {
ObservableList<String> listitems = FXCollections.observableArrayList(
"Visual Basic", "ASP.net", "JavaFX");
ObservableList<Persons> personData = FXCollections.observableArrayList();
Persons pp = new Persons();
private Label label;
#FXML
private TextField txtName;
#FXML
private TextField txtLastName;
#FXML
private Button btnSave;
#FXML
private TextArea txtArea;
#FXML
private ListView<String> listview = new ListView<String>();
#FXML
private Button btnTest;
#FXML
private Label lblCategory;
#FXML
private Label lblIndex;
#Override
public void initialize(URL url, ResourceBundle rb) {
listview.setItems(listitems);
}
#FXML
private void handleSave(ActionEvent event) {
String category = lblCategory.getText();
boolean duplicate = false;
//Add data. Check first if personData is empty
if (personData.isEmpty()){
pp = new Persons(Integer.valueOf(lblIndex.getText()),category,txtName.getText(),txtLastName.getText());
personData.add(pp);
}else{
for(int i = 0 ; i<personData.size() ; i ++){
if(Integer.toString(personData.get(i).getID()).equals(lblIndex.getText())){
duplicate = true;
}else{
duplicate = false;
}
}
System.out.println(duplicate);
if (duplicate == false){
pp = new Persons(Integer.valueOf(lblIndex.getText()),category,txtName.getText(),txtLastName.getText());
personData.add(pp);
}else{
System.out.println("Duplicate");
// Do Update later.
}
}
//Show data to Test
System.out.println("-- START OF LIST --");
for (Persons person : personData){
System.out.println(person.getID() + " " + person.getCategory()+ " " + person.getName() + " " + person.getLastname() + " " );
}
System.err.println(" ");
}
#FXML
private void handleListClick(MouseEvent event) {
lblCategory.setText(listview.getSelectionModel().getSelectedItem());
lblIndex.setText(String.valueOf(listview.getSelectionModel().getSelectedIndex()));
}
}
If I click multiple times on one item on the listbox it works well.. but if for example I click on Visual Basic, then ASP.net the go back to Visual Basic it still accepts it. :(
Need advice and help. Please
Here is the code based on Phil's suggestion
#FXML
private void handleSave(ActionEvent event) {
String category = lblCategory.getText();
boolean duplicate = false;
int x = 0;
//Add data
Persons newPerson = new Persons(Integer.valueOf(lblIndex.getText()), category, txtName.getText(), txtLastName.getText());
if (!personData.contains(newPerson)) {
personData.add(newPerson);
}else{
System.out.println("Duplicate!");
}
//Show data
System.out.println("-- START OF LIST --");
for (Persons person : personData){
System.out.println(person.getID() + " " + person.getCategory()+ " " + person.getName() + " " + person.getLastname() + " " );
}
}
here's my Persons Class
public class Persons {
private SimpleIntegerProperty id;
private SimpleStringProperty name;
private SimpleStringProperty lastname;
private SimpleStringProperty category;
public Persons(){}
public Persons(int id, String category, String name, String lastname){
this.id = new SimpleIntegerProperty(id);
this.name = new SimpleStringProperty(name);
this.lastname = new SimpleStringProperty(lastname);
this.category = new SimpleStringProperty(category);
}
public Persons(String name, String lastname){
this.name = new SimpleStringProperty(name);
this.lastname = new SimpleStringProperty(lastname);
}
#Override
public boolean equals(Object o){
if (o == this) return true;
if (!(o instanceof Persons)){
return false;
}
Persons persons = (Persons) o;
return persons.id.equals(id) &&
persons.name.equals(name) &&
persons.lastname.equals(lastname) &&
persons.category.equals(category);
}
//SETTERS
public void setID(int id) {
this.id = new SimpleIntegerProperty(id);
}
public void setName(String name) {
this.name = new SimpleStringProperty(name);
}
public void setLastname(String lastname) {
this.lastname = new SimpleStringProperty(lastname);
}
public void setCategory(String category) {
this.category = new SimpleStringProperty(category);
}
//GETTERS
public int getID() {
return id.getValue();
}
public String getName() {
return name.getValue();
}
public String getLastname() {
return lastname.getValue();
}
public String getCategory(){
return category.getValue();
}
// PROPERTIES
public SimpleIntegerProperty idProperty(){
return this.id;
}
public SimpleStringProperty nameProperty(){
return this.name;
}
public SimpleStringProperty lastnameProperty(){
return this.lastname;
}
public SimpleStringProperty categoryProperty(){
return this.category;
}
}
Ok, this looks good. Just one little thing in your equals implementation:
#Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Persons)) {
return false;
}
Persons persons = (Persons) o;
// persons.id.equals() leads to the default implementation in Object
// --> instead use this one.
// The Property classes have their own isEqualTo method
// with get(), you will get your simple boolean from the returned BooleanBinding
return persons.id.isEqualTo(id).get() &&
persons.name.isEqualTo(name).get() &&
persons.lastname.isEqualTo(lastname).get() &&
persons.category.isEqualTo(category).get();
}
The default equals implementation from Object just compares if it is the same instance. And we are creating a new instance and check with contains(...) if the list contains the Persons.
Here is the whole code I used to test it:
public class Main extends Application {
private ObservableList<String> listitems = FXCollections.observableArrayList("Visual Basic", "ASP.net", "JavaFX");
private ObservableList<Persons> personData = FXCollections.observableArrayList();
private TextField txtName = new TextField();
private TextField txtLastName = new TextField();
private Button btnSave = new Button("save");
private ListView<String> listview = new ListView<>();
private Label lblCategory = new Label();
private Label lblIndex = new Label();
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
listview.setItems(listitems);
listview.setOnMouseClicked(this::handleListClick);
btnSave.setOnAction(this::handleSave);
VBox vb = new VBox(new HBox(5, new Label("Index:"), lblIndex),
new HBox(5, new Label("Category:"), lblCategory),
new HBox(5, new Label("Name:"), txtName),
new HBox(5, new Label("Last name:"), txtLastName)
);
BorderPane bp = new BorderPane();
bp.setLeft(listview);
bp.setCenter(vb);
bp.setRight(btnSave);
Scene scene = new Scene(bp, 600, 400);
stage.setScene(scene);
stage.show();
}
private void handleSave(ActionEvent event) {
Persons newPerson = new Persons(Integer.valueOf(lblIndex.getText()), lblCategory.getText(), txtName.getText(), txtLastName.getText());
if (!personData.contains(newPerson)) {
personData.add(newPerson);
} else {
System.out.println("Duplicate!");
}
System.out.println("-- START OF LIST --");
for (Persons person : personData) {
System.out.println(person);
}
}
private void handleListClick(MouseEvent event) {
System.out.println("click");
lblCategory.setText(listview.getSelectionModel().getSelectedItem());
lblIndex.setText(String.valueOf(listview.getSelectionModel().getSelectedIndex()));
}
public class Persons {
SimpleIntegerProperty id;
SimpleStringProperty name;
SimpleStringProperty lastname;
SimpleStringProperty category;
Persons(int id, String category, String name, String lastname) {
this.id = new SimpleIntegerProperty(id);
this.name = new SimpleStringProperty(name);
this.lastname = new SimpleStringProperty(lastname);
this.category = new SimpleStringProperty(category);
}
#Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Persons)) {
return false;
}
Persons persons = (Persons) o;
// persons.id.equals() leads to the default implementation in Object
// --> instead use this one.
// The Property classes have their own isEqualTo method
// with get(), you will get your simple boolean from the returned BooleanBinding
return persons.id.isEqualTo(id).get() &&
persons.name.isEqualTo(name).get() &&
persons.lastname.isEqualTo(lastname).get() &&
persons.category.isEqualTo(category).get();
}
#Override
public String toString() {
return "Persons{" +
"id=" + id +
", name=" + name +
", lastname=" + lastname +
", category=" + category +
'}';
}
}
}

Having trouble retrieving value from tableview

I'm having having trouble getting a correct output from tableview. I'm using a button to set one item from tableview to a label. However, it prints "StringProperty [Value pineapples]" where I would like it to be just "pineapples".
The tableview gives them correctly.
public class ProductListController implements Initializable {
#FXML public TableView<Model> tableview ;
#FXML private TableColumn<Model, Number> ProductID;
#FXML private TableColumn<Model, String> ProductName;
#FXML private TableColumn<Model, Number> ProductPrice;
#FXML private Label lblProduct;
#FXML private Label lblPrice;
#FXML
private void btnActionShow(ActionEvent event) {
assert tableview !=null : " ";
ProductID.setCellValueFactory(cellData -> cellData.getValue().ProductIDProperty());
ProductName.setCellValueFactory(cellData -> cellData.getValue().ProductNameProperty());
ProductPrice.setCellValueFactory(cellData -> cellData.getValue().ProductPriceProperty());
buildData();
}
private ObservableList<Model> data;
public void buildData(){
data = FXCollections.observableArrayList();
try{
Connection conn = DriverManager.getConnection
("jdbc:derby://localhost:1527/Stock", "*****", "*****");
Statement stmt = conn.createStatement();
String SQL = "SELECT * FROM PRODUCTS";
ResultSet rs = stmt.executeQuery(SQL);
while (rs.next()) {
Model mod = new Model();
mod.ProductID.set(rs.getInt("ID"));
mod.ProductName.set(rs.getString("NAME"));
mod.ProductPrice.set(rs.getInt("SELL_PRICE"));
data.add(mod);
}
tableview.setItems(data);
}
catch ( SQLException err) {
System.out.println(err.getMessage() );
}
}
//Button to fetch data from Tableview. Sets the data not the way I want.
#FXML
private void btnConfirmAction(ActionEvent event) {
Model model = tableview.getSelectionModel().getSelectedItem();
String prd;
prd = model.getProductName().toString();
lblProduct.setText(prd);
}
#FXML
private void btnNextAction(ActionEvent event) {
try{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/appl/Discount.fxml"));
Parent parent = loader.load();
DiscountController discountcontr = loader.getController();
discountcontr.setProduct(tableview.getSelectionModel().getSelectedItem().getProductName().toString());
Stage stage = new Stage();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();
}
catch(IOException e){
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
}
}
Model
public class Model {
public SimpleIntegerProperty ProductID = new SimpleIntegerProperty();
public SimpleStringProperty ProductName = new SimpleStringProperty ();
public SimpleIntegerProperty ProductPrice = new SimpleIntegerProperty();
private final SimpleBooleanProperty Checked = new SimpleBooleanProperty(false);
public SimpleBooleanProperty checkedProperty() {
return this.Checked;
}
public java.lang.Boolean getChecked() {
return this.checkedProperty().get();
}
public void setChecked(final java.lang.Boolean checked) {
this.checkedProperty().set(checked);
}
public SimpleIntegerProperty getProductID() {
return ProductID;
}
public SimpleStringProperty getProductName() {
return ProductName;
}
public SimpleIntegerProperty getProductPrice() {
return ProductPrice;
}
Since getProductName() returns a SimpleStringProperty, you need to retrieve the String from it using the get(). Just use :
String prd = model.getProductName().get();
Your model is implemented incorrectly. You should use the following pattern:
public class Model {
private SimpleStringProperty productName = new SimpleStringProperty();
public SimpleStringProperty productNameProperty() {
return productName ;
}
public final String getProductName() {
return productNameProperty().get();
}
public final void setProductName(String productName) {
productNameProperty().set(productName);
}
}
and similarly for the other properties.
If you use the e(fx)clipse plugin, you can generate the methods automatically from the property definition by right-clicking, choosing "Source" and then "Generate JavaFX Getters and Setters". I think NetBeans has similar functionality.

Resources