javafx treetableview cell value not getting updated - javafx

Event after updating the data model javafx treetableview cell value not getting updated.
I am using the sample code here # http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/tree-table-view.htm
(Example 15-2)
On click of button i m trying to update first item: employees.get(0).setName("Test");
Is there any trick using which treetableview can be updated?

The example, somewhat strangely, returns a ReadOnlyStringWrapper wrapping the property values for the cell value factories. Thus instead of binding the value displayed in the column directly to the properties in the Employee class, it binds them to a new, read-only, property wrapping the value retrieved when updateItem(..) is called on the cell. This means it won't get updated when the underlying data is updated, but only if the updateItem(...) method is invoked on the cell. (I have no idea why they would do this.) So you should find, for example, that if you change the value, then collapse the root node in the TreeTableView and expand it again, that your new value is displayed after expanding the root (because this causes the cells' updateItem(...) methods to be invoked).
To make the cells update when the data is changed, bind the cell value directly to the property defined in the model (Employee) class:
empColumn.setCellValueFactory( param -> param.getValue().getValue().nameProperty());
and
emailColumn.setCellValueFactory( param -> param.getValue().getValue().emailProperty());

Related

Can tree column be hidden programmatically in TreeTableView of JavaFX?

I have a useful TreeTableView GUI view definition builder and would like to use in on tabular data where I don't need drill downs. I tried ...
m_treeTableView.getTreeColumn().setVisible(false);
... but it doesn't hide the tree column. Any help?
getTreeColumn appears to be returning null regardless of the thread or delayed execution I call it from. However, the column exists at index zero in the TreeTableView vector of columns. Hiding it by index works fine but gives unpredictable results if drill-down is defined.
However, TreeTableView hides the column automatically and elegantly if no drill-down is defined - logical behaviour and exactly what I need.
Edit: getTreeColumn returns null even if the column is not null, but is at its default position, leftmost place. Otherwise it returns the column.

How to use setCellValueFactory and setCellFactory?

How do we use setCellValueFactory and setCellFactory?
What's difference between them?
According to the documentation :
setCellValueFactory
The cell value factory needs to be set to specify how to populate all cells within a single TableColumn. A cell value factory is a Callback that provides a TableColumn.CellDataFeatures instance, and expects an ObservableValue to be returned. The returned ObservableValue instance will be observed internally to allow for immediate updates to the value to be reflected on screen.
setCellFactory
The cell factory for all cells in this column. The cell factory is responsible for rendering the data contained within each TableCell for a single table column.
By default TableColumn uses the default cell factory, but this can be replaced with a custom implementation, for example to show data in a different way or to support editing.
Resource :http://docs.oracle.com/javase/8/javafx/api/toc.htm and http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TableColumn.html#setCellFactory-javafx.util.Callback-

Flex - ComboBox labelFunction Not Refreshing With Updating of an ArrayCollection

I have two issues with my ComboBox.
My first issue is that when my ArrayCollection only has one item, for some reason I cannot select that first item. The change function set on the mxml never gets called when I try to select that one item. If I set a prompt, it works. I know that the item is not already selected because when debugging, the selected item shows null. I do not want to use a prompt. I want the first item in the ArrayCollection to be selected automatically. So if there is only one item, I want the selected item to be that. FYI - I am using a labelFunction to format the data in the Array Collection. Any idea why I cannot select the first item or set the first item in the ArrayCollection to be the selected item?
My bigger issue is that when my Array Collection gets updated, my label function must not be refreshing because when I first open the dropdown, it has specfic data. When the Array Collection gets updated and I open the dropdown again, I see the old data in the dropdown, yet it doesn't exist anymore.
So let's say I have a ComboBox that has a datasource of an array collection of one state code and it's state name. Their is a labelFunction that puts a dash in between the state code and the state name. Their is no prompt, so the ComboBox would look like GA - Georgia and when you open the dropdown, that would be all that is there. I want that item to be selected automatically. Any idea why the old data shows up when opening the dropdown after the array collection was updated with new data?
<mx:ComboBox labelFunction="getFormattedNpaNxxCollectionList()"
dataProvider="arrayColl" change="doSomething()"/>
public function getFormattedNpaNxxCollectionList(item:Object):String
{
return StringUtil.substitute("{0} - {1}", item.stateCode, item.stateName);
}
Is the arrayColl declared as [Bindable] - the updates won't be reflected automatically if its not bindable. Post the code where you're updating the collection.
To select the first item if there's just one item after updating the array collection, you can call
if(arrayColl.length == 1)
cb.selectedIndex = 0;
after the update.
If you are only updating the values in the collection (and not reassigning a whole new array collection object to arrayColl), you can do this from the collectionChange event handler of the array collection. Otherwise you have to do this after assigning the new collection object to the arrayColl variable.

how can i passing value to combobox in flex?

i have combobox in one screen(namely 1) and i need to pass the selected value of the combobox to the another combobox which is in the another screen(namely 2) and the passed value should be selected by default.
In simple terms (if both comboboxes have plain strings or same set of objects in their data providers):
combobox2.selectedItem = combobox1.selectedItem;
If that is not the case, you will have to get a mapping from items of first combobox to the second one.

Flex DataGridColumn with array of objects as data provider

I have a datagrid that uses an array of objects as the data provider. The objects are essentially key/value pairs:
{ foo:"something"}
{ bar:"hello"}
{ caca:"lorem"}
The datagrid has 2 columns. The first column is the key and the second column is the value. Right now my grid looks like:
My dataFormatter function makes sure that depending on the column (i.e. the dataField value) the correct key or value gets printed out. This works fine for displaying. However, as soon as I try and edit the value field it essentially adds a new value into the object with a key of '1'. For example, if I edit the {caca:"lorem"} object it will then contain the value {caca:"lorem",1:"new value"}.
Is there any possible way I can set the DataGridColumn so that when I edit a value it will update the value associated with the key rather than inserting a new value? I've tried using a custom item editor but it still does the insert. It seems like I need to be able to update the 'dataField' with the actual key value but I'm not sure how to do that.
Looks like you need to think about where your data is going to be stored. I would recommend listening for a CollectionEvent.COLLECTION_CHANGE event on your data model. That event object will contain info about what change occurred, and you can make any updates you need to make.

Resources