无法阻止javafx表忽略我的setter函数验证

时间:2016-04-13 16:17:11

标签: validation javafx textfield

我正在使用javafx做一些表格。我想验证myTextRow类中的文本字段。在“setText2”方法中,如果输入不大于6个符号,则检查输入,但它根本没有效果。

import java.util.ArrayList;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TextArea;
import javafx.util.Callback;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Supermain extends Application {

    @Override
    public void start(Stage primaryStage) {

        ArrayList myindizes=new ArrayList();



        final TableView<myTextRow> table = new TableView<>();
        table.setEditable(true);
        table.setStyle("-fx-text-wrap: true;");

        //Table columns
        TableColumn<myTextRow, String> clmID = new TableColumn<>("ID");
        clmID.setMinWidth(160);
        clmID.setCellValueFactory(new PropertyValueFactory<>("ID"));

        TableColumn<myTextRow, String> clmtext = new TableColumn<>("Text");
        clmtext.setMinWidth(160);
        clmtext.setCellValueFactory(new PropertyValueFactory<>("text"));
        clmtext.setCellFactory(new TextFieldCellFactory());

        TableColumn<myTextRow, String> clmtext2 = new TableColumn<>("Text2");
        clmtext2.setMinWidth(160);
        clmtext2.setCellValueFactory(new PropertyValueFactory<>("text2"));
        clmtext2.setCellFactory(new TextFieldCellFactory());

        //Add data
        final ObservableList<myTextRow> data = FXCollections.observableArrayList(
                new myTextRow(5, "Lorem","bla"),
                new myTextRow(2, "Ipsum","bla")
        );

        table.getColumns().addAll(clmID, clmtext,clmtext2);
        table.setItems(data);

        HBox hBox = new HBox();
        hBox.setSpacing(5.0);
        hBox.setPadding(new Insets(5, 5, 5, 5));

        Button btn = new Button();
        btn.setText("Get Data");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                for (myTextRow data1 : data) {
                    System.out.println("data:" + data1.getText2());
                }
            }
        });

        hBox.getChildren().add(btn);

        BorderPane pane = new BorderPane();
        pane.setTop(hBox);
        pane.setCenter(table);
        primaryStage.setScene(new Scene(pane, 640, 480));
        primaryStage.show();


    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }


    public static class TextFieldCellFactory
            implements Callback<TableColumn<myTextRow, String>, TableCell<myTextRow, String>> {

        @Override
        public TableCell<myTextRow, String> call(TableColumn<myTextRow, String> param) {
            TextFieldCell textFieldCell = new TextFieldCell();
            return textFieldCell;
        }

        public static class TextFieldCell extends TableCell<myTextRow, String> {

            private TextArea textField;
            private StringProperty boundToCurrently = null;

            public TextFieldCell() {

                textField = new TextArea();
                textField.setWrapText(true);
                textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);

                this.setGraphic(textField);
            }

            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                if (!empty) {
                    // Show the Text Field
                    this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);


                   // myindizes.add(getIndex());

                    // Retrieve the actual String Property that should be bound to the TextField
                    // If the TextField is currently bound to a different StringProperty
                    // Unbind the old property and rebind to the new one
                    ObservableValue<String> ov = getTableColumn().getCellObservableValue(getIndex());
                    SimpleStringProperty sp = (SimpleStringProperty) ov;

                    if (this.boundToCurrently == null) {
                        this.boundToCurrently = sp;
                        this.textField.textProperty().bindBidirectional(sp);
                    } else if (this.boundToCurrently != sp) {
                        this.textField.textProperty().unbindBidirectional(this.boundToCurrently);
                        this.boundToCurrently = sp;
                        this.textField.textProperty().bindBidirectional(this.boundToCurrently);
                    }

                    double height = real_lines_height(textField.getText(), this.getWidth(), 30, 22);
                    textField.setPrefHeight(height);
                    textField.setMaxHeight(height);

                    textField.setMaxHeight(Double.MAX_VALUE);
                    // if height bigger than the biggest height in the row
                    //-> change all heights of the row(textfields ()typeof textarea) to this height
                    // else leave the height as it is


                    //System.out.println("item=" + item + " ObservableValue<String>=" + ov.getValue());
                    //this.textField.setText(item);  // No longer need this!!!
                } else {
                    this.setContentDisplay(ContentDisplay.TEXT_ONLY);
                }
            }

        }

    }

    public class myTextRow {

        private final SimpleIntegerProperty ID;

        private final SimpleStringProperty text;
        private final SimpleStringProperty text2;

        public myTextRow(int ID, String text,String text2) {

            this.ID = new SimpleIntegerProperty(ID);
            this.text = new SimpleStringProperty(text);
            this.text2 = new SimpleStringProperty(text2);


        }

        public void setID(int id) {
            this.ID.set(id);
        }

        public void setText(String text) {
            this.text.set(text);
        }

        public void setText2(String text) {
            if(text2check(text)){
            this.text2.set(text);}
            else
            {System.out.println("wrong value!!!");}
        }

        public int getID() {
            return ID.get();
        }

        public String getText() {
            return text.get();
        }

        public StringProperty textProperty() {
        return text;

        }

        public String getText2() {
            return text2.get();
        }

        public StringProperty text2Property() {
        return text2;

        }

    public IntegerProperty IDProperty() {
        return ID;
    }

    public boolean text2check(String t)
    {
       if(t.length()>6)return false;
       return true;
    }
    }

    private static double real_lines_height(String s, double width, double heightCorrector, double widthCorrector) {
        HBox h = new HBox();
        Label l = new Label("Text");
        h.getChildren().add(l);
        Scene sc = new Scene(h);
        l.applyCss();
        double line_height = l.prefHeight(-1);

        int new_lines = s.replaceAll("[^\r\n|\r|\n]", "").length();
        //  System.out.println("new lines= "+new_lines);
        String[] lines = s.split("\r\n|\r|\n");
        //  System.out.println("line count func= "+ lines.length);
        int count = 0;
        //double rest=0;
        for (int i = 0; i < lines.length; i++) {
            double text_width = get_text_width(lines[i]);
            double plus_lines = Math.ceil(text_width / (width - widthCorrector));
            if (plus_lines > 1) {
                count += plus_lines;
                //rest+= (text_width / (width-widthCorrector)) - plus_lines;
            } else {
                count += 1;
            }

        }
        //count+=(int) Math.ceil(rest);
        count += new_lines - lines.length;

        return count * line_height + heightCorrector;
    }

    private static double get_text_width(String s) {
        HBox h = new HBox();
        Label l = new Label(s);
        l.setWrapText(false);
        h.getChildren().add(l);
        Scene sc = new Scene(h);
        l.applyCss();
        // System.out.println("dubbyloop.FXMLDocumentController.get_text_width(): "+l.prefWidth(-1));
        return l.prefWidth(-1);

    }

}

