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
0to 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 theStringno longer matches the regex or if the cell becomes empty. This means you can get theTableCellinto a state where thetextproperty is not empty and thegraphiccontains aTextFlow. Note: Always make sure the state of the look of a cell is correct no matter how often theupdateItemmethod is called and independent of the arguments passed.graphic+ aTextFlowfor one case and thetextproperty for another. (Just take a look at the leftmost part of the text in your screenshot! Those are not properly aligned).Cells is reusing the node to prevent unnecessary creation of nodes. You kind of ruin this attempt by recreating theTextFlow, ect. in theupdateItemmethod instead of reusing those nodes.The regex needs not start with
^sincematchesalready makes sure the whole input is matched. Furthermore the delimiter used forsplitdoes 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+Matcherand capturing groups. This way you also do not have the issue mentioned above.