JavaFx 2 - TableView,返回所选项目

Alb*_*sut 2 tableview javafx-2

我想从TableView中将所选项目作为String获取,但它返回整个路径.

在此输入图像描述

在上面的图片中,当我点击选择单元格时,我想得到

Indicator selected is: = Shannon Entropy;
Run Code Online (Sandbox Code Playgroud)

我得到了

Indicator selected is: taindicators.indicatorsmaingui.IndicatorsGuiController$Indicators@2dafcbf
Run Code Online (Sandbox Code Playgroud)

这是我的代码

tableviewIndicators.setOnMousePressed(new EventHandler<MouseEvent>() {
        @Override 
        public void handle(MouseEvent event) {
            if (event.isPrimaryButtonDown() && event.getClickCount() == 1) {

                String indSelected = tableviewIndicators.getSelectionModel().getSelectedItem().toString();
                System.out.println("Indicator selected is: " + indSelected);                   
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

这段代码出了什么问题?

谢谢.

Bra*_*zic 14

正如我在我的评论说,这将是更好地使用ChangeListenerTableView代替EventHandler<MouseEvent>.这是一个小型演示:

编辑:Java 8

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
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 TableDemo extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {
        final Label lblTool = new Label();


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


        final TableView<TableProps> table = new TableView<>();

        //Table columns
        TableColumn<TableProps, String> clmTool = new TableColumn<>("Tool");
        clmTool.setMinWidth(160);
        clmTool.setCellValueFactory(new PropertyValueFactory<>("tool"));

        TableColumn<TableProps, String> clmChart = new TableColumn<>("Chart");
        clmChart.setMinWidth(160);
        clmChart.setCellValueFactory(new PropertyValueFactory<>("chart"));

        //Add data
        final ObservableList<TableProps> data = FXCollections.observableArrayList(
                new TableProps("Volume", ""),
                new TableProps("Shannon entropy", "")
        );
        table.setItems(data);
        table.getColumns().addAll(clmTool, clmChart);

        //Add change listener
        table.getSelectionModel().selectedItemProperty().addListener((observableValue, oldValue, newValue) -> {
            //Check whether item is selected and set value of selected item to Label
            if (table.getSelectionModel().getSelectedItem() != null) {
                lblTool.setText(newValue.getTool());
            }
        });

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

    }

    public class TableProps {
        private final SimpleStringProperty tool;
        private final SimpleStringProperty chart;

        public TableProps(String tool, String chart) {
            this.tool = new SimpleStringProperty(tool);
            this.chart = new SimpleStringProperty(chart);
        }

        public String getTool() {
            return tool.get();
        }

        public SimpleStringProperty toolProperty() {
            return tool;
        }

        public void setTool(String tool) {
            this.tool.set(tool);
        }

        public String getChart() {
            return chart.get();
        }

        public SimpleStringProperty chartProperty() {
            return chart;
        }

        public void setChart(String chart) {
            this.chart.set(chart);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

您可能还注意到,即使您移动箭头键也可以正常工作.不只是通过鼠标点击.