I am trying to populate my javafx application TableView using setItems method, while doing so, i first defined my controller path, by doing
fx:"sample.Application"
then i defined my data model with the class name "Products" with all the necessary constructors and getters and setters. I then started writing my controller code, i defined all the necessary fx: id's with annotation of FXML, I override the initialize method which apparently is giving no errors, also to populate TableView i used ObserverList and called constructor of Products by using observerArrayList, in the end when i try to populate the TableView with fx:id ="table", by using setItems(), i got an error:
table.setItems(prodList);
error:
Error:(46, 19) java: identifier expected Error:(46, 28) java: identifier expected
here is the code:
FXML CODE:
<TableView fx:id="table" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.halignment="LEFT" GridPane.rowIndex="3" >
<columns>
<TableColumn fx:id="col_id" text="PRODUCT ID"/>
<TableColumn fx:id="col_name" text="NAME"/>
<TableColumn fx:id="col_price" text="PRICE" />
<TableColumn fx:id="col_tax" text="TAX" />
<TableColumn fx:id="col_discount" text="DISCOUNT" />
</columns>
</TableView>
Controller code
public class Application implements Initializable {
@FXML
private TableView<Products> table;
@FXML
private TableColumn<Products, Integer> col_id;
@FXML
private TableColumn<Products, String> col_name;
@FXML
private TableColumn<Products, Integer> col_price;
@FXML
private TableColumn<Products, Integer> col_tax;
@FXML
private TableColumn<Products, Integer> col_discount;
final ObservableList<Products> prodList = FXCollections.observableArrayList(
new Products(11, "Laptop", 25000, 23, 12 )
);
@Override
public void initialize(URL location, ResourceBundle resources) {
col_id.setCellValueFactory(new PropertyValueFactory<>("productId"));
col_name.setCellValueFactory(new PropertyValueFactory<>("name"));
col_price.setCellValueFactory(new PropertyValueFactory<>("price"));
col_tax.setCellValueFactory(new PropertyValueFactory<>("tax"));
col_discount.setCellValueFactory(new PropertyValueFactory<>("discount"));
}
table.setItems(prodList); //error
}
You haven't set the property value factory in the initialize method. I think that's causing the problem.
There's nothing inside the PropertyValueFactory<>
. You have to set it the way given below -
col_id.setCellValueFactory(new PropertyValueFactory<Products, Integer>("productId"));
col_name.setCellValueFactory(new PropertyValueFactory<Products, String>("name"));
col_price.setCellValueFactory(new PropertyValueFactory<Products, Integer>("price"));
col_tax.setCellValueFactory(new PropertyValueFactory<Products, Integer>("tax"));
col_discount.setCellValueFactory(new PropertyValueFactory<Products, Integer>("discount"));
Another thing, table.setItems(prodList)
should be inside the initialize method. Correct it.