Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
JavaFX:TableView打印所选行项目_Javafx_Tableview - Fatal编程技术网

JavaFX:TableView打印所选行项目

JavaFX:TableView打印所选行项目,javafx,tableview,Javafx,Tableview,我现在有一个tableview,它显示了一组存储在数据库中的关于播放器的信息。我试图做的是,在选择了名称的行时,将名称的第一个名称打印到控制台。什么也没印出来。我将把整个代码从表视图发布到我要打印的地方。我省略了将播放器加载到tableview的函数,因为它是不相关的 //Start Right Menu TableView<TableDisplay> table = new TableView<>(); final ObservableList<T

我现在有一个tableview,它显示了一组存储在数据库中的关于播放器的信息。我试图做的是,在选择了名称的行时,将名称的第一个名称打印到控制台。什么也没印出来。我将把整个代码从表视图发布到我要打印的地方。我省略了将播放器加载到tableview的函数,因为它是不相关的

//Start Right Menu
    TableView<TableDisplay> table = new TableView<>();
    final ObservableList<TableDisplay> data = 
FXCollections.observableArrayList();

    TableColumn column1 = new TableColumn("Id");
    column1.setMinWidth(100);
    column1.setCellValueFactory(new PropertyValueFactory<>("id"));

    TableColumn column2 = new TableColumn("First Name");
    column2.setMinWidth(100);
    column2.setCellValueFactory(new PropertyValueFactory<>("firstName"));

    TableColumn column3 = new TableColumn("Last Name");
    column3.setMinWidth(100);
    column3.setCellValueFactory(new PropertyValueFactory<>("lastName"));

    TableColumn column4 = new TableColumn("Birthdate");
    column4.setMinWidth(100);
    column4.setCellValueFactory(new PropertyValueFactory<>("birthdate"));

    TableColumn column5 = new TableColumn("Nationality");
    column5.setMinWidth(100);
    column5.setCellValueFactory(new PropertyValueFactory<>("nationality"));

    TableColumn column6 = new TableColumn("Height");
    column6.setMinWidth(100);
    column6.setCellValueFactory(new PropertyValueFactory<>("height"));

    TableColumn column7 = new TableColumn("Position");
    column7.setMinWidth(100);
    column7.setCellValueFactory(new PropertyValueFactory<>("Position"));

    TableColumn column8 = new TableColumn("Foot");
    column8.setMinWidth(100);
    column8.setCellValueFactory(new PropertyValueFactory<>("foot"));

    TableColumn column9 = new TableColumn("Team Id");
    column9.setMinWidth(100);
    column9.setCellValueFactory(new PropertyValueFactory<>("teamId"));

    table.getColumns().addAll(column1, column2, column3, column4, column5, column6, column7, column8, column9);

    rightEditMenu.getChildren().addAll(table);
    //End Right Menu

    //Start Left Menu 2

由于我在代码中没有看到任何类型的侦听器,所以我假设您没有

似乎您正在尝试在加载
场景
之前打印选择的值,这意味着用户尚未进行任何选择

因此,在设置
表视图时添加以下代码:

// Add a listener to print the selected item to console when selected
table.getSelectionModel().selectedItemProperty().addListener((observableValue, oldValue, newValue) -> {
    if (newValue != null) {
        System.out.println("Selected Person: "
            + newValue.getId() + " | "
            + newValue.getFirstName() + " " + newValue.getLastName()
        );
   }
});
现在,无论何时选择一行,都会触发此
侦听器
,并打印
newValue
中的值,该值表示存储在所选行中的对象


由于您没有提供自己的MCVE,请参见下面的示例。您也不会声明
TableView
TableColumn
对象要显示的类的类型;这是一个糟糕的设计,应该按照下面的示例进行更新

