如何为tableView行定义setOnAction? [英] How to define setOnAction for tableView rows?

查看:88
本文介绍了如何为tableView行定义setOnAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写具有javafx和tableView功能的程序.

I am writing a program with javafx and tableView feature.

我的目的是当我单击该表的行时,另一个窗口打开并显示一些内容,但是我不知道如何为我的表定义诸如setOnMouseClicked功能.

My purpose is when I click on a row of this table,another window opens and shows something but I don't know how to define something like setOnMouseClicked feature for my table.

我搜索了很多,但找不到简单的方法

I searched a lot but I coudn't find a simple way

这是我现有的定义表列和行的代码.(行是通过可观察的功能定义的)

This is my existing code that define table columns and rows.(rows are defined with observable feature)

package sample;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
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) {

    TableView tableView = new TableView();

    TableColumn<String, Account> column1 = new TableColumn<>("UserName");
    column1.setCellValueFactory(new PropertyValueFactory<>("userName"));
    column1.setMinWidth(100);

    TableColumn<String, Account> column2 = new TableColumn<>("PassWord");
    column2.setCellValueFactory(new PropertyValueFactory<>("passWord"));
    column2.setMinWidth(100);


    tableView.getColumns().add(column1);
    tableView.getColumns().add(column2);
    tableView.setItems(getAllAccounts());



    VBox vbox = new VBox(tableView);

    Scene scene = new Scene(vbox,200,200);
    Stage window ;

    window = primaryStage;

    window.setScene(scene);
    window.show();
}
private ObservableList<Account> getAllAccounts(){
ObservableList<Account> accounts= FXCollections.observableArrayList(Account.getAccounts());
return accounts;

}


}

推荐答案

您实际上有两个选择:

方法1:

TableView上实现单击侦听器,并检索所选的项目.

Implement a click listener on the TableView and retrieve the item that was selected.

// Listen for a mouse click and access the selectedItem property
tblAccounts.setOnMouseClicked(event -> {
    // Make sure the user clicked on a populated item
    if (tblAccounts.getSelectionModel().getSelectedItem() != null) {
        System.out.println("You clicked on " + tblAccounts.getSelectionModel().getSelectedItem().getUsername());
    }
});

方法2:

TableView创建自己的RowFactory并在那里处理逻辑. (我更喜欢这种方法)

Create your own RowFactory for the TableView and handle your logic there. (I prefer this method)

// Create a new RowFactory to handle actions
tblAccounts.setRowFactory(tv -> {

    // Define our new TableRow
    TableRow<Account> row = new TableRow<>();
    row.setOnMouseClicked(event -> {
        System.out.println("Do your stuff here!");
    });
    return row;
});


方法1是最简单的方法,可以满足大多数需求.您将需要使用方法2来满足更复杂的需求,例如设置单个行的样式或处理对空行的点击.


Method #1 is the simplest approach and will work for most needs. You'll want to use method #2 for more complex needs, such as styling the individual rows, or to handle clicks on empty rows.

这篇关于如何为tableView行定义setOnAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