I have a ListView with observableList attached to it,
#FXML
private ListView<Weapon> listViewWeapons;
....
//initialize
listViewWeapons.setCellFactory(lv -> new CustomWeaponDetailListCell<>());
listViewWeapons.setItems(CsgoRr.getModel().getWeaponCache());
Custom cell:
public class CustomWeaponDetailListCell<T extends Weapon> extends ListCell<T> {
private final StringBuilder sb = new StringBuilder();
#Override
public void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
} else {
sb.append(item.getName()).append(" Detail:")
.append((String) CsgoRr.objectToJsonString(item.getRecoilPattern()));
setText(sb.toString());
}
}
}
Function for creating new Weapon and adding it to a database and a list:
private static int newWeaponNameIncrement = 1;
#FXML
private void newWeaponOnAction() {
try {
System.out.println("DEBUG WEAPON NAME TRYING TO BE CREATED IS :" + "newWeapon" + newWeaponNameIncrement);
Weapon newWeapon = Weapon.createWeapon("newWeapon" + newWeaponNameIncrement,
new RecoilPattern());
newWeapon.setId(DbUtil.storeWeapon(newWeapon));
CsgoRr.getModel().getWeaponCache().add(newWeapon);
} catch (SQLException ex) {
if (ex.getErrorCode() == 23505) {//duplicate name
System.out.println("DEBUG :Duplicate name on add new weapon");
newWeaponNameIncrement++;
newWeaponOnAction();
}
Logger.getLogger(WeaponViewController.class.getName()).log(Level.SEVERE, null, ex);
} catch (AWTException ex) {
Logger.getLogger(WeaponViewController.class.getName()).log(Level.SEVERE, null, ex);
}
}
Everything works fine, data is added to a database, but the problem once again is with how I see listView behave.The problem is demonstrated here in a GIF (GIF got a bit too big, you have to click link, can't embed it here).
As you can see from image, the problem is that it duplicates items in a list at least visually, so change is refreshed but not in a proper way. Once I change view to something else and then go back, which calls constructor and initialize method, everything looks as it should. Anyone knows what the problem is with this?
Weird part is I have similar code in other controller which points to my previous SO question which I fixed no problem and works flawlessly, but when I do this almost same way I have different results here. JavaFX ListView adding item into observable list doesn't reflect change and it's not selectable
It's a different problem since before I didn't had any update feedback now I have feedback but not the correct one.
You reuse the same StringBuilder every time the item is swapped without clearing it. This means the resulting String will be the concatenation of all values for items that were stored in the Cell.
You need to use different StringBuilders every time or clear the StringBuilder:
public class CustomWeaponDetailListCell<T extends Weapon> extends ListCell<T> {
private final StringBuilder sb = new StringBuilder();
#Override
public void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
} else {
sb.append(item.getName()).append(" Detail:")
.append((String) CsgoRr.objectToJsonString(item.getRecoilPattern()));
setText(sb.toString());
// clear StringBuilder content
sb.delete(0, sb.length());
}
}
}
Related
I am building am application where I need to have a tree view alongside a pane where data is presented. When someone selects an item in the tree view, the application must identify what they have selected and seek the correct data from a database, then present it in the format I've chosen. It must not matter how the user selects the item in the tree view (mouse click, tabbing, arrow keys, etc), just that when an item gets focus, a method is triggered to present the data to the user.
I got this working perfectly for mouse clicks only in the following way:
// Application thread method to build the tree map, used in the generateTree
// method.
public void treeBuilder(TreeMap<ModelSites, ArrayList<ModelPlants>> map) {
TreeMap<ModelSites, ArrayList<ModelPlants>> treeMap = map;
final TreeItemProperties<String, String> rootTreeItem = new TreeItemProperties<String, String>("EMT", null);
TreeItemProperties<String, Integer> site = null;
TreeItemProperties<String, Integer> plant = null;
for (Map.Entry<ModelSites, ArrayList<ModelPlants>> entry : treeMap.entrySet()) {
site = new TreeItemProperties<String, Integer>(entry.getKey().getLongName(), entry.getKey().getPrimaryKey());
rootTreeItem.getChildren().add(site);
if (site.getValue().equalsIgnoreCase("test item")) {
site.setExpanded(true);
}
for (int i = 0; i < entry.getValue().size(); i++) {
plant = new TreeItemProperties<String, Integer>(entry.getValue().get(i).getSitePlantId() + " " + entry.getValue().get(i).getShortName(), entry.getValue().get(i).getPrimaryKey());
site.getChildren().add(plant);
}
}
//Cell Factory is used to effectively turn the tree items into nodes, which they are not natively.
//This is necessary to have actions linked to the tree items (eg. double click an item to open an edit window).
emtTree.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {
#Override
public TreeCell<String> call(TreeView<String> param) {
FactoryTreeCell<String> cell = new FactoryTreeCell<String>();
cell.setOnMouseClicked(event -> {
if (!cell.isEmpty()) {
#SuppressWarnings("unchecked")
TreeItemProperties<String, Integer> treeItem = (TreeItemProperties<String, Integer>) cell.getTreeItem();
generateEquipmentPanes(treeItem.getPropertyValue());
}
});
return cell;
}
});
rootTreeItem.setExpanded(true);
emtTree.setRoot(rootTreeItem);
}
// Populate the main screen with all equipment items in the selected plant.
public void generateEquipmentPanes(int plantId) {
int plant = plantId;
Task<LinkedList<ModelEquipment>> task = new Task<LinkedList<ModelEquipment>>() {
#Override
public LinkedList<ModelEquipment> call() {
LinkedList<ModelEquipment> equipmentList = DAOEquipment.listEquipmentByPlant(plant);
return equipmentList;
}
};
// When list is built successfully, send the results back to the application
// thread to load the equipment panes in the GUI.
task.setOnSucceeded(e -> equipmentPaneBuilder(task.getValue()));
task.setOnFailed(e -> task.getException().printStackTrace());
task.setOnCancelled(null);
String methodName = new Object() {}.getClass().getEnclosingMethod().getName();
Thread thread = new Thread(task);
thread.setName(methodName);
//System.out.println("Thread ID: " + thread.getId() + ", Thread Name: " + thread.getName());
thread.setDaemon(true);
thread.start();
}
// Application thread method to build the equipment panes, used in the
// generateEquipmentPanes method.
public void equipmentPaneBuilder(LinkedList<ModelEquipment> list) {
LinkedList<ModelEquipment> equipmentList = list;
EquipmentPanels.getChildren().clear();
for (int i = 0; i < equipmentList.size(); i++) {
ModelEquipment item = equipmentList.get(i);
try {
PaneEquipment equipmentPane = new PaneEquipment();
equipmentPane.updateFields(item.getTechId(), item.getShortName(), item.getLongDesc()); equipmentPane.setId("equipPane" + i);
EquipmentPanels.getChildren().add(equipmentPane);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I've done a tonne of searching and I sort of figured out how to implement listeners instead of handlers as this seemed to be the way to do what I want - put a listener on a property of the cell. But when replace the event handler with the listener, like the below two examples, I get many issues.
emtTree.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
#SuppressWarnings("unchecked")
TreeItemProperties<String, Integer> treeItem = (TreeItemProperties<String, Integer>) cell.getTreeItem();
generateEquipmentPanes(treeItem.getPropertyValue());
}
});
cell.focusedProperty().addListener(new ChangeListener<Boolean>() {
#Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (!cell.isEmpty()) {
#SuppressWarnings("unchecked")
TreeItemProperties<String, Integer> treeItem = (TreeItemProperties<String, Integer>) cell.getTreeItem();
generateEquipmentPanes(treeItem.getPropertyValue());
}
}
});
For a start I get nullpointerexceptions every time I click on a tree item, which comes from the line generateEquipmentPanes(treeItem.getPropertyValue());. Secondly, it tends to pick data from the wrong item, not the one I selected. Then after a few clicks it seems to break down altogether and do nothing except provide more nullpointerexceptions.
From what I can understand, I think that the issue is the location of the listener relative to the variables that need to be passed to the method generateEquipmentPanes. And something about removing listeners at a certain point and re-adding them later.
Should I somehow be putting the listener into the cell factory? At the moment it just looks like this:
import javafx.scene.control.TreeCell;
public class FactoryTreeCell<T> extends TreeCell<T> {
public FactoryTreeCell() {
}
/*
* The update item method simply displays the cells in place of the tree items (which disappear when setCellFactory is set.
* This can be used for many more things (such as customising appearance) not implemented here.
*/
#Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setGraphic(null); //Note that a graphic can be many things, not just an image. Refer openJFX website for more details.
} else {
setText(item.toString());
}
}
}
Another thing I've noticed is that there are other ways to implement the Callback, that might work better with listeners, but I don't know how to do that.
I've been stuck on this for ages so a breakthrough would be great.
You should not be using a tree cell to examine the selected value. Your ChangeListener already receives the new value directly:
emtTree.getSelectionModel().selectedItemProperty().addListener(
(observable, oldSelection, newSelection) -> {
if (newSelection != null) {
TreeItemProperties<String, Integer> treeItem = newSelection;
generateEquipmentPanes(treeItem.getPropertyValue());
}
});
I've got a project written in JavaFX and I'm trying to get a refresh on a tableview without result.
I've googled around and tried some examples I've found but it still doesn't work.
I populate a tableview with information each row in this table can have new comments added to by double click on the row. The a new Tabpane is opened and the new comment can be added there. On close of this tabpane I'd like the one I clicked from to be refreshed.
I must be doing something wrong. I just don't know what.
In my StoreController
private void populateTableView(List<Store> stores) {
ObservableList<Store> data = FXCollections.observableArrayList(stores);
storeNumberColumn.setCellValueFactory(
new PropertyValueFactory<Store, String>("id"));
storePhoneColumn.setCellValueFactory(
new PropertyValueFactory<Store, String>("phoneNbr"));
chainColumn.setCellValueFactory(
new PropertyValueFactory<Store, String>("chainId"));
commentColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Store, ImageView>, ObservableValue<String>>() {
#Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<Store, ImageView> p) {
Integer numberOfComments = p.getValue().getCommentsCount();
ReadOnlyObjectWrapper wrapper = null;
if (numberOfComments == 0) {
wrapper = null;
} else if (numberOfComments == 1) {
wrapper = new ReadOnlyObjectWrapper(new ImageView(COMMENT_SINGLE_FLAG_SOURCE));
} else {
wrapper = new ReadOnlyObjectWrapper(new ImageView(COMMENT_DOUBLE_FLAG_SOURCE));
}
return wrapper;
}
});
storeTable.setItems(data);
sortTable(storeTable, missedColumn);
}
#FXML
public void handleTableAction(MouseEvent event) {
if (event.getClickCount() == 2) {
showNewCommentStage();
}
}
private void showNewCommentStage() {
initCommentController();
Store store
= storeTable.getSelectionModel().selectedItemProperty().getValue();
commentController.showNewStage(commentPane, store);
}
It seems like the call-function doesn't get called when the commentpane is closed.
CommentController
public void showNewStage(Pane pane, Store store) {
this.store = store;
initStage(pane);
windowHandler = new WindowHandler(stage);
effectHandler.playEffect(pane);
constructCommentHeaders();
List<Comment> comments;
comments = commentService.listByStoreId(store.getId());
populateCommentTable(comments);
}
Like I said I've tried a lot of the solutions found here on Stackoverflow but with no results. The Tableview doesn't refresh. The Stores and the Comments are in different database tables if that's important
Can someone point me in the right direction?
Thanks!
****EDIT****
The Store.class
public class Store extends CommentEntity {
private String id;
private String chainId;
private String phoneNbr;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getChainId() {
return chainId;
}
public void setChainId(String chainId) {
this.chainId = chainId;
}
public String getPhoneNbr() {
return phoneNbr;
}
public void setPhoneNbr(String phoneNbr) {
this.phoneNbr = phoneNbr;
}
#Override
public String toString() {
return "Store{" + "id=" + id + ", chainId=" + chainId + '}';
}
#Override
public String getCommentIdentifier() {
return id;
}
}
The CommentEntity.Class
public abstract class CommentEntity {
private int commentsCount;
public int getCommentsCount() {
return commentsCount;
}
public void setCommentsCount(int commentsCount) {
this.commentsCount = commentsCount;
}
public abstract String getCommentIdentifier();
}
Thank you for input, I hadn't even reflected over the ImageView / String.
Two issues:
First, you need to distinguish between the data the cells in your column are displaying, and the cells that actually display those data. The cellValueFactory determines the data that are displayed. The PropertyValueFactory is a cellValueFactory implementation that references a JavaFX Property, so when you call
storeNumberColumn.setCellValueFactory(new PropertyValueFactory<Store, String>("id"));
it effectively tells the cells in the storeNumberColumn to call the idProperty() method on the Store object in the current row to get the data for the cell. (If no such method exists, it will try to use getId() as a backup plan.)
By default, you get a cellFactory that displays text resulting from calling toString() on the data generated by the cellValueFactory. In the case where your data are simply Strings, this is usually what you need. In other cases, you often need to provide a cellFactory of your own to get the correct way to display the data.
In your case, the data for the commentColumn are simply the number of comments. You are going to display that by choosing an image based on that numeric value.
So you should have
TableColumn<Store, Number> commentColumn = new TableColumn<>("Comments");
For the cellValueFactory, you can just use
commentColumn.setCellValueFactory(new PropertyValueFactory<>("commentsCount"));
Then you need a cellFactory that displays the appropriate ImageView:
commentColumn.setCellFactory(new Callback<TableColumn<Store, Number>, new TableCell<Store, Number>>() {
#Override
public TableCell<Store, Number>() {
private ImageView imageView = new ImageView();
#Override
public void updateItem(Number numberOfComments, boolean empty) {
super.updateItem(count, empty) ;
if (empty) {
setGraphic(null);
} else {
if (numberOfComments.intValue() == 0) {
setGraphic(null);
} else if (numberOfComments.intValue() == 1) {
imageView.setImage(new Image(COMMENT_SINGLE_FLAG_SOURCE));
setGraphic(imageView);
} else {
imageView.setImage(new Image(COMMENT_DOUBLE_FLAG_SOURCE));
setGraphic(imageView);
}
}
}
}
});
The second issue is actually about the update. A TableView keeps its contents "live" by observing JavaFX properties that are provided by the cellValueFactory as ObservableValues. If the value might change while the table is displayed, you must provide an actual property that can be observed: using a ReadOnlyObjectWrapper is no good (because it's read only, so it's wrapped value will not change). The PropertyValueFactory will also return a ReadOnlyObjectWrapper if you do not have JavaFX property accessor methods (i.e. if it is only using getXXX() methods to access the data). So your model class must provide JavaFX Properties.
You can make an immediate fix to this by updating CommentEntity to use an IntegerProperty:
public abstract class CommentEntity {
private final IntegerProperty commentsCount = new SimpleIntegerProperty();
public final int getCommentsCount() {
return commentsCountProperty().get();
}
public final void setCommentsCount(int commentsCount) {
commentsCountProperty().set(commentsCount);
}
public IntegerProperty commensCountProperty() {
return commentsCount ;
}
public abstract String getCommentIdentifier();
}
I would also strongly recommend updating the Store class to use JavaFX Properties in a similar manner.
I'm trying to fill a listview with the artist and title of songs using the open() method.
To achieve this I created the artist and title ArrayLists and merged them using the create() method.
The problem is, when I try to run create() inside open() nothing happens. However, if I assign the create() method to a different button and click it after using the filechooser everything works fine.
So, I would like to know if it is possible to run the create() method after the open() method using only one button via fxml or regular java code.
public class PLController implements Initializable {
#Override
public void initialize(URL location, ResourceBundle resources) {
list.setItems(visibleList);
}
List<File> filelist = new ArrayList<File>();
ArrayList<String> title = new ArrayList<String>();
ArrayList<String> artist = new ArrayList<String>();
ObservableList<String> visibleList = FXCollections.observableArrayList();
#FXML
ListView<String> list;
#FXML
Button impButton;
public void create(){
for(int i = 0; i < title.size(); i++){
visibleList.add(artist.get(i) +" - " +title.get(i));
Collections.sort(visibleList);
}
}
public void handleMetadata(String key, Object value){
if (key.equals("title")){
title.add(value.toString());
}
if (key.equals("artist")){
artist.add(value.toString());
}
}
public void open(){
FileChooser chooser = new FileChooser();
filelist = chooser.showOpenMultipleDialog(impButton.getScene().getWindow());
for(File f:filelist){
try {
Media media = new Media(f.toURI().toURL().toString());
media.getMetadata().addListener(new MapChangeListener<String, Object>(){
#Override
public void onChanged(Change<? extends String, ? extends Object> change) {
if(change.wasAdded()) {
handleMetadata(change.getKey(), change.getValueAdded());
}
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
}
}create(); //Nothing happens
}
As others have pointed out, the Media object does not have its metadata initialized immediately. (It needs to read data from the URL and populate those metadata as it receives them.) That is why the metadata are exposed as an ObservableMap. When you reach the end of your open() method, it is highly unlikely that the metadata will have been initialized, so your create() method will not see any data at that point.
What you need to do is observe the map, and update the ListView once both the artist and title are available. The best way to do this, in my opinion, is to encapsulate the information you want into a separate class:
public class Video {
private final Media media ;
private final ReadOnlyStringWrapper artist = new ReadOnlyStringWrapper("Unknown");
private final ReadOnlyStringWrapper title = new ReadOnlyStringWrapper("Title");
public Video(File file) {
try {
this.media = new Media(file.toURI().toURL().toExternalForm());
artist.bind(Bindings.createStringBinding(() -> {
Object a = media.getMetadata().get("artist");
return a == null ? "Unknown" : a.toString();
}, media.getMetadata()));
title.bind(Bindings.createStringBinding(() -> {
Object t = media.getMetadata().get("title");
return t == null ? "Unknown" : t.toString();
}, media.getMetadata()));
}
catch (Exception e) {
throw new RuntimeException("Could not create Video for "+file, e);
}
}
public ReadOnlyStringProperty titleProperty() {
return title.getReadOnlyProperty();
}
public ReadOnlyStringProperty artistProperty() {
return artist.getReadOnlyProperty();
}
public final String getTitle() {
return title.get();
}
public final String getArtist() {
return artist.get();
}
public final Media getMedia() {
return media ;
}
}
Now you can create a ListView<Video> to display the videos. Use a cell factory to display the artist and the title in the format you want. You can make sure that the observable list fires updates when either the artist or title properties change, and you can keep it sorted via a SortedList.
#FXML
private ListView<Video> list ;
private ObservableList<Video> visibleList ;
public void initialize() {
visibleList = FXCollections.observableArrayList(
// make list fire updates when artist or title change:
v -> new Observable[] {v.artistProperty(), v.titleProperty()});
list.setItems(new SortedList<>(list, Comparator.comparing(this::formatVideo)));
list.setCellFactory(lv -> new ListCell<Video>() {
#Override
public void updateItem(Video item, boolean empty) {
super.updateItem(item, empty) ;
setText(formatVideo(item));
}
});
}
#FXML
private void open() {
FileChooser chooser = new FileChooser();
List<File> fileList = chooser.showOpenMultipleDialog(impButton.getScene().getWindow());
if (fileList != null) {
fileList.stream()
.map(Video::new)
.forEach(visibleList::add);
}
}
private String formatVideo(Video v) {
if (v == null) return "" ;
return String.format("%s - %s", v.getArtist(), v.getTitle());
}
Simply creating a Media object and assigning a listener to it won't fire the code in the listener. So the title list in your code remains empty. The create() method is called, but since you are iterating over an empty list, nothing actually happens.
Use a debugger or add some logging information in such cases.
Also, you should sort the list after the for loop, not every time you add an item.
I have a java bean object list which I would like to display in ListView control. By default ListView uses toString method.
How can I define which property to use for rendering in ListView?
I want to achieve same functionality as in TableView can be achieved by PropertyValueFactory in this code:
#FXML
private TableView<Person> mainTableView;
//...
TableColumn<Person,String> personColumn = new TableColumn<>("Name");List
personColumn.setCellValueFactory(new PropertyValueFactory("name"));
mainTableView.getColumns().add(personColumn);
Edit
It looks like there is no easy(out of the box) solution. Based on code from James_D I created generic class to deal with the problem. It wraps PropertyValueFactory - note that PropertyValueFactory firstly looks for method [NAME]Property() trying to get observable, only when it is not found it tries to access standard bean properties.
public class PropertyValueFactoryWrapperCellFactory<T> implements Callback<ListView<T>, ListCell<T>> {
private final PropertyValueFactory<T, String> pvf;
public PropertyValueFactoryWrapperCellFactory(String propertyName) {
super();
pvf = new PropertyValueFactory(propertyName);
}
#Override
public ListCell<T> call(ListView<T> param) {
return new ListCell<T>() {
#Override
public void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
textProperty().unbind();
if (item == null) {
return;
}
TableColumn.CellDataFeatures<T, String> cdf = new TableColumn.CellDataFeatures<>(null, null, item);
textProperty().bind(pvf.call(cdf));
}
};
}
}
Usage:
#FXML
private ListView<Person> mainListView;
//...
mainListView.setCellFactory(new PropertyValueFactoryWrapperCellFactory("name"));
Use a cell factory.
If the property is immutable, it's pretty straightforward:
ListView<MyDataType> listView = new ListView<>();
listView.setCellFactory(new Callback<ListView<MyDataType>, ListCell<MyDataType>>() {
#Override
public ListCell<MyDataType> call(ListView<MyDataType> lv) {
return new ListCell<MyDataType>() {
#Override
public void updateItem(MyDataType item, boolean empty) {
super.updateItem(item, empty);
if (item == null) {
setText(null);
} else {
// assume MyDataType.getSomeProperty() returns a string
setText(item.getSomeProperty());
}
}
};
}
});
If the property can change its value and the list view needs to update dynamically in response to these changes, you need to bind the textProperty of the list cell:
listView.setCellFactory(new Callback<ListView<MyDataType>, ListCell<MyDataType>>() {
#Override
public ListCell<MyDataType> call(ListView<MyDataType> lv) {
return new ListCell<MyDataType>() {
#Override
public void updateItem(MyDataType item, boolean empty) {
super.updateItem(item, empty);
textProperty().unbind();
if (item == null) {
setText(null);
} else {
// assumes MyDataType.someProperty() returns a StringProperty:
textProperty.bind(item.someProperty());
}
}
};
}
});
Binding String Property
This code allows you to choose what JavaFX property is displayed in a ListView. I use an anonymous class inside a lambda to add some clarity to what is happening. In this example:
Display - The underlying object behind the ListView
titleProperty - The JavaFX property to be displayed
listView.setCellFactory(
listview -> {
return new ListCell<Display>() {
#Override
public void updateItem(Display item, boolean empty) {
super.updateItem(item, empty);
textProperty().unbind();
if(item != null)
textProperty().bind(item.titleProperty());
else
setText(null);
}
};
}
);
Explanation
With this code, we are basically making a custom ListCell. When it is updated and the item it is display is null, we clear the display text. Otherwise, we set the text to be whatever the title property of our item is.
TL;DR :: Modified James_D's Solution
I based this on James_D's second example. I wanted to bind a SimpleStringProperty to be the displayed text in a ListView. James_D's solution worked great but didn't update when I deleted an object from the ObservableList in the ListView, so I modified it. I also thought having a cleaner lambda example would be good.
I've developed a simple custom TableCell to enable the edition of the values in a table. The behaviour of the component is show a BigDecimalTextField no matter if the user is editing or not that cell, it should enable the edition all the time. The component is working fine, there is only a strange problem: when the table is rendered, instead of show only a single line, three lines are shown:
The code of the component is this:
public class BigDecimalEditingCell extends TableCell {
private BigDecimalField spinner = new BigDecimalField(new BigDecimal("0.00"));
private ObjectProperty<BigDecimal> campoLigado = null;
public BigDecimalEditingCell() {
this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
createField();
}
private void createField() {
spinner.setStepwidth(new BigDecimal("0.01"));
spinner.setMinValue(new BigDecimal("0.00"));
spinner.setFormat(NumberFormat.getInstance());
spinner.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
}
#Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
criarBind();
setGraphic(spinner);
setText(null);
}
private void criarBind() {
ObservableValue<BigDecimal> valor = getTableColumn().getCellObservableValue(getIndex());
if (valor != null) {
ObjectProperty<BigDecimal> propriedade = (ObjectProperty<BigDecimal>) valor;
if (campoLigado == null) {
spinner.numberProperty().bindBidirectional(propriedade);
campoLigado = propriedade;
} else if (campoLigado != propriedade) {
spinner.numberProperty().unbindBidirectional(campoLigado);
spinner.numberProperty().bindBidirectional(propriedade);
campoLigado = propriedade;
}
}
}
}
If I use the default TextFieldTableCell, the table is rendered correctly. I have another component (like this) that uses JavaFX's DatePicker and the same problem happens.
What I'm doing wrong?
ADDED
Here is the usage of this component:
public class ControladorPainelFormaPagamento extends ControladorPainelSubmeter {
#FXML
private TableView<ParcelaBean> tabela;
#FXML
private TableColumn<ParcelaBean, LocalDate> colunaVencimento;
#FXML
private TableColumn<ParcelaBean, BigDecimal> colunaValor;
private FormaPagamentoBean bean;
.
.
.
private void configurarColunaValor() {
colunaValor.setCellValueFactory(new PropertyValueFactory<ParcelaBean, BigDecimal>("valor"));
colunaValor.setCellFactory(new Callback<TableColumn<ParcelaBean, BigDecimal>, TableCell<ParcelaBean, BigDecimal>>() {
#Override
public TableCell<ParcelaBean, BigDecimal> call(TableColumn<ParcelaBean, BigDecimal> parcelaBeanStringTableColumn) {
return new BigDecimalEditingCell();
}
});
}
private void configurarColunaVencimento() {
colunaVencimento.setCellValueFactory(new PropertyValueFactory<ParcelaBean, LocalDate>("dataVencimento"));
}
public void carregar(ModoExibicao modoExibicao, FormaPagamentoBean formaPagamento) {
this.bean=formaPagamento;
tabela.setItems(bean.getParcelas());
.
.
.
}
.
.
.
}
I've checked, inclusive using debug, if there was more than one bean in the list used by the table. Every time only one was there.
I was looking at another table in the same system and noticed this: when there is no item in the table, no one row is rendered.
but when you add just one row, the table renders empty row until reach the height of the table (repair the light grey rows):
So, it was obvious that the extra rows were added by TableView, this way a simple null check solve the problem:
public class BigDecimalEditingCell extends TableCell {
private BigDecimalField element = new BigDecimalField(new BigDecimal("0.00"));
private ObjectProperty<BigDecimal> campoLigado = null;
#Override
protected void updateItem(Object item, boolean empty) {
super.updateItem(item,empty);
if (getIndex() < getTableView().getItems().size() && !empty) {
createField();
createBind();
setGraphic(element);
setText(null);
} else {
removeBind();
setGraphic(null);
}
}
.
.
.
}