JavaFX News, Demos and Insight // FX Experience

FX Experience Has Gone Read-Only

I've been maintaining FX Experience for a really long time now, and I love hearing from people who enjoy my weekly links roundup. One thing I've noticed recently is that maintaining two sites (FX Experience and JonathanGiles.net) takes more time than ideal, and splits the audience up. Therefore, FX Experience will become read-only for new blog posts, but weekly posts will continue to be published on JonathanGiles.net. If you follow @FXExperience on Twitter, I suggest you also follow @JonathanGiles. This is not the end - just a consolidation of my online presence to make my life a little easier!

tl;dr: Follow me on Twitter and check for the latest news on JonathanGiles.net.

Alternate row highlighting in empty TableView (and ListView) rows

by | Nov 27, 2011 | Controls, Tips n' Tricks, UI Design | 4 comments

One thing I see asked a lot (either directly via email, or in the rather excellent JavaFX Forums) is why does TableView (and ListView) look like this in JavaFX 2.0:

A TableView control using default styling and API as provided in JavaFX 2.0.

Or, more specifically, why it doesn’t look like this:

A TableView without alternate row highlighting in empty rows

I can see where this question comes from, given this is how Swing’s JTable looks:

Swing JTable

It comes down to taste I guess, but given that there is a subset of developers who expect this look, I thought I’d put a blog post up here to clarify how it is done (and to save me having to explain it to people individually by email!) πŸ™‚ All you need is the following CSS for TableView:

.table-row-cell:empty {
 -fx-background-color: white;
}
.table-row-cell:empty .table-cell {
 -fx-border-width: 0px;
}

Similarly, for ListView, you can get away with just the first statement above (slightly modified for ListView):

.list-cell:empty {
 -fx-background-color: white;
}

The end result is precisely what you see in the second image above (there ain’t no photoshop trickery here!) πŸ™‚ Hopefully this helps people customising their user interfaces, and hopefully also encourages you to further explore what is possible. The best starting point is the excellent JavaFX documentation website, the CSS Reference (which is about to be updated for 2.0.2 I believe), and as always, the caspian.css file that ships with every JavaFX release (embedded within jfxrt.jar).

4 Comments

  1. svuksic on November 30, 2011 at 9:05 am

    Thank you for advice, saved me a lot of time!

    Reply
  2. Bipin Bhandari on April 26, 2012 at 5:17 am

    Please could you tell me how to attach this css . i have made one as u posted but its not working.

    Reply
  3. Neal on May 22, 2012 at 10:02 am

    Thank you for this. Any idea on how to add a style to specific rows?

    Bipin, here’s how you add a css file to your scene:
    scene.getStylesheets().addAll(
    Main.class.getResource(“myStyles.css”).toExternalForm())

    Reply
  4. Yordanys on October 1, 2014 at 1:47 pm

    how i can get tablerow into drawTable.getSelectionModel().selectedIndexProperty().addListener(…);?
    i need to change the background color of the row when user press any key…

    Reply

Trackbacks/Pingbacks

  1. JavaFX links of the week, December 4 // JavaFX News, Demos and Insight // FX Experience - [...] did a post to show how to disable alternate row highlighting in TableView and ListView controls when the rows…

Submit a Comment Cancel reply

Your email address will not be published. Required fields are marked *

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /