I am making a table with JavaFX. Every row has text. One row has a graphic because the text of that cell has multiple colors.
The code only applies when a certain condition is true (that part works):
departTimeCol.setCellFactory(column -> new TableCell<Ride, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(item);
if(item != null && ! empty){
if(item.matches("^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]\\s\\+[\\d]")) {
Text timeText = new Text(item.split(" ")[0].trim() + " ");
Text delayText = new Text(item.split(" ")[1].trim());
delayText.setFill(Color.RED);
TextFlow flow = new TextFlow(timeText, delayText);
setText(null);
setGraphic(flow);
}
}
}
});
The result is:
The row with the red +2 is the graphic. All the other rows contain text. How can I give the row - containing a graphic - the same height?
Simply set the prefered height to
0
to make the height just what is needed to store the text.Note that there are a few more things in the code that should be fixed:
null
, even if theString
no longer matches the regex or if the cell becomes empty. This means you can get theTableCell
into a state where thetext
property is not empty and thegraphic
contains aTextFlow
. Note: Always make sure the state of the look of a cell is correct no matter how often theupdateItem
method is called and independent of the arguments passed.graphic
+ aTextFlow
for one case and thetext
property for another. (Just take a look at the leftmost part of the text in your screenshot! Those are not properly aligned).Cell
s is reusing the node to prevent unnecessary creation of nodes. You kind of ruin this attempt by recreating theTextFlow
, ect. in theupdateItem
method instead of reusing those nodes.The regex needs not start with
^
sincematches
already makes sure the whole input is matched. Furthermore the delimiter used forsplit
does not have an exact equivalent in the regex. There are other space chars than, such as tab. Just check what the following code does...
You can also parse the input and match it in the same step by using
Pattern
+Matcher
and capturing groups. This way you also do not have the issue mentioned above.