How can I get the contents of a JavaFX TableColumn? - java

If I have a TableView in JavaFX with some TableColumn's is there a way I can get the data in that column's cells?
TableColumn concentrationCol = new TableColumn("Initial Concentration");
concentrationCol.setMinWidth(150);
concentrationCol.setCellValueFactory(
new PropertyValueFactory<SpeciesDoubleWrapper, String>("d"));
concentrationCol.setCellFactory(TextFieldTableCell.forTableColumn());
concentrationCol.setOnEditCommit(
new EventHandler<TableColumn.CellEditEvent<SpeciesDoubleWrapper, String>>() {
#Override
public void handle(TableColumn.CellEditEvent<SpeciesDoubleWrapper, String> t) {
((SpeciesDoubleWrapper) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setD(t.getNewValue());
}
}
);
This is the code I use to create my TableColumn. I know there is a function getCellObservableValue(int i) which returns the contents of the ith cell, but when I use it I get ObjectProperty [value: 0.0] returned instead of 0.0, which is the value of the cell.

You can call getValue method of extended class ObjectExpression:
getCellObservableValue(0).getValue()
https://docs.oracle.com/javase/8/javafx/api/javafx/beans/binding/ObjectExpression.html#getValue--

You are getting a Property, and more precisely an ObjectProperty.
These classes are used for the concept of data binding. To make it short, data binding allows JavaFX to keep track of the values if they change and automatically update the view accordingly.
To get the value, just call get().

Related

JavaFX complex string binding

I'm new to JavaFX and was wondering if the Bindings API allowed an easier way to achieve the following. Consider a model that contains a database that may be null (because the database loads asynchronously) and a view that displays a label status reflecting the state of the database. If it is null it should say something like "Loading..." and if it isn't it should display how many items are in the database. It also would be great if the status could reflect the size of the database as it grows or shrinks.
So far, I understand that I could bind an integer property (size of the database) to the text property of the label by using a converter. This is fine, but I want the label to display more than the number. A localized string like "Loaded {0} items" precisely. And let's not forget that the database may still be null.
This is the solution I have in place
#Override
public void initialize(URL url, ResourceBundle bundle) {
// Initialize label with default value
status();
model.databaseProperty().addListener((obs, old, neu) -> {
// Update label when database is no longer null
status();
// Update label when size of database changes
neu.sizeProperty().addListener(x -> status());
});
}
public void status() {
if (model.database() == null) {
status.setText(bundle.getString("status.loading"));
} else {
String text = bundle.getString("status.ready");
int size = model.database().size();
text = new MessageFormat(text).format(size);
status.setText(text);
}
}
It works, but is there a way to do it with a chain of bindings, or at least part of it? I've seen how powerful (and lenghty) boolean bindings can be but I'm not sure something as flexible is possible with string bindings.
You can use Bindings.when, which is essentially a dynamic if/then binding:*
status.textProperty().bind(
Bindings.when(model.databaseProperty().isNull())
.then(bundle.getString("status.loading"))
.otherwise(
Bindings.selectInteger(model.databaseProperty(), "size").asString(
bundle.getString("status.ready")))
);
However, the above assumes bundle.getString("status.ready") returns a java.util.Formatter string, not a MessageFormat string. In other words, it would need to be "Loaded %,d items" rather than "Loaded {0,number,integer} items".
Bindings doesn’t have built-in support for MessageFormat, but if you really want to stick with MessageFormat (which is a legitimate requirement, as there are things MessageFormat can do which Formatter cannot), you can create a custom binding with Bindings.createStringBinding:
MessageFormat statusFormat = new MessageFormat(bundle.getString("status.ready"));
status.textProperty().bind(
Bindings.when(model.databaseProperty().isNull())
.then(bundle.getString("status.loading"))
.otherwise(
Bindings.createStringBinding(
() -> statusFormat.format(new Object[] { model.getDatabase().getSize() }),
model.databaseProperty(),
Bindings.selectInteger(model.databaseProperty(), "size")))
);
* Actually, it’s more like the ternary ?…: operator.

add property to Cell (in code)

I'm using GWT primarily in code. I currently have a column with a TextInputCell:
public class EditPanel extends Composite{
#UiField
protected DataGrid<MyObject> dataGrid;
public EditPanel(final ActionHandler actionHandler){
...
Column<MyObject, String> inputColumn = new Column<MyObject, String>(new TextInputCell()){
... // Override of the getValue
};
inputColumn.setFieldUpdater(...);
this.dataGrid.addColumn(inputColumn, "Column title");
...
}
...
}
Based on this SO answer I know I can add a placeholder-text (or any other property) to a regular TextField in GWT like this:
TextField myInputField = new TextField();
myInputField.getElement().setPropertyString("placeholder", "some placeholder text");
However, on the TextInputCell there isn't really a getElement() method to retrieve the input-field.
When looking through the TextInputCell-class code I came across the protected getInputElement(parent) method, so I did managed to get a placeholder with the following work-around:
final TextInputCell myInputCell = new TextInputCell(){
#Override
protected InputElement getInputElement(final Element parent){
final InputElement inputElement = super.getInputElement(parent);
inputElement.setPropertyString("placeholder", "my placeholder text");
return inputElement;
}
};
Column<MyObject, String> inputColumn = new Column<MyObject, String>(myInputCell){
...
};
It works, but I have two concerns:
Obviously it is very ugly to set it like this..
The getInputElement(parent)-method isn't called initially. I do get the placeholder when I focus one of the input-field, but the property is not always added by default..
Does anyone have an actual solution of how to add a Property to the (TextInput)Cell of a Column, instead of this ugly maybe-working work-around?
EDIT: Some things I've tried:
1) Trying to retrieve the element with the getRowElement method, like in this SO question & answer:
this.dataGrid.addColumn(inputColumn, "Column title");
if (this.dataGrid.getRowCount() > 0){
final Element element = this.dataGrid.getRowElement(0);
element.setProperty("placeholder", "some placeholder text");
}
This doesn't work because getRowCount() always returns 0. I also tried a this.dataGrid.redraw(); right before it, but it still returns 0.
2) Overriding the TextInputCell as geert3 suggested:
public class MyTextInputCell extends TextInputCell{
#Override
public InputElement getInputElement(Element parent){
return super.getInputElement(parent);
}
}
The problem? I don't know what to put in for the parent-parameter. I did try this.getElement():
MyTextInputCell textInputCell = new MyTextInputCell();
Column<MyObject, String> inputColumn = new Column<MyObject, String>(textInputCell){
... // Override of the getValue
};
inputColumn.setFieldUpdater(...);
this.dataGrid.addColumn(inputColumn, "Column title");
textInputCell.getInputElement(this.getElement()).setPropertyString("placeholder", "some placeholder text");
but that doesn't seem to work. And when I try this, this.dataGrid or inputColumn it gives an error because they aren't considered as Elements (and I also can't cast them).
3) Using my initial work-around.
Problem: I can't really find a way to enforce the getInputElement(parent) method call when the page loads, instead of when an input-field is focused.
Pretty annoying that there is no way to directly access the InputFields of a TextInputCell in GWT...
I've been reading up a bit on Cells. These are in fact reused, a single Cell can render multiple DOM instances of itself, and it can handle events from multiple rendered instances. This means that a Cell doesn't have a one-to-one associated Element, but receives the "current" element as it is being reused, passed as the "parent" argument to several methods.
So in conclusion I think this means that the original solution you've described in your question appears to be a valid solution, not "ugly" at all.

