0

I'm trying to understand better how the TableView in JavaFX works. In order to reach several goals I will try to explain what I did so far, why I did it, and what are my problems.

Of course, I have a TableView, which is populated thanks to an ObservableList.

Platform.runLater(()->ObservableList.add(0, myObject));

The Table is composed by 2 columns defined in my FXML file, in which I also have:

<cellValueFactory><PropertyValueFactory property="column1" /></cellValueFactory>
<cellValueFactory><PropertyValueFactory property="column2" /></cellValueFactory>

MyObject class, naturally, has these 2 properties and the columns are populated automatically. The first problem I had, was about the strings in the 2nd column (they were too long). That's why I added in my Controller class:

@Override
public void initialize(URL location, ResourceBundle resources) {
    setCellFactory();
}

private void setCellFactory() {
    secondColumn.setCellFactory(new Callback<TableColumn, TableCell>() {

        @Override
        public TableCell call(TableColumn param) {
            TableCell cell = new TableCell<>();
            Text text = new Text();
            cell.setGraphic(text);
            cell.setPrefHeight(Control.USE_COMPUTED_SIZE);
            text.wrappingWidthProperty().bind(secondColumn.widthProperty());
            text.textProperty().bind(cell.itemProperty());
            text.setFill(Color.WHITE);
            return cell;
        }
    });
}

In that way I solved the problem about the wrapped text but, now, I would like to reach another goal and I really cannot find anything of help on the web.


  1. For each new entry on the top of the Table, I would like to compare the strings in order to find the differences between them (I already found a library to do that), but I don't know how to find a good way to get the current value in the second column.

  2. I have to highlight the differences in the old strings in the table. So, I guess I have to update all old cell. How?

  3. Highlight. How to do that? I read it's only possible if I insert several String in the same HBox or something like that. Is there a way to do that in a table?

11
  • Could you explain the "on the top of the Table" part in "For each new entry on the top of the Table"? Should this happen for every row or just for the topmost row displayed.
    – fabian
    Jul 21, 2016 at 10:33
  • For every row. I have another field in which I see the last message
    – db92
    Jul 21, 2016 at 11:25
  • So basically you manually add a new entry with a string and then want to highlight all the differences between the new strings and each of the existing ones? Jul 21, 2016 at 16:58
  • Also check out TextFlow for #3 of your questions... You should be able to use that in a custom TableCell. Jul 21, 2016 at 17:00
  • @Maverick283 how precisely? I already tried the TextFlow but it doesn't provide methods as wrappingWidthProperty() and textProperty(). That's why I don't know how to create the binding as I did for the Text.
    – db92
    Jul 22, 2016 at 5:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.