• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with setCellValueFactory

 
Clark Sann
Greenhorn
Posts: 5
Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a problem using setCellValueFactory using lambdas.

If I have a class with two properties, one a SimpleStringProperty and the other a SimpleLongProperty.

I can use the setCellValueFactory to configure a TableView column with the SimpleStringProperty but not for the SimpleLongProperty.

Here is the class defining the data to be put in the table:



Here is my code to configure the tableview columns:



The first statement works, the second doesn't.
The second line throws an error that says "Incompatible return type SimpleLongProperty in lambda expression"

I am using IntelliJ. I have read that some earlier versions (I am using the latest version 14.0.2), threw incorrect errors similar to this. Their web site says this bug has been fixed.

Is this a problem with IntelliJ or is there some reason lambdas cannot be used in this manner with SimpleLongProperty?
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For similar situations, I have used the PropertyValueFactory with CellValueFactory, like:
new PropertyValueFactory<>("minValue");

and, Welcome to the Ranch!
 
Clark Sann
Greenhorn
Posts: 5
Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranga

I am currently using PropertyValueFactory but I am trying to convert to lambda expressions. There are two reasons I want to do this. One is to learn to use lambda expressions. The other is to get away from having to use the string to define the property.

I have also made a simple program to test this using Netscape and it doesn't work there either. Actually on Netscape it is even worse - the getValue method does not appear to exist when I use Netscape so I am not even able to enter the lambda expression for the SimpleStringProperty.

ARGGGG!

Clark
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, lambdas will be needed if we are dealing with a POJO. However, with a JavaFX style JavaBean, PropertyValueFactory works (for editing too).
 
Clark Sann
Greenhorn
Posts: 5
Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found how to modify the lambda expression to make it work with SimpleLongProperty

colStudy.setCellValueFactory(cellData -> cellData.getValue().parameterProperty());
colMin.setCellValueFactory(cellData -> cellData.getValue().minValueProperty().asObject());

As shown in the 2nd line, add the asObject() method.
 
Chris R Olsen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clark --

I am getting similar error messages, and I'm wondering if what you are attempting to do is similar to what I want to do. I am a newbie to FX, and unfamiliar with CellValueFactory, but perhaps you can help.

What I want to do is create a dynamic table, one that does not know in advance how many columns it has. (I'm actually using as a springboard for my understanding a program I found here at the Ranch.) What I am trying to do is create a sort of Extremely-Mini-Spreadsheet for doing statistical analysis. The TableView would be the vehicle for data entry; in addition the program will read data from files and populate the TableView. My idea thus far has been to try to create an array of SimpleStringProperties for the data. (Strings b/c of the need to parse the data; some data will be Yes/No, Blue/Green/Red, etc.) My Netbeans IDE does not complain when I ask for such an array, and I even get column headings correctly. However, when I try to get() and set() values, I get tsk-tsks about incompatible return types. I believe the problem I am trying to get around is that the property name seems to be hard-coded as part of the get/set statements. In order to try to get the syntax correct, I am modifying the Ranch contribution. Here is where I'm running into incompatibility complaints. (Apologies, I don't know how to insert the code with those nifty numbers.)

//Create a new table show details of the selected item
Record selectedRecord = (Record) tblView.getItems().get(selectdIndex);
ObservableList<SubRecord> subDataList
= FXCollections.observableArrayList(
new SubRecord("ID", selectedRecord.getDaInteger(0)), // <--------------------------
new SubRecord("Monday", selectedRecord.getValue_0()),

What I THINK I want to do is have the "selectedRecord" part somehow be a variable rather than hard-wired. Would CellValueFactory somehow let me do this?

And if not, does anyone have a better strategy?

Thanks to all in advance!!

-- Chris

 
Clark Sann
Greenhorn
Posts: 5
Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris

I am not sure what you are trying to do or what your exact problem is. But let me review what I understand about setCellValueFactory.

setCellValueFactory is used to associate a column in a TableView with a property of a class that contains the data to be displayed in the TableView's column. An ObservableList will then be created containing several instances of the class, each containing data. A row in the table will be created for each instance of the class.

For example, here is a class containing several properties.



Now that the class is created, instances of the class are created and placed into an ObservableList. Here is an example of this:



This ObservableList has two entries so the TableView will have two rows.

The last thing to do is to associate the columns in the TableView with properties in the class in the ObservableList. That is done with the setCellValueFactory method. There are two ways to use this method.

Method 1 (the old method)



This informs the TableViewColumn that it will acquire the data for the column named "colStudy" from the property named "parameter". Note that rather than hardcoding the property name, you could also put a string variable in as follows:



This would allow you to dynamically name the property.

Method 2 (new method using lambda expression)



This method allows you to assign the column to the property in a more direct manner without using a fixed string. Note that in the case above, parameterProperty is a SimpleStringProperty. Had the property been a SimpleIntegerProperty, you would have to modify the statement by adding the AsObject method as follows:



I am not sure I am answering your question, but I hope I have shed some light on the purpose of setCellValueFactory. Here is a link to an article that explains this procedure a bit further.

Table View Documentation

I am not sure this will help you to dynamically add columns. I read somewhere that it might be better to use DataFX.

Let me know if this helped. Also let me know if it didn't. Maybe I can help more once I have a better understanding of where your problem is.

Clark
 
Chris R Olsen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clark --

This is very helpful! Thank you. The Ranch has been very helpful with several Java and FX problems I've encountered along the way with this project. In general, part of what I am trying to do is write a scatterplot program, the kind of thing you see in Algebra I. (Technically it is part of a larger statistics project.) I want to be able to read in data from a CSV file, show the results in an editable TableView for possible change, and plot the points. I also want to be able to to click and drag on the x- and y-axis to move it as desired. The last problem I have to solve is the TableView part.

I am hoping to "pay back" the Ranch and post this when I get it done, thinking that others may have similar problems.

Thanks again!

-- Chris
 
Clark Sann
Greenhorn
Posts: 5
Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an article that discusses populating a TableViewfrom a tab delimited text file....almost the same as a CSV file.

It creates an ObservableList of Lists or ObservableLists. Because the List or ObservableList can be any length, the number of columns will vary.

Creating columns dynamically

I haven't taken the time to study this code yet, but it does make sense to have a OL of OLs.

Let me know what you think of this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic