我想从 TableView 中获取所选项目作为字符串,但它返回整个路径。
在上图中,当我点击选中单元格时,我想得到
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/