1 个答案:

答案 0 :(得分:1)

JavaFX Properties模式的规则是对于属性x,调用xProperty().setValue(value)应始终与调用setX(value)相同。您的验证使这不正确。您的单元实现使用的绑定会在属性上调用setValue方法,这就是为什么它会绕过您的验证检查。

(旁注:在我要更改名称的所有代码中,以便它们遵守正确的naming conventions。)

在此模式中实现属性的默认方式是:

public class MyTextRow {

    private final StringProperty text = new SimpleStringProperty();

    public StringProperty textProperty() {
        return text ;
    }

    public final void setText(String text) {
        textProperty().set(text);
    }

    public final String getText() {
        return textProperty().get();
    }
}

通过将set / get方法委托给适当的属性方法,即使在子类中重写textProperty()方法,也可以保证强制执行这些规则。使set和get方法最终确保规则不会被覆盖这些方法的子类破坏。

一种方法可能是覆盖属性中的setsetValue方法,如下所示:

public class MyTextRow {

    private final StringProperty text2 = new StringPropertyBase() {
        @Override
        public String getName() {
            return "text2";
        }
        @Override
        public Object getBean() {
            return MyTextRow.this ;
        }
        @Override
        public void setValue(String value) {
            if (text2Check(value)) {
                super.setValue(value);
            }
        }
        @Override
        public void set(String value) {
            if (text2Check(value)) {
                super.set(value);
            }
        }
    }

    public StringProperty text2Property() {
        return text2 ;
    }

    public final void setText2(String text2) {
        text2Property().set(text2);
    }

    public final String getText2() {
        return text2Property().get();
    }

    // ...
}

但是,我认为这会破坏您对textTextArea属性的双向绑定(基本上,当更改被否决时,无法与文本区域进行通信,因此文本区域将不知道恢复到以前的值)。一个修复方法是使用属性上的侦听器而不是绑定来实现单元格。您可以在文本区域使用TextFormatter来简单地更新属性,如果更改没有发生,则否决文本更改。

