JavaFX 2:获取TableCell行索引 [英] JavaFX 2: Get TableCell Row Index

查看:77
本文介绍了JavaFX 2:获取TableCell行索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带复选框的表格.当我单击第三或第四列中的复选框时,我想更改第一列中复选框的选择.我希望能够更改同一行上的其他单元格.我已经有了列,所以我想知道单元格所在的行.我也不确定目前为止是否正确.

I have a Table with checkboxes. I want to change the selection of the checkbox in the first column when I click on the checkbox in the third or fourth column. I want to be able to change the other cells on the same row. I already have the columns so I want to know what row the cell is in. I am also very uncertain whether I have it right so far or not.

到目前为止,我所做的主要是

What I have done so far I figured mostly from

  • http://download.oracle.com/javafx/2.0/ui_controls/list-view.htm
  • http://download.oracle.com/javafx/2.0/ui_controls/table-view.htm
  • http://download.oracle.com/javafx/2.0/api/index.html?javafx/scene/control/Cell.html

这是我的SSCCE(简短的自包含可编译示例)

如果下面的代码有问题,请纠正我.

Please correct me if there is something wrong with the code below.

package javafxapplication5;

import javafx.application.Application;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;

public class JavaFXApplication extends Application {

    private static final ObservableList<ContactOptions> addContactOption = FXCollections.observableArrayList(
            new ContactOptions("Yes", "John Doe", "No", "Yes"),
            new ContactOptions("Yes", "Jane Doe", "No", null),
            new ContactOptions("Yes", "John Smith", "Yes", "Yes"),
            new ContactOptions("Yes", "Patty Smith", "Yes", "No"),
            new ContactOptions("Yes", "Jo Johnson", "Yes", "Yes"),
            new ContactOptions("No", "Mary Johnson", "No", "No"),
            new ContactOptions("Yes", "Clint Doe", "No", null),
            new ContactOptions("Yes", "Sally Sue", "No", "Yes"),
            new ContactOptions("Yes", "Bob Ryan", null, "Yes"),
            new ContactOptions("No", "Mary Sue", "No", "No"),
            new ContactOptions("Yes", "Bob Smith", "No", "Yes"));
    private static TableView<ContactOptions> contactOptions = new TableView<ContactOptions>();

    public static void main(String[] args) {
        Application.launch(JavaFXApplication.class, args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World");
        Group root = new Group();
        Scene scene = new Scene(root, 400, 200, Color.LIGHTGREEN);

        Callback<TableColumn, TableCell> cellFactory = new Callback<TableColumn, TableCell>() {

            @Override
            public TableCell call(final TableColumn param) {
                final CheckBox checkBox = new CheckBox();
                final TableCell cell = new TableCell() {

                    @Override
                    public void updateItem(Object item, boolean empty) {
                        super.updateItem(item, empty);
                        if (item == null) {
                            checkBox.setDisable(true);
                            checkBox.setSelected(false);
                        } else {
                            checkBox.setDisable(false);
                            checkBox.setSelected(item.toString().equals("Yes") ? true : false);
                            commitEdit(checkBox.isSelected() ? "Yes" : "No");
                        }
                    }
                };
                cell.setNode(checkBox);
                return cell;
            }
        };

        TableColumn firstCol = new TableColumn("Contact?");
        firstCol.setPrefWidth(60);
        firstCol.setProperty("one");
        firstCol.setCellFactory(cellFactory);

        TableColumn secondCol = new TableColumn("Name");
        secondCol.setPrefWidth(200);
        secondCol.setSortAscending(true);
        secondCol.setProperty("two");

        TableColumn thirdCol = new TableColumn("Call");
        thirdCol.setPrefWidth(60);
        thirdCol.setProperty("three");
        thirdCol.setCellFactory(cellFactory);

        TableColumn fourthCol = new TableColumn("Email");
        fourthCol.setPrefWidth(60);
        fourthCol.setProperty("four");
        fourthCol.setCellFactory(cellFactory);

        contactOptions.setItems(addContactOption);
        contactOptions.getColumns().addAll(firstCol, secondCol, thirdCol, fourthCol);
        contactOptions.setPrefSize(400, 200);

        root.getChildren().add(contactOptions);
        primaryStage.setScene(scene);
        primaryStage.setVisible(true);
    }

    public static class ContactOptions {

        private final StringProperty one;
        private final StringProperty two;
        private final StringProperty three;
        private final StringProperty four;

        ContactOptions(String col1, String col2, String col3, String col4) {
            this.one = new StringProperty(col1);
            this.two = new StringProperty(col2);
            this.three = new StringProperty(col3);
            this.four = new StringProperty(col4);
        }

        public String getOne() {
            return one.get();
        }

        public String getTwo() {
            return two.get();
        }

        public String getThree() {
            return three.get();
        }

        public String getFour() {
            return four.get();
        }
    }
}

推荐答案

快到了

在调用commitEdit之前,必须先调用getTableView().edit(getTableRow().getIndex(), param).这使单元进入编辑模式".由于没有startEdit方法,因此进入编辑模式的参与很少,但是仍然是必需的.

Almost There

Before calling commitEdit, it is necessary to call getTableView().edit(getTableRow().getIndex(), param). This puts the cell into "editing mode". Since there is no startEdit method, there is very little involved in entering edit mode, but it is still required.

之后,如下所述: http://download.oracle .com/javafx/2.0/ui_controls/table-view.htm

有必要打电话

firstCol.setOnEditCommit(new EventHandler<EditEvent<String>>() {
    @Override
    public void handle(EditEvent<String> event) {
        String newValue = event.getNewValue();
        ContactOptions data = (ContactOptions) event.getTableView().getItems().get(event.getTablePosition().getRow());
        data.one.set(newValue)
        if(newValue.equals("No")) {
            data.three.set("No");
            data.four.set("No");
        }
    }
}

现在我所需要知道的是如何更新数据更新后表格的显示.

Now all I need to know is how to update the table's display once the data is updated.

这篇关于JavaFX 2:获取TableCell行索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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