在javafx tableview中,我们如何从tableview中撤消突出显示以及如何使突出显示的单元格可编辑?

时间:2018-06-08 09:06:17

标签: javafx-2

在onClickRep上,这段代码正在搜索一个单词并突出显示它,然后可以用另一个单词替换该单词,那么究竟我们想要撤消突出显示的内容并使单元格可以在其中突出显示单词?所以这段代码在执行此代码后现在正在做什么,它保留了高亮的单词,因此在项目中执行其他功能时会出现问题,因此我想在完成后撤消此功能,以便我可以执行进一步的操作项目

public void OnClickRep(ActionEvent event) {
            searchText.set(srep.getText());
            String replace;
            replace = rrep.getText();
            tc_source.setCellFactory(column -> new TableCell<File, String>() {
                private final ChangeListener<String> listener = (o, oldValue, replace) -> {
                    updateContents(getItem(), replace);
                };

                @Override
                protected void updateItem(String item, boolean empty) {

                    super.updateItem(item, empty);
                    searchText.removeListener(listener);

                    if (item == null || empty) {
                        setGraphic(null);
                        setText(null);
                    } else {
                        updateContents(item, searchText.get());
                        searchText.addListener(listener);
                    }

                }

                private void updateContents(String item, String search) {
                    if (search == null || search.isEmpty() || !item.toLowerCase().contains(search.toLowerCase())) {
                        setText(item);
                        setTextFill(javafx.scene.paint.Color.BLACK);
                        setContentDisplay(ContentDisplay.TEXT_ONLY);
                    } else {
                        double rowHeight = this.getTableRow().getHeight();
                        tv_new.setFixedCellSize(50);
                        tv_new.prefHeightProperty().bind(tv_new.fixedCellSizeProperty().multiply(Bindings.size(tv_new.getItems()).add(1.01)));
                        tv_new.minHeightProperty().bind(tv_new.prefHeightProperty());
                        tv_new.maxHeightProperty().bind(tv_new.prefHeightProperty());
                        setGraphic(buildTextFlow(item, search));
                        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                    }
                }

                private TextFlow buildTextFlow(String text, String filter) {
                    int filterLength = filter.length();
                    if (filterLength == 0) {
                        return new TextFlow(createNormalText(text));
                    }
                    Font font = Font.font("Italic", 12);

                    java.awt.Color color = java.awt.Color.BLUE;

                    TextFlow t = new TextFlow();
                    String lowerText = text.toLowerCase();
                    filter = filter.toLowerCase();
                    int index1 = 0;
                    int matchIndex = 0;
                    while ((matchIndex = lowerText.indexOf(filter, index1)) != -1) {
                        if (index1 != matchIndex) {
                            t.getChildren().add(createNormText(text.substring(index1, matchIndex)));
                        }
                        index1 = matchIndex + filterLength;
                        t.getChildren().add(createReplace(text.substring(matchIndex, index1), replace));

                    }
                    if (index1 != text.length()) {
                        t.getChildren().add(createNormText(text.substring(index1)));
                    }
                    return t;
                }
            });

        }

        public static Text createReplace(String text, String replace) {
            Text result = new Text(text);
            result.setText(replace);
            System.out.println(result);
            return result;
        }
public static Text createText(String text, java.awt.Color color) {
        Text result = new Text(text);
        result.setFill(javafx.scene.paint.Color.BLUE);
        System.out.println(result);
        return result;
    }

    public static Text createNormText(String text) {
        Text result = new Text(text);

        return result;
    }

0 个答案:

没有答案