Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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 强制TableView在后续添加时调整列的大小_Java_User Interface_Javafx 2_Javafx - Fatal编程技术网

Java 强制TableView在后续添加时调整列的大小

Java 强制TableView在后续添加时调整列的大小,java,user-interface,javafx-2,javafx,Java,User Interface,Javafx 2,Javafx,似乎当您第一次创建TableView并向其中添加项时,它会根据添加的数据计算列宽。如果在初始添加之后添加项,则不会展开列以适应新数据。下面是两个可运行的示例,演示: 此表扩展以适合长地址(因为它位于原始数据加载中) 导入javafx.application.application; 导入javafx.application.Platform; 导入javafx.beans.property.SimpleStringProperty; 导入javafx.collections.FXCollecti

似乎当您第一次创建TableView并向其中添加项时,它会根据添加的数据计算列宽。如果在初始添加之后添加项,则不会展开列以适应新数据。下面是两个可运行的示例,演示:

此表扩展以适合长地址(因为它位于原始数据加载中)

导入javafx.application.application;
导入javafx.application.Platform;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类TableViewSample2扩展了应用程序{
private TableView table=new TableView();
私有最终可观测列表数据=
FXCollections.observableArrayList(
新人(“雅各布”、“史密斯”、“雅各布”。smith@example.com"),
新人(“伊莎贝拉”、“约翰逊”、“伊莎贝拉”。johnson@example.com"),
新人(“伊桑”、“威廉姆斯”、“伊桑”。williams@example.com"),
新人(“艾玛”、“琼斯”、“艾玛”。jones@example.com"),
新人(“迈克尔”、“布朗”、“迈克尔”。brown@example.com"),
新人(“雅各布”、“史密斯”、“雅各布”。smith@example.com雅各布。smith@example.com雅各布。smith@example.com雅各布。smith@example.com雅各布。smith@example.com")
);
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公众假期开始(阶段){
场景=新场景(新组());
TableColumn firstNameCol=新的TableColumn(“名字”);
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
新财产价值工厂(“名字”);
TableColumn lastNameCol=新的TableColumn(“姓氏”);
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
新财产价值工厂(“姓氏”);
TableColumn emailCol=新的TableColumn(“电子邮件”);
设置最小宽度(200);
emailCol.setCellValueFactory(
新物业价值工厂(“电子邮件”);
表2.设置项目(数据);
table.getColumns().addAll(firstNameCol、lastNameCol、emailCol);
最终VBox VBox=新的VBox();
vbox.getChildren().addAll(表);
((组)scene.getRoot()).getChildren().addAll(vbox);
舞台场景;
stage.show();
}
公共静态类人员{
私有最终SimpleStringProperty名字;
私有最终SimpleStringProperty姓氏;
私人最终SimpleStringProperty电子邮件;
私人(字符串fName、字符串lName、字符串电子邮件){
this.firstName=新的SimpleStringProperty(fName);
this.lastName=新的SimpleStringProperty(lName);
this.email=新的SimpleStringProperty(电子邮件);
}
公共字符串getFirstName(){
返回firstName.get();
}
public void setFirstName(字符串fName){
firstName.set(fName);
}
公共字符串getLastName(){
返回lastName.get();
}
public void setLastName(字符串fName){
lastName.set(fName);
}
公共字符串getEmail(){
返回email.get();
}
public void setEmail(字符串fName){
email.set(fName);
}
}
} 
此版本不展开列,而是截断数据(因为数据是在后续数据加载时添加的)

导入javafx.application.application;
导入javafx.application.Platform;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类TableViewSample2扩展了应用程序{
private TableView table=new TableView();
私有最终可观测列表数据=
FXCollections.observableArrayList(
新人(“雅各布”、“史密斯”、“雅各布”。smith@example.com"),
新人(“伊莎贝拉”、“约翰逊”、“伊莎贝拉”。johnson@example.com"),
新人(“伊桑”、“威廉姆斯”、“伊桑”。williams@example.com"),
新人(“艾玛”、“琼斯”、“艾玛”。jones@example.com"),
新人(“迈克尔”、“布朗”、“迈克尔”。brown@example.com")
);
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公众假期开始(阶段){
场景=新场景(新组());
TableColumn firstNameCol=新的TableColumn(“名字”);
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
新财产价值工厂(“名字”);
TableColumn lastNameCol=新的TableColumn(“姓氏”);
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
新财产价值工厂(“姓氏”);
TableColumn emailCol=新的TableColumn(“电子邮件”);
设置最小宽度(200);
emailCol.setCellValueFactory(
新物业价值工厂(“电子邮件”);
表2.设置项目(数据);
table.getColumns().addAll(firstNameCol、lastNameCol、emailCol);
最终VBox VBox=新的VBox();
vbox.getChildren().addAll(表);
((组)scene.getRoot()).getChildren().addAll(vbox);
舞台场景;
stage.show();
table.getItems().add(新人物(“Jacob”、“Smith”、“Jacob”)。smith@example.com雅各布。smith@example.com雅各布。smith@example.com雅各布。smith@example.comJ
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TableViewSample2 extends Application {

    private TableView<Person> table = new TableView<Person>();
    private final ObservableList<Person> data =
        FXCollections.observableArrayList(
            new Person("Jacob", "Smith", "jacob.smith@example.com"),
            new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
            new Person("Ethan", "Williams", "ethan.williams@example.com"),
            new Person("Emma", "Jones", "emma.jones@example.com"),
            new Person("Michael", "Brown", "michael.brown@example.com"),
            new Person("Jacob", "Smith", "jacob.smith@example.com jacob.smith@example.com jacob.smith@example.com jacob.smith@example.com jacob.smith@example.com")
        );

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

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());

        TableColumn firstNameCol = new TableColumn("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("firstName"));

        TableColumn lastNameCol = new TableColumn("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("lastName"));

        TableColumn emailCol = new TableColumn("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("email"));

        table.setItems(data);
        table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);

        final VBox vbox = new VBox();
        vbox.getChildren().addAll(table);

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        stage.setScene(scene);
        stage.show();
    }

    public static class Person {

        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;
        private final SimpleStringProperty email;

        private Person(String fName, String lName, String email) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
            this.email = new SimpleStringProperty(email);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String fName) {
            firstName.set(fName);
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String fName) {
            lastName.set(fName);
        }

        public String getEmail() {
            return email.get();
        }

        public void setEmail(String fName) {
            email.set(fName);
        }
    }
} 
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TableViewSample2 extends Application {

    private TableView<Person> table = new TableView<Person>();
    private final ObservableList<Person> data =
        FXCollections.observableArrayList(
            new Person("Jacob", "Smith", "jacob.smith@example.com"),
            new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
            new Person("Ethan", "Williams", "ethan.williams@example.com"),
            new Person("Emma", "Jones", "emma.jones@example.com"),
            new Person("Michael", "Brown", "michael.brown@example.com")
        );

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

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());

        TableColumn firstNameCol = new TableColumn("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("firstName"));

        TableColumn lastNameCol = new TableColumn("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("lastName"));

        TableColumn emailCol = new TableColumn("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("email"));

        table.setItems(data);
        table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);

        final VBox vbox = new VBox();
        vbox.getChildren().addAll(table);

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        stage.setScene(scene);
        stage.show();
        table.getItems().add(new Person("Jacob", "Smith", "jacob.smith@example.com jacob.smith@example.com jacob.smith@example.com jacob.smith@example.com jacob.smith@example.com"));
    }

    public static class Person {

        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;
        private final SimpleStringProperty email;

        private Person(String fName, String lName, String email) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
            this.email = new SimpleStringProperty(email);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String fName) {
            firstName.set(fName);
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String fName) {
            lastName.set(fName);
        }

        public String getEmail() {
            return email.get();
        }

        public void setEmail(String fName) {
            email.set(fName);
        }
    }
}