JavaFX:TableView打印选定的行项目

时间:2018-07-28 17:24:14

标签: javafx 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

这是我要打印但无法正常工作的地方

    TableDisplay person = table.getSelectionModel().getSelectedItem();
    if(table.getSelectionModel().getSelectedItem() != null) {
        System.out.println(person.getFirstName());
    }

1 个答案:

答案 0 :(得分:1)

由于我在代码中看不到任何类型的侦听器,因此我假设您没有侦听器。

似乎您在尝试加载Scene之前就打印选择的值,这意味着用户尚未选择任何内容。

因此,在设置TableView时添加以下代码:

// 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()
        );
   }
});

现在,每当选择一行时,都会触发此Listener并打印newValue中的值,该值表示存储在所选行中的对象。


由于您没有提供自己的MCVE,请参见以下示例。您也不会声明TableViewTableColumn对象打算显示什么类型的类;这是一个较差的设计,应按照以下示例进行更新。

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;
        }
    }
}