Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 列格式化的可重用方法_Java_Javafx - Fatal编程技术网

Java 列格式化的可重用方法

Java 列格式化的可重用方法,java,javafx,Java,Javafx,我的javafx应用程序中有以下方法: columExample.setCellFactory((TableColumn<Person, Person> column) -> { return new TableCell<Peson, Person>() { @Override protected void updateItem(Person item, boolean empty) {

我的javafx应用程序中有以下方法:

columExample.setCellFactory((TableColumn<Person, Person> column) -> {
        return new TableCell<Peson, Person>() {
            @Override
            protected void updateItem(Person item, boolean empty) {
                super.updateItem(item, empty);
                if (item == null || empty) {
                    setText(null);
                } else {
                    setText(item.getPerson_name());
                }
            }
        };
    });
在我的初始化方法中

 columnX.setCellValueFactory(new PropertyValueFactory("name"));

  @FXML
TableColumn columnX;
在这一点上,我的所有列都充满了数据。现在,棘手的部分是,我的模型类中的所有原始数据都显示为ok,但是objecst显示为string方法。这就是为什么我使用我首先链接的方法

columExample.setCellFactory((TableColumn<Person, Person> column) -> {
    return new TableCell<Peson, Person>() {
        @Override
        protected void updateItem(Person item, boolean empty) {
            super.updateItem(item, empty);
            if (item == null || empty) {
                setText(null);
            } else {
                setText(item.getPerson_name());
            }
        }
    };
});
columExample.setCellFactory((TableColumn列)->{
返回新的TableCell(){
@凌驾
受保护的void updateItem(个人项目,布尔值为空){
super.updateItem(项,空);
如果(项==null | |空){
setText(空);
}否则{
setText(item.getPerson_name());
}
}
};
});
所以我只得到人名,而不是整个to string方法

有什么简单的方法可以做到这一点吗

TableColumn<Person, String> columnExample = new TableColumn<>("Name");
columnExample.setCellValueFactory(cellData -> 
new SimpleStringProperty(cellData.getValue().getPersonName()));
tableColumnExample=新的TableColumn(“名称”);
columnExample.setCellValueFactory(cellData->
新的SimpleStringProperty(cellData.getValue().getPersonName());
由于我无法在当前设置中使用它,因此我的程序在应用程序启动时显示错误。(我假设这会创建新的tablecolum,我希望将其应用于我当前的@FXML列)


干杯

在您引用的案例中,您应该真正使用单元格值工厂:

TableColumn<Person, String> columnExample = new TableColumn<>("Name");

columnExample.setCellValueFactory(cellData -> 
    new SimpleStringProperty(cellData.getValue().getPersonName()));
然后这就变成了

TableColumn<Person, String> columnExample = new TableColumn<>("Name");
columnExample.setCellValueFactory(cellData -> cellData.getValue().personNameProperty());
你可以把这个叫做

TableColumn<Person, String> columnExample = this.<Person, String>column("Name", Person::personNameProperty);
我经常发现,通过这种设置,除了将列传递到表之外,我不需要该列,因此代码通常看起来像

table.getColumns().add(column("Name", Person::personNameProperty));

如果您确实需要一种生成单元工厂的方法,您可以使用类似的技术。在您的示例中,不同之处包括:

  • 行和单元格中项目的类型
  • 从单元格中的项(即从传递给
    updateItem(…)
    方法的项)获取
    字符串的函数
  • 因此,第一个问题可以通过创建一个通用方法来解决,
    返回一个
    回调
    。第二个问题通过使用类型为
    Function
    的参数来解决,该参数将单元格项(类型
    T
    )映射到
    字符串

    public <S,T> Callback<TableColumn<S,T>, TableCell<S,T>> 
            cellFactory(Function<T, String> textExtractor) {
    
        return col -> new TableCell<S,T>() {
            @Override
            protected void updateItem(T item, boolean empty) {
                super.updateItem(item, empty) ;
                if (empty || item == null) {
                    setText(null);
                } else {
                    setText(textExtractor.apply(item));
                }
            }
        };
    }
    
    最后,如果需要,您可以将所有这些想法结合起来。假设您有一个
    地址
    类:

    public class Address {
    
        private String street ;
        private String city ; 
    
        // etc etc
    
        public String shortTextForm() {
            return street+" ,"+city;
        }
    
        // ...
    }
    
    public class Person {
    
        private final StringProperty name = new SimpleStringProperty();
    
        public StringProperty nameProperty() {
            return name ;
        }
    
        public final String getName() {
            return nameProperty().get();
        }
    
        public final void setName(String name) {
            nameProperty().set(name);
        }
    
        private final ObjectProperty<Address> address = new SimpleObjectProperty<>();
    
        public ObjectProperty<Address> addressProperty() {
             return address ;
        }
    
        public Address getAddress() {
            return addressProperty().get();
        }
    
        public void setAddress(Address address) {
            addressProperty().set(address);
        }
    
        // other properties...
    }
    
    类:

    public class Address {
    
        private String street ;
        private String city ; 
    
        // etc etc
    
        public String shortTextForm() {
            return street+" ,"+city;
        }
    
        // ...
    }
    
    public class Person {
    
        private final StringProperty name = new SimpleStringProperty();
    
        public StringProperty nameProperty() {
            return name ;
        }
    
        public final String getName() {
            return nameProperty().get();
        }
    
        public final void setName(String name) {
            nameProperty().set(name);
        }
    
        private final ObjectProperty<Address> address = new SimpleObjectProperty<>();
    
        public ObjectProperty<Address> addressProperty() {
             return address ;
        }
    
        public Address getAddress() {
            return addressProperty().get();
        }
    
        public void setAddress(Address address) {
            addressProperty().set(address);
        }
    
        // other properties...
    }
    
    然后你就这么做了

    TableView<Person> table = new TableView<>();
    table.getColumns().add(column("Name", Person::nameProperty, s -> s));
    table.getColumns().add(column("Address", Person::addressProperty, Address:shortTextForm));
    
    TableView table=newtableview();
    table.getColumns().add(column(“Name”,Person::nameProperty,s->s));
    table.getColumns().add(列(“地址”,Person::addressProperty,Address:shortTextForm));
    
    在您引用的案例中,您应该真正使用单元格值工厂:

    TableColumn<Person, String> columnExample = new TableColumn<>("Name");
    
    columnExample.setCellValueFactory(cellData -> 
        new SimpleStringProperty(cellData.getValue().getPersonName()));
    
    然后这就变成了

    TableColumn<Person, String> columnExample = new TableColumn<>("Name");
    columnExample.setCellValueFactory(cellData -> cellData.getValue().personNameProperty());
    
    你可以把这个叫做

    TableColumn<Person, String> columnExample = this.<Person, String>column("Name", Person::personNameProperty);
    
    我经常发现,通过这种设置,除了将列传递到表之外,我不需要该列,因此代码通常看起来像

    table.getColumns().add(column("Name", Person::personNameProperty));
    

    如果您确实需要一种生成单元工厂的方法,您可以使用类似的技术。在您的示例中,不同之处包括:

  • 行和单元格中项目的类型
  • 从单元格中的项(即从传递给
    updateItem(…)
    方法的项)获取
    字符串的函数
  • 因此,第一个问题可以通过创建一个通用方法来解决,
    返回一个
    回调
    。第二个问题通过使用类型为
    Function
    的参数来解决,该参数将单元格项(类型
    T
    )映射到
    字符串

    public <S,T> Callback<TableColumn<S,T>, TableCell<S,T>> 
            cellFactory(Function<T, String> textExtractor) {
    
        return col -> new TableCell<S,T>() {
            @Override
            protected void updateItem(T item, boolean empty) {
                super.updateItem(item, empty) ;
                if (empty || item == null) {
                    setText(null);
                } else {
                    setText(textExtractor.apply(item));
                }
            }
        };
    }
    
    最后,如果需要,您可以将所有这些想法结合起来。假设您有一个
    地址
    类:

    public class Address {
    
        private String street ;
        private String city ; 
    
        // etc etc
    
        public String shortTextForm() {
            return street+" ,"+city;
        }
    
        // ...
    }
    
    public class Person {
    
        private final StringProperty name = new SimpleStringProperty();
    
        public StringProperty nameProperty() {
            return name ;
        }
    
        public final String getName() {
            return nameProperty().get();
        }
    
        public final void setName(String name) {
            nameProperty().set(name);
        }
    
        private final ObjectProperty<Address> address = new SimpleObjectProperty<>();
    
        public ObjectProperty<Address> addressProperty() {
             return address ;
        }
    
        public Address getAddress() {
            return addressProperty().get();
        }
    
        public void setAddress(Address address) {
            addressProperty().set(address);
        }
    
        // other properties...
    }
    
    类:

    public class Address {
    
        private String street ;
        private String city ; 
    
        // etc etc
    
        public String shortTextForm() {
            return street+" ,"+city;
        }
    
        // ...
    }
    
    public class Person {
    
        private final StringProperty name = new SimpleStringProperty();
    
        public StringProperty nameProperty() {
            return name ;
        }
    
        public final String getName() {
            return nameProperty().get();
        }
    
        public final void setName(String name) {
            nameProperty().set(name);
        }
    
        private final ObjectProperty<Address> address = new SimpleObjectProperty<>();
    
        public ObjectProperty<Address> addressProperty() {
             return address ;
        }
    
        public Address getAddress() {
            return addressProperty().get();
        }
    
        public void setAddress(Address address) {
            addressProperty().set(address);
        }
    
        // other properties...
    }
    
    然后你就这么做了

    TableView<Person> table = new TableView<>();
    table.getColumns().add(column("Name", Person::nameProperty, s -> s));
    table.getColumns().add(column("Address", Person::addressProperty, Address:shortTextForm));
    
    TableView table=newtableview();
    table.getColumns().add(column(“Name”,Person::nameProperty,s->s));
    table.getColumns().add(列(“地址”,Person::addressProperty,Address:shortTextForm));
    
    我可能没有抓住这里的要点,但是如果您对person类进行了适当的封装,那么您最好使用PropertyValueFactory。如果person_name是person类中的属性,则

    columExample.setCellValueFactory(new PropertyValueFactory<Person, String>("person_name"));
    
    columExample.setCellValueFactory(新的PropertyValueFactory(“人名”);
    

    我们应该做到这一点

    我可能忽略了这一点,但是如果您对person类进行了适当的封装,那么您最好使用PropertyValueFactory。如果person_name是person类中的属性,则

    columExample.setCellValueFactory(new PropertyValueFactory<Person, String>("person_name"));
    
    columExample.setCellValueFactory(新的PropertyValueFactory(“人名”);
    

    我们应该做到这一点

    在这种情况下,为什么要使用单元格工厂而不是单元格值工厂?它显示了什么错误?好的,你的回答帮助了我很多,我遇到的错误是hibernate的一些问题,在我删除表后得到了解决。非常感谢!在这种情况下,为什么要使用单元格工厂而不是单元格值工厂?它显示了什么错误?好的,你的回答帮助了我很多,我遇到的错误是hibernate的一些问题,在我删除表后得到了解决。非常感谢!我强烈建议使用lambda表达式代替(遗留)
    PropertyValueFactory
    setCellValueFactory(cellData->cellData.getValue().person\u nameProperty())
    的代码量大致相同,但运行速度更快(没有反射),而且(最重要的是)编译时可检查。编译器将查找该方法,因此如果方法名称中有输入错误,编译器将捕获该方法。使用
    PropertyValueFactory
    您将得到需要跟踪的运行时错误,这可能会更加困难。我强烈建议使用lambda表达式代替(遗留的)
    PropertyValueFactory
    setCellValueFactory(cellData->cellData.getValue().person\u nameProperty())
    的代码量大致相同,但运行速度更快(没有反射),而且(最重要的是)编译时可检查。编译器将查找该方法,因此如果方法名称中有输入错误,编译器将捕获该方法。