JavaFX's TableView
has a placeholder property that is basically a Node
that gets displayed in the TableView
whenever it is empty. If this property is set to null (its default value), it appears as a Label
or some other text based Node
that says "There is no content in the table."
But if there are any rows of data in the table, then the placeholder Node
disappears and the entire vertical space in the TableView
gets filled with rows, including empty rows if there isn't enough data to fill the whole table.
These empty rows are what I want, even when the table is empty. In other words, I don't want to use the placeholder at all. Does anyone know how I can do this?
I'd rather not do something kludgey like put a empty-looking row in the TableView
whenever it's supposed to be actually empty.
I found a solution for javafx8. It makes use of the non-public api, but it uses no reflection (luckly). Basically you need to set (or replace) the skin of the TableView and return a non-zero value in the method
getItemCount()
. Like so:This method can also be used to add an extra row at the bottom of your last item (for if you want to include an add button for example). Basically return always one higher than the actual item-count.
Eventhough this is an old question, hopefully this was helpfull to someone.
Here is a tricky way to perform your task,
Unfortunately, the old issue is still not fixed in fx9 nor fx10. So revisited the hacks in the context of fx9. There had been changes, good and bad ones:
While digging, I noticed ever so slight glitches with the hacks (note: I did not run them against fx8, so these might be due differences in fx8 vs fx9!)
So I decided to go with the visibility enforcement: the reason for the slight glitches is that layoutChildren doesn't layout the flow if it thinks the placeholder is visible. That's handled by including the flow in the layout if super didn't.
The custom skin:
Usage example:
If any one is still looking for an alternate solution apart from what others had provided, below is the one which I worked with. As far as to me, this is the most simpliest approach I can go with (no custom skins, no API tweaking & no heavy controls like ListView).
Set a StackPane with a customized CSS that resembles alternate row coloring.
Below is the quick reference for implementation. The left table is with data and the right table is without data showing customized placeholder.
If your are specific with showing the column lines as well, you can follow the @Shreyas Dave approach of building a HBox of the StackPane(s) with border implementation.
And the CSS implementation is as below:
I have a requirement of having contrast border color to row background. I can easily acheive that with the above approach for having border color to my placeholder columns.
I think I found a solution. It is definitely not nice, since it is accessing the API in a not wanted way, and I'm probably also making undesired use of the visibleProperty, but here you go:
You can try to hack the TableViewSkin. Basically do this to retrieve a hacked Skin:
For the TableViewSkin you then need to override following method:
And for the skin using reflection stop showing the placeholder:
Maybe you can change the visibility of the flow in the same method to make the code shorter... But I think you get the concept