I have a bill table where I want to list all products which are on the bill. I saved the ProductInBill objects within an ArrayList<ProductInBill> on the bill.
When I created a TableView my common approach is to create the JavaFX fields. On the controller class, I have my fields:
#FXML public TableColumn<ProductInBill, String> finishedBillProductNameColumn;
#FXML public TableColumn<Integer, Integer> finishedBillProductNumberColumn;
#FXML public TableColumn<ProductInBill, Integer> finishedBillProductPriceBruttoLabel;
#FXML public TableColumn<Integer, Integer> finishedBillProductTotalAmountColumn;
#FXML public TableView finishedBillProductTable;
Then I am using a setUp() method with the code like:
private void setUpFinishedBillProductTable() {
finishedBillProductNameColumn.setCellValueFactory(new PropertyValueFactory<ProductInBill, String>("productName"));
finishedBillProductPriceBruttoLabel.setCellValueFactory(new PropertyValueFactory<ProductInBill, Integer>("productPrice"));
}
Also there is an updateBillTable() method to load the necessary ProductInBill objects, save them to an TableList and give it to the table.
private void updateFinishedBillProductTable(Bill bill) {
LOG.info("Start reading all Products from Bill");
for(ProductInBill product : bill.getProducts()){
finishedBillProductCurrent.add(product);
}
finishedBillProductTable.getItems().clear();
if(!finishedBillProductCurrent.isEmpty()) {
for (ProductInBill p : finishedBillProductCurrent) {
finishedBillProductTableList.add(p);
}
//here i want to calculate some other Integer values based on the ProductInBill values and insert them to the table too.
finishedBillProductTable.setItems(finishedBillProductTableList);
}
}
This is all working very good. My problem now is, that I have also a field on my TableView with calculated Integer values which I don't want to save within an object.
Take for example the finishedBillProductNumberColumn. I want iterate on my ArrayList, find all products with the same name and populate the number of the same items to the table.
How can I do this? I found only solutions where I have to use a value from my object to insert something to my TableView.
You just have to write a custom CellValueFactory for those case instead of using premade ones. Using PropertyValueFactory is just an handy short cut to fill cells with members.
For your example:
finishedBillProductNameColumn.setCellValueFactory(new PropertyValueFactory<ProductInBill, String>("productName"));
is just a shorter way to do:
finishedBillProductNameColumn.setCellValueFactory( cellData -> {
ProductInBill productInBill = cellData.getValue();
return data == null ? null : new SimpleStringProperty(productInBill.getProductName());
});
That being said, i have an 100% preference for the second syntax. Because on the first one if you rename the member, and you forgot to change it there, you won't know there is a mistake until you get there in the application. Plus it allow to display different value than just the members.
As a concrete example for your finishedBillProductNumberColumn you could do:
First change the definition(the first Generic type is the one received with cellData.getValue():
#FXML public TableColumn<ProductInBill, Integer> finishedBillProductNumberColumn;
and then define the CellValueFactory you want like:
finishedBillProductNumberColumn.setCellValueFactory( cellData -> {
ProductInBill productInBill = cellData.getValue();
if(productionInBill != null){
Long nbProduct = finishedBillProductTable.getItems().stream().filter(product -> product.getProductName().equals(productInBill.getProductName())).count();
return new SimpleIntegerProperty(nbProduct.intValue()).asObject();
}
return null;
});
Hope it helped!
Related
I have a suspicion that I have stumbled upon a bug in JavaFX.
I have several TableViews that hold information about different objects.
In this example, I have an Examiner object with a name and a corresponding Course object.
I have created a function selectExaminer() that populates the Examiner name TextField and the Course ChoiceBox upon clicking on the Examiner object from the TableView with it's corresponding values.
But as can be seen from the screenshot above, only the TextField examinerName is populated, while the ChoiceBox choiceBoxExaminer is not. Here is the method: (it is called in the initialize() method)
public void selectExaminer(){
examinerTableView.getSelectionModel().setCellSelectionEnabled(true);
ObservableList selectedCells = examinerTableView.getSelectionModel().getSelectedItems();
selectedCells.addListener(new ListChangeListener() {
#Override
public void onChanged(Change c) {
if(selectedCells.size()>0)
{
Examiner aux = (Examiner) selectedCells.get(0);
examinerName.setText(aux.getName());
choiceBoxExaminer.setValue(aux.getCourse()); //here is the issue
System.out.println("Choice box: " + choiceBoxExaminer.getValue());
System.out.println("Actual object: " + aux.getCourse());
lastExaminerSelectedName = examinerName.getText();
}
}
});
The ChoiceBox dropdown does work but doesn't display the value set through .setValue()
When printing to the console the value of the Course of the actual Examiner and the one from the TableView, both show that they are populated.
System.out.println("Choice box: " + choiceBoxExaminer.getValue());
System.out.println("Actual object: " + aux.getCourse());
But alas... the ChoiceBox is still blank.
This issue arose after implementing data storage to binary files (this is a college project, no db), although I'm not sure how it influences the particular issue
Thank you
Try choiceBoxExaminer.getSelectionModel().setSelectedItem(aux.getCourse());
But, honestly, you make a good point; you would think that setValue() would also do the trick.
aux.getCourse()
Make sure that the return value is exactly the same as an item in the list added to the choiceBox. Only choicebox.setValue() works:
String[] fruits = {"apple", "orange"};
choiceBox.setItems(FXCollections.observableArrayList(fruits);
choiceBox.setValue("grape");//this won't work since grape isn's in the list.
choiceBox.setValue("orange"); // choicebox will display the value.
In my JavaFx project I have a scene with a few views. For the footer of the window I have a class extending from TableView:
public class FooterView extends TableView<Area> implements ViewTemplate {...}
This displays a Table with some data from a .csv-file.
When it comes to assigning the value of the specific presentationmodel property to the cells I do it like that:
TableColumn<Area,Double> powerColumn = new TableColumn<>("Power Overview");
powerColumn.setCellValueFactory(new PropertyValueFactory<>("powerPerArea"));
this.setItems(filePM.getAreas()); //filePm is my filehandler
this.getColumns().addAll(powerColumn, other_columns);
getAreas() looks like this:
public List<Area> readCantonsFromFile() {
try (Stream<String> stream = getStreamOfLines(AREA_FILE_NAME)) {
return stream.skip(1)
.map(l -> new Area(l.split(DELIMITER, 12)))
.collect(Collectors.toList());
}
}
In the constructor of Area i set the properties. One of the properties is the mentioned powerPerArea
private final DoubleProperty powerPerArea = new SimpleDoubleProperty();
...
public void setPowerPerCanton(double powerPerCanton) {
this.powerPerCanton.set(powerPerCanton);
}
My question: Is there a way to change the value in the FooterView before the value is displayed? I tried something like this:
powerColumn.setCellValueFactory(Math.round(new PropertyValueFactory<>("powerPerArea")));
But it seems that I mixed up DoubleProperty, Double and ObservableDouble. Can I even modify the value in here?
The problem: I can not round the value in the setter because I add a double value in a loop through this function:
public void addPowerPerArea(double power){
double sum = getPowerPerCanton() + power;
setPowerPerCanton(sum);
}
And rounding the value in here would give me a wrong result. (rounding not precise enough). I need to do it in the end when all sums are added
You should use the cellValueFactory to determine which data are displayed in the cells: in this case the data returned by your PropertyValueFactory is the actual double value returned from powerPerAreaProperty().get(), which is exactly what you want.
If you want to control how the data are displayed, you should use a cellFactory. So to display the data in a particular format, including limiting the number of decimal places, you can do:
powerColumn.setCellFactory(tc -> new TableCell<Area, Double>() {
#Override
protected void updateItem(Double power, boolean empty) {
super.updateItem(power, empty);
if (empty) {
setText(null);
} else {
setText(String.format("%.0f", power.doubleValue()));
}
}
});
The point here is that you should not modify the data based on how you want to display it; the purpose of having both cellValueFactory and cellFactory is to separate the display of the data from the actual data itself.
An alternative to returning custom cells from a cellFactory would be to use a custom cellValueFactory to return the property formatted as string:
TableColumn<Area, String> powerColumn = new TableColumn<>("Power Overview");
powerColumn.setCellValueFactory(cd -> cd.getValue().powerPerAreaProperty().asString(""%.0f""));
I see two ways to do this. You could:
use the setCellFactory method and in the updateItem method you format it. Should look something like this, haven't tested
powerColumn.setCellFactory(column -> {
return new TableCell<Area, Double>() {
#Override
protected void updateItem(Double item, boolean empty) {
super.updateItem(Math.round(item), empty);
}
};
});
OR: You could make another property of your Area class that is bound to the existing powerOfArea property but returns the value rounded. I am sure that is somehow possible, you could just override some functions of a readOnlyDoubleProperty but there should be a better way. Maybe via DoubleBindings.
I have created a table using ObservableMap instead of ObservableList. You can see the code (not written by me) here. Now I need to add context menu to every row. So I wrote the code as below:
public MapTableView<String, LineItem> initialize(MapTableView<String, LineItem> tableView) {
tableView.setRowFactory((TableView<Entry<String, LineItem>> tableView1) -> {
final TableRow<Entry<String, LineItem>> row = new TableRow<>();
final ContextMenu contextMenu = new ContextMenu();
final MenuItem cancelMenuItem = new MenuItem("Cancel");
cancelMenuItem.setOnAction((ActionEvent event) -> {
LineItem item = tableView1.getSelectionModel().getSelectedItem().getValue();
System.out.println(item.getLineNo()); // gives me null
});
});
contextMenu.getItems().add(cancelMenuItem);
// Set context menu on row, but use a binding to make it only show for non-empty rows:
row.contextMenuProperty().bind(Bindings.when(row.emptyProperty()).then((ContextMenu) null)
.otherwise(contextMenu)
); tableView1.setContextMenu(contextMenu);
return row;
});
return tableView;
}
Actually it is retrieving the order from the table but when I am accessing the variables of the object it is giving me "null". I couldn't find out what wrong I am doing. Please help me with this. More-over I am initializing the table with:
tableView.setEditable(false);
tableView.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);
Should I have to use anything like:
tableView.getSelectionModel().setCellSelectionEnabled(true);
Everything is working fine, but when I am trying to access the variables of the "item" I am getting the "Null Pointer Exception" as the values are null, but the object is not null. Thanks in advance.
As #James_D said, using
LineItem item = row.getItem().getValue() solved the issue.
It is hard to explain so I'll use an example:
#Override
public void start(Stage primaryStage) throws Exception
{
final VBox vbox = new VBox();
final Scene sc = new Scene(vbox);
primaryStage.setScene(sc);
final TableView<Person> table = new TableView<>();
final TableColumn<Person, String> columnName = new TableColumn<Person, String>("Name");
table.getColumns().add(columnName);
final ObservableList<Person> list = FXCollections.observableArrayList();
list.add(new Person("Hello"));
list.add(new Person("World"));
Bindings.bindContent(table.getItems(), list);
columnName.setCellValueFactory(new PropertyValueFactory<>("name"));
vbox.getChildren().add(table);
final Button button = new Button("test");
button.setOnAction(event ->
{
final Person removed = list.remove(0);
removed.setName("Bye");
list.add(0, removed);
});
vbox.getChildren().add(button);
primaryStage.show();
}
public static class Person
{
private String name = "";
public Person(String n)
{
name = n;
}
public String getName()
{
return name;
}
public void setName(String n)
{
name = n;
}
}
In this example, I show a TableView with a single column named "Name". Running this sample code, you will get two rows: first row with "Hello" in "Name" column; and second row with "World" in "Name" column.
Additionally, there is a button, this button removes the first Person object from the list, then makes some changes to the object, then adds it back in at the same index. Doing so would cause any ListChangeListener added to the ObservableList to be triggered, and I have tested this to be true.
I would expect the row with "Hello" to be replaced with "Bye", but it seems like the TableView continues to show "Hello". If I used a TimeLine to add delay before I add the removed Person object back to the list, it would change to "Bye".
final Timeline tl = new Timeline(new KeyFrame(Duration.millis(30), ae -> list.add(0, removed)));
tl.play();
Is there something weird with the API? Is there any way to do this without this problem?
This is essentially expected behavior.
Note that (and I'm guessing you are trying to work around this issue), if you simply called
list.get(0).setName("Bye");
which has the same effect in terms of the underlying data, the table would not update as it has no way of being notified that the String field name in the element of the list has changed.
The code
Person removed = list.remove(0);
removed.setName("Bye");
list.add(0, removed);
is really equivalent to list.get(0).setName("Bye");: you just temporarily remove the item from the list before changing it, and then add it back. As far as the list is concerned, the net result is the same. I guess you are doing this in the hope that removing and replacing the item from the list will persuade the table to notice the state of the item has changed. There's no guarantee this will be the case. Here is what's happening:
The binding between your two lists:
Bindings.bindContent(table.getItems(), list);
works like any other binding: it defines how to get the value of the binding (the elements of list), and marks the data as invalid if list is invalidated at any time. The latter happens when you add and remove elements from list.
The TableView will not perform layout every time the binding to the list changes; instead, when then binding is invalidated (add or remove an element), then the table view marks itself as potentially needing to be redrawn. Then, on the next rendering pulse, the table will check the data and see if it really needs to be redrawn, and re-render if needed. There are obvious performance-saving features of this implementation.
So what happens with your code is that an item is removed from the list, causing the binding to be marked as invalid. The item is then changed (by calling setName(...)), and the same item is then added back into the list at the same position. This also causes the binding to be marked as invalid, which has no effect (it is already invalid).
No rendering pulse can occur between the removal and re-addition of this element. Consequently, the first time the table actually looks at the changes that were made to the list has to be after the entire remove-change-add process. At that point, the table will see that the list still contains the exact same elements in the exact same order that it previously contained. (The internal state of one of the elements has changed, but since this is not an observable value - not a JavaFX property - the table is unaware of this.) Consequently, the table sees no changes (or sees that all the changes have cancelled each other out), and doesn't re-render.
In the case where you add the pause, then a rendering frame (or two) occurs between the removal of the item and its re-addition. Consequently, the table actually renders one or two frames without the item, and when it is added back in, it adds it back and renders the current value. (You might, possibly, be able to make the behavior unpredictable, by pausing for 16 or 17 milliseconds, which is right on the cusp of the time for one rendering frame.)
It's not clear what you really intend to do. If you are trying to persuade the table to update without using JavaFX properties, you can do
list.get(0).setName("Bye");
table.refresh();
though this is not a very satisfactory solution.
Note too that
list.remove(0);
list.add(0, new Person("Bye"));
will also work (since now the added element is not the same as the removed element).
The better approach is to implement your model class with JavaFX properties:
public static class Person
{
private final StringProperty name = new SimpleStringProperty("");
public Person(String n)
{
setName(n);
}
public StringProperty nameProperty() {
return name ;
}
public final String getName()
{
return nameProperty().get();
}
public final void setName(String n)
{
nameProperty().set(n);
}
}
and then simply calling
list.get(0).setName("Bye");
will update the table (because the cell will be observing the property).
I have an object with two essential fields that must be displayed for the user together in ComboBox something kind of:
MyObject {
Long id;
Integer from;
Integer to;
...
}
My properties look like :
MyObjectProperties {
ModelKeyProvider<MyObject> id();
LabelProvider<MyObject> from();
LabelProvider<MyObject> to();
...
}
I'm trying to display **from - to** in each combobox cell, is it possible using GXT 3 ComboBox?
That's not working for me because i can't use LabelProvider for an int and can't merge two wroperties in same field!
LabelProvider is in fact the only clean way to go, but you need only one LabelProvider:
LabelProvider<MyObject> labelProvider = new LabelProvider<MyObject>(){
#Override
public String getLabel(MyObject item){
return item.getFrom() + " - " + item.getTo();
}
}
And assign it at ComboBox creation time:
ComboBox<MyObject> cb = new ComboBox<MyObject>(store, labelProvider);