How to change the font size in ListView in JavaFX?


Question

I tried this suggestion http://forums.sun.com/thread.jspa?threadID=5421037 but style cannot be found.

Is there any other way?

Lots of thanks, cancelledout

1
3
6/25/2010 9:59:04 AM

Accepted Answer

It depends on the version of JavaFX, I will suppose it is about 1.3.
By default, ListView shows the result of toString() applied to the objects stored in its items variable.
But you can define a cellFactory function that will generate a ListCell that takes these objects and presents in a Node holding whatever you put in, for example a Text or Label, or something more complex. So, there, you can format the text the usual way.

Now, if you don't want to go to that level of detail, just use CSS:

.list-cell 
{
  -fx-font: 18pt "Arial";
}
8
7/9/2010 4:08:29 PM

Another solution, direct approach:

ListView listView=new ListView();
        if(!listView.getItems().isEmpty())
        {
            VirtualFlow ch=(VirtualFlow) listView.getChildrenUnmodifiable().get(0);
            Font anyfont=new Font("Tahoma",16);
            for (int i = 0; i < ch.getCellCount(); i++)
            {
                Cell cell= ch.getCell(i);
                cell.setFont(anyfont);
            }
        }

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon