javafx-2 - JavaFx 2 - TableView,返回所选项目

标签 javafx-2 tableview

我想从 TableView 中获取所选项目作为字符串,但它返回整个路径。

enter image description here

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

Indicator selected is: = Shannon Entropy;

当我得到

Indicator selected is: taindicators.indicatorsmaingui.IndicatorsGuiController$Indicators@2dafcbf

这是我的代码

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

这段代码有什么问题?

谢谢。

最佳答案

正如我在评论中所述,最好使用 ChangeListener对于 TableView而不是 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);
        }
    }

}

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

关于javafx-2 - JavaFx 2 - TableView,返回所选项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18374626/

相关文章:

mysql - 在javafx中过滤要从数据库显示的数据

未使用 JavaFX 2 slider labelFormatter

java - 如何在 javafx 应用程序中使用 StackOverflow 询问按钮格式?

javafx-2 - Javafx 组合框样式

swift - 如何将Alamofire生成的数据从tableview推送到webview

java - 过滤 tableView 在特定列中留下一些数据

java - 文本对象拒绝改变颜色,尽管 CSS 指令这样做

Netbeans 和 JavaFX - 每次运行时都进行清理和构建?

ios - 如何根据包含 tableview 的 View Controller 转换单元格框架的原点?

通过按钮编辑时,JavaFX TableView 的行为很奇怪