【问题标题】:how can i do replacement of one word with another in the tableview cell texfield in javafx?如何在 javafx 的 tableview 单元格文本字段中用另一个词替换一个词?
【发布时间】:2018-05-31 05:11:24
【问题描述】:

我在 tableview 中搜索一个单词,有一个名为 find 的文本字段可以在 tableview 中找到任何文本,然后我想用 javafx tableview 中的其他文本替换该文本?我已经运行了查找单词的代码,但现在我想要替换代码,有人可以帮我解决这个问题吗?

public void onClickSearch(ActionEvent event) {
    String st=srep.getText();
    //sbtn.setOnAction((ActionEvent event1) -> {
    tc_source.setCellFactory((column) -> {

        return new TableCell<File, String>() {
            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                if (item == null || empty) {
                    setGraphic(null);
                    setText(null);
                    setStyle("");
                }
                else {

                    if (!srep.getText().isEmpty() && item.toLowerCase().contains(srep.getText().toLowerCase())) {
                        Double rowHeight = this.getTableRow().getHeight();
                        setGraphic(buildTextFlow(item ,srep.getText()));
                        setHeight(rowHeight);
                        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                    }
                    else {
                        setText(item);
                        setTextFill(javafx.scene.paint.Color.BLACK);
                        setStyle("");
                        setContentDisplay(ContentDisplay.TEXT_ONLY);
                    }


                }

            }

            public 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(createText(text.substring(matchIndex, index1), color));
                }
                if (index1 != text.length()) {
                    t.getChildren().add(createNormText(text.substring(index1)));
                }
                return t;
            }
        };
    });

}

【问题讨论】:

    标签: java javafx tableview


    【解决方案1】:

    替换cellFactory 不会自动导致将列的单元格替换为新工厂的单元格。 afaik 没有办法强制TableView 重新创建它的单元格。初始 cellFactory 返回的单元格应根据搜索文本动态变化:

    private final StringProperty searchText = new SimpleStringProperty();
    

    TableView 创建/初始化

    tc_source.setCellFactory(column ->  new TableCell<File, String>() {
        private final ChangeListener<String> listener = (o, oldValue, newValue) -> {
            updateContents(getItem(), newValue);
        };
    
        @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();
                setGraphic(buildTextFlow(item, search));
                setHeight(rowHeight);
                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; // why are we using the awt class here?
            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(createText(text.substring(matchIndex, index1), color));
            }
            if (index1 != text.length()) {
                t.getChildren().add(createNormText(text.substring(index1)));
            }
            return t;
        }
    });
    
    public void onClickSearch(ActionEvent event) {
        searchText.set(srep.getText());
    }
    

    注意:您应该考虑重用TextFlow,因为每次搜索或滚动足够远时,TextFlow 及其所有子项都会为当前代码中的每个单元格重新创建。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      • 2020-09-01
      相关资源
      最近更新 更多