首页 > 解决方案 > 在同一行JavaFX中单击按钮从tableview中获取行

问题描述

我在 JavaFX 中创建了一个 TableView,它包含两列:

我的方法是:

    static ObservableList<Student> list= FXCollections.observableArrayList();
    static ObservableList<Student> getStudentList() throws SQLException {
        con = ConnectionUtil.getDataBaseConnection();

        String SQL = "Select * from student;";            
            ResultSet rs = con.createStatement().executeQuery(SQL);  
            while(rs.next()){
              //Student cm = new Student(rs.getInt("rno"), rs.getString("name"),false);
                Student cm = new Student();
                cm.setFullName(rs.getString("name"));
                cm.setRoll(rs.getString("rno"));
                cm.setSingle(false);
              list.add(cm); 
            } 
      return list;
    }

    static TableColumn<Student, String> rollCol = new TableColumn<Student, String>("Roll");

    static TableColumn<Student, Void> viewCol = new TableColumn<Student, Void>("Details");

      static TableColumn<Student, Void>  addButtonToTable() {
        //TableColumn<Student, Void> viewCol = new TableColumn("Button Column");

        Callback<TableColumn<Student, Void>, TableCell<Student, Void>> cellFactory = new Callback<TableColumn<Student, Void>, TableCell<Student, Void>>() {
            @Override
            public TableCell<Student, Void> call(final TableColumn<Student, Void> param) {
                final TableCell<Student, Void> cell = new TableCell<Student, Void>() {

                    private final Button btn = new Button("View");

                    {

                        btn.setOnAction((ActionEvent event) -> {

                            /////////////////WORK START FROM HERE
                            static Student stu=tables.getItems();

                            String res = (stu.getRollNo().toString());

                            System.out.println(res);
                        });
                    }

                    @Override
                    public void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(btn);
                        }
                    }
                };
                return cell;
            }
        };

        viewCol.setCellFactory(cellFactory);

        return viewCol;
    }
    public String getRollNo() {
        return rollNo;
    }
    public void setRoll(String rollNo) {
        this.rollNo = rollNo;
    }   

我的问题:

请告诉我我该怎么做...在此先感谢

标签: buttonjavafxtableviewrow

解决方案


static不允许声明局部变量并TableView.getItems返回 a ObservableList,而不是Student。通过访问Student实例TableRow。(如果您将所有按钮显示在一行中,而不是在一列中,则所需的逻辑将非常不同。)

private final Button btn = new Button("View");

{

    btn.setOnAction((ActionEvent event) -> {
        Student stu = getTableRow().getItem();
        String res = stu.getRollNo().toString();
        System.out.println(res);
    });
}

@Override
public void updateItem(Void item, boolean empty) {
    super.updateItem(item, empty);
    setGraphic(empty ? null : btn);
}

推荐阅读