JavaFX table: shortcut does not behave as expected

I am using the following code to highlight text in Table

c HBoxes

that contains multiple Labels

. But some Labels

display ellipses instead of text. How can I get it to Labels

display in full width?

    col3.setCellValueFactory(new PropertyValueFactory<RegexMatch, String>("excerptLineTable"));
    col3.setCellFactory(new Callback<TableColumn, TableCell>() {
        @Override
        public TableCell call(TableColumn param) {
            TableCell cell = new TableCell() {
                @Override
                protected void updateItem(Object text, boolean empty) {
                    if (text != null && text instanceof String) {
                        HBox hbox = new HBox();
                        hbox.setAlignment(Pos.CENTER_LEFT);
                        String str = (String) text;
                        if (txtSearchField.getText().length() > 3 && str.contains(HIGHLIGHT_START)) {
                            // Something to highlight
                            hbox.getChildren().clear();
                            while (str.contains(HIGHLIGHT_START)) {
                                // First part
                                Label label = new Label(str.substring(0, str.indexOf(HIGHLIGHT_START)));
                                label.setWrapText(false);
                                label.setMaxWidth(Double.MAX_VALUE);
                                hbox.getChildren().add(label);
                                str = str.substring(str.indexOf(HIGHLIGHT_START) + HIGHLIGHT_START.length(), str.length());
                                // Part to highlight
                                Label label2 = new Label(str.substring(0, str.indexOf(HIGHLIGHT_END)));
                                label2.setWrapText(false);
                                label2.setMaxWidth(Double.MAX_VALUE);
                                label2.setStyle("-fx-background-color: "+ HIGHLIGHT_COLOR+";");
                                hbox.getChildren().add(label2);
                                // Last part
                                str = str.substring(str.indexOf(HIGHLIGHT_END) + HIGHLIGHT_END.length(), str.length());
                                if (!str.contains(HIGHLIGHT_START)) {
                                    Label label3 = new Label(str);
                                    label3.setWrapText(false);
                                    label3.setMaxWidth(Double.MAX_VALUE);
                                    hbox.getChildren().add(label3);
                                }
                            }
                        } else if (txtSearchField.getText().length() < 1) {
                            // Remove former highlightings and show simple text
                            str = str.replaceAll(HIGHLIGHT_START, "");
                            str = str.replaceAll(HIGHLIGHT_END, "");
                            hbox.getChildren().clear();
                            Label label = new Label(str);
                            label.setWrapText(false);
                            label.setMaxWidth(Double.MAX_VALUE);
                            hbox.getChildren().add(label);
                        } else {
                            // show simple text
                            hbox.getChildren().clear();
                            Label label = new Label(str);
                            label.setWrapText(false);
                            label.setMaxWidth(Double.MAX_VALUE);
                            hbox.getChildren().add(label);
                        }
                        setGraphic(hbox);
                    }
                }
            };
            return cell;
        }
    });

      

enter image description here

Solution
Using Labels

in HBox

with different background colors for each TableView

works fine. My problem was a bug in my method of finding and replacing selection.

0


source to share


1 answer


I found a rather ugly solution:

private static Label createNoEllipsisLabel(String pText)
{
    Label label = new Label(pText);
    double width = computeTextWidth(label.getFont(), pText, 0);
    label.setPrefWidth(width);
    label.setMinWidth(width);
    return label;
}

private static final TextLayout layout = Toolkit.getToolkit().getTextLayoutFactory().createLayout();

private static double computeTextWidth(Font font, String text, double wrappingWidth) {
    layout.setContent(text != null ? text : "", font.impl_getNativeFont());
    layout.setWrapWidth((float)wrappingWidth);
    return layout.getBounds().getWidth();
}

      



As copied from com.sun.javafx.scene.control.skin.Utils.computeTextWidth(Font, String, double)

, it used a lot of internal APIs, which is a pretty good idea.

Can anyone else find a better solution?

+1


source







All Articles