以下是使用此方法的完整SSCCE:

import java.util.function.Function;
import java.util.function.UnaryOperator;

import javafx.application.Application;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.property.StringPropertyBase;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.TextFormatter.Change;
import javafx.stage.Stage;

public class VetoStringChange extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Item> table = new TableView<>();
        table.setEditable(true);
        table.getColumns().add(column("Item", Item::nameProperty));
        table.getColumns().add(column("Description", Item::descriptionProperty));

        for (int i = 1; i <= 20 ; i++) {
            table.getItems().add(new Item("Item "+i, ""));
        }

        primaryStage.setScene(new Scene(table, 600, 600));
        primaryStage.show();
    }

    public static <S> TableColumn<S,String> column(String title, Function<S,Property<String>> property) {
        TableColumn<S,String> col = new TableColumn<>(title);
        col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));

        col.setCellFactory(tc -> new TextAreaCell<S>(property));
        col.setPrefWidth(200);

        return col ;
    }

    public static class TextAreaCell<S> extends TableCell<S, String> {

        private TextArea textArea ;

        public TextAreaCell(Function<S, Property<String>> propertyAccessor) {

            textArea = new TextArea();
            textArea.setWrapText(true);
            textArea.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
            textArea.setMaxHeight(Double.MAX_VALUE);

            UnaryOperator<Change> filter = c -> {
                String proposedText = c.getControlNewText() ;
                Property<String> prop = propertyAccessor.apply(getTableView().getItems().get(getIndex()));
                prop.setValue(proposedText);
                if (prop.getValue().equals(proposedText)) {
                    return c ;
                } else {
                    return null ;
                }
            };
            textArea.setTextFormatter(new TextFormatter<String>(filter));
            this.setGraphic(textArea);
        }

        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if (!empty) {

                if (! textArea.getText().equals(item)) {
                    textArea.setText(item);
                }

                // Show the Text Field
                this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
            } else {
                this.setContentDisplay(ContentDisplay.TEXT_ONLY);
            }
        }

    }



    public static class Item {
        private final StringProperty name = new StringPropertyBase() {

            @Override
            public Object getBean() {
                return Item.this;
            }

            @Override
            public String getName() {
                return "name" ;
            }

            @Override
            public void set(String value) {
                if (checkValue(value)) {
                    super.set(value);
                }
            }

            @Override
            public void setValue(String value) {
                if (checkValue(value)) {
                    super.setValue(value);
                }
            }

        };
        private final StringProperty description = new SimpleStringProperty();

        public Item(String name, String description) {
            setName(name);
            setDescription(description);
        }

        private boolean checkValue(String value) {
            return value.length() <= 6 ;
        }

        public final StringProperty nameProperty() {
            return this.name;
        }

        public final String getName() {
            return this.nameProperty().get();
        }

        public final void setName(final String name) {
            this.nameProperty().set(name);
        }

        public final StringProperty descriptionProperty() {
            return this.description;
        }

        public final String getDescription() {
            return this.descriptionProperty().get();
        }

        public final void setDescription(final String description) {
            this.descriptionProperty().set(description);
        }

    }

    public static void main(String[] args) {
        launch(args);
    }
}

另一种方法是允许&#34;提交并恢复&#34;在您的财产上键入策略:

public class MyTextRow {

    private final StringProperty text2 = new SimpleStringProperty();

    public MyTextRow() {
        text2.addListener((obs, oldText, newText) -> {
            if (! checkText2(newText)) {
                // sanity check:
                if (checkText2(oldText)) {
                    text2.set(oldText);
                }
            }
        });
    }

    public StringProperty text2Property() {
        return text ;
    }

    public final void setText2(String text2) {
        text2Property().set(text2);
    }

    public final String getText2() {
        return text2Property().get();
    }
}

一般情况下,我不喜欢通过侦听无效值并像这样恢复来进行验证,因为属性的其他侦听器将看到所有更改,包括对无效值的更改和更改。但是,在这种情况下,这可能是最佳选择。

最后,您可以考虑否决第一个选项中的无效更改,并在单元格中的控件上设置TextFormatter,但不允许文本输入导致无效字符串。从可用性的角度来看,这并不总是可行的(例如,如果空字符串无效,您几乎总是希望允许用户暂时删除所有文本),这意味着在代码中保持两个验证检查同步,是一种痛苦。