import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
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 Main extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        // Simple UI
        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(10));

        // Setup the TableView and columns
        TableView<Person> table = new TableView<>();

        TableColumn<Person, Integer> colId = new TableColumn<>("ID");
        colId.setMinWidth(100);
        colId.setCellValueFactory(new PropertyValueFactory<>("id"));

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

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

        // Add the columns to the TableView
        table.getColumns().addAll(colId, colFirstName, colLastName);

        // Add a listener to print the selected item to console when selected
        table.getSelectionModel().selectedItemProperty().addListener((observableValue, oldValue, newValue) -> {
            if (newValue != null) {
                System.out.println("Selected Person: "
                        + newValue.getId() + " | "
                        + newValue.getFirstName() + " " + newValue.getLastName()
                );
            }
        });

        // Sample Data
        ObservableList<Person> people = FXCollections.observableArrayList();
        people.addAll(
                new Person(1, "John", "Smith"),
                new Person(2, "William", "Scott"),
                new Person(4, "Susan", "Ryder")
        );

        table.setItems(people);
        root.getChildren().add(table);

        // Show the stage
        primaryStage.setScene(new Scene(root));
        primaryStage.setWidth(300);
        primaryStage.setHeight(300);
        primaryStage.show();
    }

    public static class Person {

        private final IntegerProperty id = new SimpleIntegerProperty();
        private final StringProperty firstName = new SimpleStringProperty();
        private final StringProperty lastName = new SimpleStringProperty();

        Person(int id, String firstName, String lastName) {

            this.id.set(id);
            this.firstName.set(firstName);
            this.lastName.set(lastName);
        }

        public int getId() {
            return id.get();
        }

        public IntegerProperty idProperty() {
            return id;
        }

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

        public StringProperty firstNameProperty() {
            return firstName;
        }

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

        public StringProperty lastNameProperty() {
            return lastName;
        }
    }
}
导入javafx.application.application;
导入javafx.beans.property.IntegerProperty;
导入javafx.beans.property.SimpleIntegerProperty;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.property.StringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.geometry.Insets;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类主扩展应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共无效开始(阶段primaryStage){
//简单用户界面
VBox根=新的VBox(10);
根部设置对齐(位置中心);
根。设置填充(新插图(10));
//设置TableView和列
TableView table=新TableView();
TableColumn colId=新的TableColumn(“ID”);
colId.setMinWidth(100);
colId.setCellValueFactory(新属性ValueFactory(“id”);
TableColumn colFirstName=新的TableColumn(“名字”);
colFirstName.setMinWidth(100);
colFirstName.setCellValueFactory(新属性ValueFactory(“firstName”);
TableColumn colLastName=新的TableColumn(“姓氏”);
colLastName.setMinWidth(100);
colLastName.setCellValueFactory(新属性ValueFactory(“lastName”);
//将列添加到TableView
table.getColumns().addAll(colId、colFirstName、colLastName);
//添加一个侦听器,以便在选中时将所选项目打印到控制台
table.getSelectionModel().SelectEditeProperty().addListener((ObservalEvalue、oldValue、newValue)->{
if(newValue!=null){
System.out.println(“所选人员:”
+newValue.getId()+“|”
+newValue.getFirstName()+“”+newValue.getLastName()
);
}
});
//样本数据
ObservableList people=FXCollections.observableArrayList();
人民网(
新人(1,“约翰”、“史密斯”),
新人(2,“威廉”,“斯科特”),
新人(4,“苏珊”、“莱德”)
);
表1.设置项目(人);
root.getChildren().add(表);
//上台
primaryStage.setScene(新场景(根));
初级阶段。设置宽度(300);
初生阶段:坐位高度(300);
primaryStage.show();
}
公共静态类人员{
private final IntegerProperty id=新的SimpleIntegerProperty();
private final StringProperty firstName=新SimpleStringProperty();
private final StringProperty lastName=新的SimpleStringProperty();
Person(int-id、String-firstName、String-lastName){
此.id.set(id);
this.firstName.set(firstName);
this.lastName.set(lastName);
}
公共int getId(){
返回id.get();
}
公共整数属性idProperty(){
返回id;
}
公共字符串getFirstName(){
返回firstName.get();
}
public StringProperty firstNameProperty(){
返回名字;
}
公共字符串getLastName(){
返回lastName.get();
}
公共StringProperty lastNameProperty(){
返回姓氏;
}
}
}

您将代码放在第二个代码段的何处?您似乎正在使用初始(空)选择。它的右下方意味着它在GUI显示之前运行…您是否在
表视图上设置了侦听器来侦听正在更改的选择?
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
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 Main extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        // Simple UI
        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(10));

        // Setup the TableView and columns
        TableView<Person> table = new TableView<>();

        TableColumn<Person, Integer> colId = new TableColumn<>("ID");
        colId.setMinWidth(100);
        colId.setCellValueFactory(new PropertyValueFactory<>("id"));

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

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

        // Add the columns to the TableView
        table.getColumns().addAll(colId, colFirstName, colLastName);

        // Add a listener to print the selected item to console when selected
        table.getSelectionModel().selectedItemProperty().addListener((observableValue, oldValue, newValue) -> {
            if (newValue != null) {
                System.out.println("Selected Person: "
                        + newValue.getId() + " | "
                        + newValue.getFirstName() + " " + newValue.getLastName()
                );
            }
        });

        // Sample Data
        ObservableList<Person> people = FXCollections.observableArrayList();
        people.addAll(
                new Person(1, "John", "Smith"),
                new Person(2, "William", "Scott"),
                new Person(4, "Susan", "Ryder")
        );

        table.setItems(people);
        root.getChildren().add(table);

        // Show the stage
        primaryStage.setScene(new Scene(root));
        primaryStage.setWidth(300);
        primaryStage.setHeight(300);
        primaryStage.show();
    }

    public static class Person {

        private final IntegerProperty id = new SimpleIntegerProperty();
        private final StringProperty firstName = new SimpleStringProperty();
        private final StringProperty lastName = new SimpleStringProperty();

        Person(int id, String firstName, String lastName) {

            this.id.set(id);
            this.firstName.set(firstName);
            this.lastName.set(lastName);
        }

        public int getId() {
            return id.get();
        }

        public IntegerProperty idProperty() {
            return id;
        }

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

        public StringProperty firstNameProperty() {
            return firstName;
        }

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

        public StringProperty lastNameProperty() {
            return lastName;
        }
    }
}