JavaFX中的Tablerow选择

JavaFX中的Tablerow选择,javafx,tableview,tablerow,Javafx,Tableview,Tablerow,每当我单击tableview中的一行时,我想在我的窗口(javafx)中更新一个图像。 表格类型为;以下是该类的代码: public class PopulateTable { private final SimpleIntegerProperty count; private final SimpleStringProperty name; private final SimpleStringProperty date; public PopulateTab

每当我单击tableview中的一行时,我想在我的窗口(javafx)中更新一个图像。
表格类型为
;以下是该类的代码:

public class PopulateTable {

    private final SimpleIntegerProperty count;
    private final SimpleStringProperty name;
    private final SimpleStringProperty date;

    public PopulateTable( int count, String name, String date) {
        super();
        this.count = new SimpleIntegerProperty(count);
        this.name = new SimpleStringProperty(name);
        this.date = new SimpleStringProperty(date);
    }
问题是,当我选择一行时,应该启动一个需要文件类型作为参数的方法(我正确地将其存储在数组中:
final List files=new ArrayList();
)。 我在这里找到了这个提示,但不是fuziona给我的。。。你认为我哪里错了

以下是我发现的方法:

public void printWaveForm(){ 
    Table1.setRowFactory( tv -> {
        TableRow<PopulateTable> row = new TableRow<>();
        row.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
                if(event.getClickCount()>=1) {
                    try {
                        plot();
                    } catch (java.lang.Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        return row;
        });
    }
public void printWaveForm(){
表1.setRowFactory(电视->{
TableRow行=新TableRow();
row.setOnMouseClicked(新的EventHandler(){
@凌驾
公共无效句柄(MouseeEvent事件){
如果(event.getClickCount()>=1){
试一试{
plot();
}catch(java.lang.e异常){
e、 printStackTrace();
}
}
}
});
返回行;
});
}
在这里,选择表的行后应立即启动的方法:

public void plot(){
    lineChart.getData().clear();
    xAxis = new NumberAxis();
    yAxis = new NumberAxis();;
    File file = files.get(Table1.getSelectionModel().getSelectedIndex());
    try {
        AudioWaveformCreator.start(file);
        extractedData = Waveform.points();
        XYChart.Series<Number,Number> series = new XYChart.Series<Number,Number>();
        for(int i =0; i<extractedData.size(); i++){
            series.getData().add(new XYChart.Data<Number, Number>(i,extractedData.get(i)));
        }
        lineChart.getData().add(series);
    } catch (java.lang.Exception e) {
        e.printStackTrace();
    }
}
public void plot(){
lineChart.getData().clear();
xAxis=新的数字axis();
yAxis=新的数字Axis();;
File File=files.get(表1.getSelectionModel().getSelectedIndex());
试一试{
AudioWaveformCreator.start(文件);
extractedData=波形点();
XYChart.Series系列=新的XYChart.Series();

对于(int i=0;i,由于您希望对所选项目的更改做出反应,因此可以在
TableView
的选择模型中向
selectedIndex
属性添加侦听器:

Table1.getSelectionModel().selectedIndexProperty().addListener((observable, oldValue, newValue) -> plot());

不要使用自定义行工厂。

不要在代码标记之外使用
。Stackoverflow将它们视为html元素并删除,除非它们是允许的元素,如

。使用内联代码标记(
`
),如果您想显示类似的内容。谢谢,请原谅。我应该将此行改为“Table1.setRowFactory(tv->{”@ReDePasquale Yes”,因为单击该行会导致选择的更改(除非该行已被选中;但在这种情况下,您不需要重新加载数据)。此外,如果您使用箭头键修改选择,这也会起作用。(所有操作都假设最多执行一次
printWaveForm
方法)。仍然不起作用…可能是因为我无法理解哪些值​放置而不是(可观察、旧值、新值)。您能帮我吗?@ReDePasquale抱歉,刚刚认识到我不知何故忘记了
addListener
部分…编辑了答案。@ReDePasquale如果您想选择打印当前选定的项目,您可以调用
plot
(使用单击方法时,您需要执行相同的操作,即如果实际替换了行…)只需确保确实存在一个选定项,即
selectedIndex
>=0。