JavaFx use String with Double on table column

I have a class called "Product", with a double attribute "price". I'm showing it on a table column inside a table view, but i wanted to show the price formatted -- "US$ 20.00" instead of just "20.00".
Here's my code for populating the table view:
priceProductColumn.setCellValueFactory(cellData -> cellData.getValue().priceProperty());
I tried everything: convert the returned value to a string, using the method toString that priceProperty has, etc, but not seems to work.
Do i need to bind an event of something like that?
Use the cellValueFactory as you have it to determine the data that is displayed. The cell value factory is basically a function that takes a CellDataFeatures object and returns an ObservableValue wrapping up the value to be displayed in the table cell. You usually want to call getValue() on the CellDataFeatures object to get the value for the row, and then retrieve a property from it, exactly as you do in your posted code.
Use a cellFactory to determine how to display those data. The cellFactory is a function that takes a TableColumn (which you usually don't need) and returns a TableCell object. Typically you return a subclass of TableCell that override the updateItem() method to set the text (and sometimes the graphic) for the cell, based on the new value it is displaying. In your case you get the price as a Number, and just need to format it as you require and pass the formatted value to the cell's setText(...) method.
It's worth reading the relevant Javadocs: TableColumn.cellFactoryProperty(), and also Cell for a general discussion of cells and cell factories.
priceProductColumn.setCellValueFactory(cellData -> cellData.getValue().priceProperty());
priceProductColumn.setCellFactory(col ->
new TableCell<Product, Number>() {
#Override
public void updateItem(Number price, boolean empty) {
super.updateItem(price, empty);
if (empty) {
setText(null);
} else {
setText(String.format("US$%.2f", price.doubleValue()));
}
}
});
(I'm assuming priceProductColumn is a TableColumn<Product, Number> and Product.priceProperty() returns a DoubleProperty.)
If you have not, read this together with #James_D post.
https://docs.oracle.com/javafx/2/ui_controls/table-view.htm

How to hide TableView column header in JavaFX 8?

I need to have an observable list of a type that will be displayed in a TableView with one single column, that when selected will display the rest of its information on the right. The TableView is wrapped in a TitledPane, which is wrapped in an Accordion. See image below:
As you can see in this scenario I don't want to show the Column Header.
I tried following the instruction here, which leads to here:
Pane header = (Pane) list.lookup("TableHeaderRow");
header.setMaxHeight(0);
header.setMinHeight(0);
header.setPrefHeight(0);
header.setVisible(false);
However, it appears to not be working for JavaFX 8. The lookup("TableHeaderRow") method returns null which makes me think that the "TableHeaderRow" selector no longer exist.
Is there an updated workaround for removing/hiding the table header in JavaFX 8?
I faced the problem of hiding column headers recently and could solve it using css.
I created a styleclass:
.noheader .column-header-background {
-fx-max-height: 0;
-fx-pref-height: 0;
-fx-min-height: 0;
}
and added it to the TableView:
tableView.getStyleClass().add("noheader");
Just in case someone needs an alternative approach. It also gives the flexibility of toggling column headers.
As observed in the comments, lookups do not work until after CSS has been applied to a node, which is typically on the first frame rendering that displays the node. Your suggested solution works fine as long as you execute the code you have posted after the table has been displayed.
For a better approach in this case, a single-column "table" without a header is just a ListView. The ListView has a cell rendering mechanism that is similar to that used for TableColumns (but is simpler as you don't have to worry about multiple columns). I would use a ListView in your scenario, instead of hacking the css to make the header disappear:
ListView<Album> albumList = new ListView<>();
albumList.setCellFactory((ListView<Album> lv) ->
new ListCell<Album>() {
#Override
public void updateItem(Album album, boolean empty) {
super.updateItem(album, empty);
if (empty) {
setText(null);
} else {
// use whatever data you need from the album
// object to get the correct displayed value:
setText(album.getTitle());
}
}
}
);
albumList.getSelectionModel().selectedItemProperty()
.addListener((ObservableValue<? extends Album> obs, Album oldAlbum, Album selectedAlbum) -> {
if (selectedAlbum != null) {
// do something with selectedAlbum
}
);
There's no need for CSS or style or skin manipulation. Simply make a subclass of TableView and override resize, like this
class XTableView extends TableView {
#Override
public void resize(double width, double height) {
super.resize(width, height);
Pane header = (Pane) lookup("TableHeaderRow");
header.setMinHeight(0);
header.setPrefHeight(0);
header.setMaxHeight(0);
header.setVisible(false);
}
}
This works fine as of June 2017 in Java 8.
Also, I would recommend using this nowadays.
tableView.skinProperty().addListener((a, b, newSkin) -> {
TableHeaderRow headerRow = ((TableViewSkinBase)
newSkin).getTableHeaderRow();
...
});
This can be executed during initialization, the other method as mention above, will return null, if run during initialization.
Combining the last two answers for a more generic solution without the need to override methods because getTableHeaderRow is no longer visible to be accessed. Tested with Java 11:
private void hideHeaders() {
table.skinProperty().addListener((a, b, newSkin) ->
{
Pane header = (Pane) table.lookup("TableHeaderRow");
header.setMinHeight(0);
header.setPrefHeight(0);
header.setMaxHeight(0);
header.setVisible(false);
});
}

Issue with default Sorting on Virtual Table & ViewerComparator

We have a Virtual Table in my Eclipse RCP application. We make a call to the backend to retrieve the data to be populated in the virtual table.
We want default sorting on the table on a single column. We use ViewerComparator to achieve sorting functionality. My problem is, I am not able to get this sorting working when the table loads with the data for the 1st time. But when I click on the column, everything works fine as expected.
This is how, I set the Comparator to the column
TableViewerColumn tvc = viewer.addColumn(100, SWT.LEFT, "Name");
viewer.setColumnComparator(tvc,
new Comparator<Person>() {
#Override
public int compare(Person o1,Person o2) {
double firstValue = Double.parseDouble(o1
.getAge());
double secondValue = Double.parseDouble(o2
.getAge());
return firstValue > secondValue ? 1 : -1;
}
});
setColumnComparator method in custom viewer
public void setColumnComparator(TableViewerColumn tvc, Comparator<T> cmp){
final MyViewerComparator c = new MyViewerComparator(cmp);
final TableColumn tc = tvc.getColumn();
setComparator(c);
getTable().setSortDirection(c.getDirection());
getTable().setSortColumn(tc);
refresh();
tc.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
<same code as above>
}
});
MyViewerComparator
class MyViewerComparator extends ViewerComparator{
Comparator<T> cmp;
boolean desc = true;
MyViewerComparator(Comparator<T> cmp){
this.cmp = cmp;
}
int getDirection(){
return desc?SWT.UP:SWT.DOWN;
}
void flipDirection(){
desc = !desc;
}
#Override
public int compare(Viewer viewer, Object e1, Object e2) {
if(e1 == null || e2==null){
return 0;
}
int rc = cmp.compare((T)e1, (T)e2);
if(desc)
return -rc;
return rc;
}
}
When the table loads the data for the 1st time, it goes inside the Bolded condition in the above code as one of the object is ALWAYS NULL
Note: This functionality works totally fine if I use a Standard table rather than VIRTUAL TABLE. I am not sure whether I can change it to use Standard table as we want the lazy load functionality as well..
ContentProvider used is: ObservableListContentProvider
Please advise..
A late answer that hopefully still helps others. I encountered exactly the same problem when using SWT.VIRTUAL with an ObservableListContentProvider in combination with sorting.
The original intent of SWT.VIRTUAL is that not all elements in the contents need to be fetched to show only part of the contents. A custom content provider needs to be implemented which only has to return the elements that need to be currently shown on the screen. You also have to tell the table the total number of elements in existence. In such a use case, a table cannot be sorted in the normal way with a ViewerComparator because not all elements are known. However SWT.VIRTUAL can also be used as a performance optimization for rendering a table with many elements. This seems to work fine with the non-observable ArrayContentProvider.
But when using ObservableListContentProvider I am seeing exactly the same issue as you have. Somehow it tries to be smart and update only the elements that have actually changed. Somewhere in the depths of it's implementation something goes wrong for virtual tables, I have no clue exactly what. But I do have a solution: don't use ObservableListContentProvider at all and simply refresh the table viewer. You can e.g. use a plain ArrayContentProvider and add the following listener to the IObservableList contents of the viewer:
new IListChangeListener() {
#Override
public void handleListChange(ListChangeEvent event) {
viewer.refresh();
}
};
I actually implemented my own "SimpleObservableListContentProvider" that does exactly this, but also takes care of switching table input by implementing the inputChanged method to remove this listener from the old input list and add it to the new one.

Categories