• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

!! I need some help understanding tableviews

 
Ranch Hand
Posts: 34
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
I have started using Javafx for GUIs, and I was trying to make a TableView that retrieves data from an mySQL server, and displays it.  

I retrieved the data, correctly displayed the window, and all nodes inside, but the TableView shows nothing.  
I have been trying to wrap my head around how cellValueFactory works, but I can't understand it.
I have 1 problem, and 2 questions that I could not find an answer.  


Problem : My TableView creates a single cell(This is correct, since theres only one data for now.), but with no text content. I can even click the cell but it does not display what I thought it would.  
Here is my code (The project got big and I cant give a runnable example. Sorry.):

ObservableIDPair, is a class I made for the purpose of having a wrapper class that contains a Name(String) and an ID(Integer), and has the correct data model so I can populate the cells.  
Its constructor gets a String and an int, wraps them in a SimpleStringProperty. (And SimpleIntegerProperty, of course.)
It has a getter and a setter for both "right" and "left".
they return the wrapped property.

So from what I suspect, I am correctly adding the columns and items, but the CellValueFactory is not valid, and the moment it tries to generate output, it doesn't.  
And I also have 2 things I did not understand from the guides and descriptions.

1. How should I declare the TableColumn?
it has two types <S,T>, and I don't understand what they mean. What I got from context in sample codes, is that S is the objects inside it, and T is what it displays.
Which is probably wrong.  
So how should I declare it? And,

2.What is cellData in that code?
From what I understood, I have to make either a lambda, or a callBack method that shows the factory how it has to extract the values.  
So what I think I am doing is:
CellData has an ObservableIDPair wrapped in. It gets it via getValue(), Then I call getLeft or getRight and retrieve the value.
but if I did that, it would be already displaying it right? So I must have gotten something wrong.

Sorry for the wall of text, and thank you for anyone reading this far to help me.


EDIT : Just used a debugger, and I noticed that cellValueFactory's boolean "Valid" is false. Does this mean I messed up at the declaration? Probably...
 
Sheriff
Posts: 6127
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I can tell from your code, you should probably have something like this:
This assumes there is a getter for the name property in ObservableIDPair called getName().

1. How should I declare the TableColumn?
it has two types <S,T>, and I don't understand what they mean. What I got from context in sample codes, is that S is the objects inside it, and T is what it displays.  


Well, I would put it this way: S is the type of an item in your data model, and T is the type of the property.  
 
Knute Snortum
Sheriff
Posts: 6127
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe cellData -> cellData.getValue().getLeft() will work if getLeft() returns a property, not a value.
 
F Lucas
Ranch Hand
Posts: 34
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. Thanks for the reply Knute. The explanation of the Tablecolumn type was very easy to understand.
But I couldn't understand what you meant on your second reply when you said "returns a property, not a value".  
Is SimpleStringProperty not a property? If not, how do I return a property?  
Edit : Forgot to mention about the PropertyValueFactory. I would like to not use it until it is ABSOLUTELY nessesary because I don't want to hard-code names as strings. I heard that its bad practice..
 
Knute Snortum
Sheriff
Posts: 6127
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, looks like I was wrong.  Try this instead:
EDIT: The cell factory wants an ObservableValue, not a Property.
 
Knute Snortum
Sheriff
Posts: 6127
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:This assumes there is a getter for the name property in ObservableIDPair called getName().  


Nope, also wrong. It assumes there is a field called "name" that is a property.
 
F Lucas
Ranch Hand
Posts: 34
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
I tried the asObject() solution.
Two problems....
1. SimpleStringProperty does not have asObject() defined.
2. SimpleIntegerProperty does. So I tried to do cellData -> cellData.getValue().getRight().asObject()
but it tells me that it cannot convert from SimpleIntegerProperty to ObservableValue.
This however is solved by making the actual method return left.asObject(), as it somehow allows me to do that if I call that the method will return an ObservableValue<Integer>.
But it still does not solve my problem.
I am kind of stuck.
 
F Lucas
Ranch Hand
Posts: 34
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I guess I have to use CellValueFactory. The lambda solution does not work and theres not a single answer in the internet that makes this work. Every single page tells me to use CellValueFactory.  
Why are some java apis so, lets say "against what they taught"?  
You are doing basic programming but the moment you want something complex you throw everything out of the window.  
Errors stop throwing exceptions.  
Now hardcoding is normal.
Lambda does not work.
Nothing can ever be singleton because of dependencies, and
You need so much understanding of deep workings of classes that encapsulation(Probably not this word. I wanted to say something that meant that you only need to know that it works, not how.)
How do I learn this? Tutorials were rich but suddently non-existent in certain parts.
 
Knute Snortum
Sheriff
Posts: 6127
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaFX documentation was -- and probably still is -- kind of spotty.  I've used books to fill in the gaps, but you're right, sometimes there are obscure things that are hard to find.

The two books I've used are a bit dated, but maybe they have newer additions : Learn JavaFX 8, and Pro JavaFX 9.
 
Knute Snortum
Sheriff
Posts: 6127
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well I guess I have to use CellValueFactory.


Do you mean a PropertyValueFactory?
 
F Lucas
Ranch Hand
Posts: 34
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just used the PropertyValueFactory. STILL DOES NOT DISPLAY ANYTHING.  
I'm gonna get insane. How can this be so difficult it feels like im the only one with this problem.
 
Knute Snortum
Sheriff
Posts: 6127
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds really frustrating.  Maybe post more of your code.  Maybe it doesn't have to do with the CellValueFactory at all.
 
F Lucas
Ranch Hand
Posts: 34
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a reason I believe it has to be JavaFX that is glitching.  
My code's Main method, takes 4 objects that implements my interface and initiates them.
Getter : Responsible for getting data
Setter : Responsible for setting data.
Browser : Responsible for the interface, and showing information.
Login : Serves as an authentication when you get a reference of this. Responsible for Login credential fetching.
First I tested the code with Text (Console) based browser, and Local getter and setter (takes and saves info in txt files).
It worked.
Then I made a MySQL version of getter and setter, and ran it again.
It fetched data correctly.  
Then I used the JavaFX implementation of the browser, which opens windows.
Now it does not work.
I suspected that it was not taking data correctly, but when I debug it with eclipse it shows all the correct data inside the columns.
What I mean is: It can't be that it hasn't gotten the data by some exception. I tested everything separately.
Sorry if I am wasting your time.
Edit : By the same reason, its very hard to give a working example of my code. If there is some part of my code you need, please tell me and I will paste it here.
 
Knute Snortum
Sheriff
Posts: 6127
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sorry if I am wasting your time.


I'm sorry that I'm not being more helpful.

Maybe post your ObservableIDPair class.  Maybe post more context around the showEmpTable() method.
 
F Lucas
Ranch Hand
Posts: 34
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm the one asking favors. You don't need to say sorry.  
Here is the code you asked for.  
ObservableIDPair :

I've tried many things for ObservableIDPair. Trying ObservableStringValue and ObservableIntegerValue did not work, neither did returning them as objects. (ObservableIntegerValue.asObject() from what I remember.)
And here is showEmpTable(). It is not REALLY nessesary, since I could just add this on start(), but I had to add it since I added the method in the interface (If you have any tips on how to make good interfaces, I greatly appreciate it.)
It has added comments for context and the workings behind it.

The only part I cannot test,have no knowledge, and is not confirmed to be working, is this. I can post all the rest of my code but the problem will most likely not be found there.
So its either ObservableIDPair, or the showEmsTable() method.
Thanks for your time.
 
Knute Snortum
Sheriff
Posts: 6127
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only thing I can see that might be a problem with ObservableIDPair is the setters.  I think they should look like this:
JavaFX relies heavily on reflection so it may be sensitive to these kinds of changes -- that is, that setters set the field, not the property.  I know this will affect other parts of your project, so maybe copying the project first or using a Git branch is a good idea so you can revert it if necessary.
 
F Lucas
Ranch Hand
Posts: 34
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still does not work....
I correctly set the setId to take an int instead of a Property, and made setName take a String instead of a Property.  
Still blank. Maybe this is a problem with my PC or javaFX installation and not the code. Literally everything is working fine except it does not display.
 
F Lucas
Ranch Hand
Posts: 34
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried something and the error message changed, and I thought it was better to share here in case it can help.  
I went to the variable emsTableView and changed the declaration to also initialize it.

Now it gives me an IllegalStateException on Main, as soon as it tries to make an instance of FXBasedBrowser.  
Did I discover a problem on my code or is initiating it just wrong?
EDIT : I got this idea because of another thread where a person was saying that he saw the table populate but did not display it.  
His problem was that he declared the tableView as static, and when he solved that it displayed correctly.
so I assumed everything else in his code was correct.  
I tried changing the declarations and BAM, an exception.
 
F Lucas
Ranch Hand
Posts: 34
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another update on some changes I tried.  
I tried initiating the tableView inside start().
That did not give me an exception.
However it did not change anything.
 
Knute Snortum
Sheriff
Posts: 6127
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

F Lucas wrote:I tried something and the error message changed, and I thought it was better to share here in case it can help.  
I went to the variable emsTableView and changed the declaration to also initialize it.

Now it gives me an IllegalStateException on Main, as soon as it tries to make an instance of FXBasedBrowser.  
Did I discover a problem on my code or is initiating it just wrong?


A statement annotated with @FXML should not initialize.  It should just be:

@FXML <class_name> <variable>;
 
F Lucas
Ranch Hand
Posts: 34
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright so Im really out of places I can find bugs. Every day I get back to this code its just me checking the same variables and code steps over and over again. I'm giving up.  
Thanks for trying to help.
 
Knute Snortum
Sheriff
Posts: 6127
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about writing a SSCCE (that's a link) that uses ObservableIDPair with some mock data and see if you can just get the Table working?  Then build from there.

If you post your FXML for the TableView, I can help.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!