JavaFX TableView从按钮单元格获取行

时间:2017-06-16 10:35:06

标签: javafx tableview javafx-8

参考问题JavaFX TableView custom cell rendering split menu button,我能够在每一行中渲染分割菜单按钮。我按照James_D的建议更新了我的代码,并在Keyur Bhanderi的答案中更新了我的代码。 问题是获取拆分菜单所在的行的值,而不是在单击之前选择行。

更新:添加图片以查看输出

下面的图片显示了输出,我点击了每个按钮。

Row selected click frist action button

Row selected click second action button

更新了SplitMenuCellFactory.java

public ConfigRoleModel(int RoleId) //You don't need that collection in the parameter list since it doesn't look like you are using it
{
    test = RoleId;
    _fullData = new ObservableCollection<ViewRoleMapClass>(WCFclient.getViewRoleMapsByRole(RoleId));       
    saveRole = new RelayCommand<Window>(configRole);
    ViewList = _fullData;
}

更新,添加缺少的课程

SplitMenuButtonFactory.java

package com.example.splimenubtn;

import java.util.List;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;

public class SplitMenuCellFactory<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> {

 private List<MenuItemFactory<T>> menuItems;

 public SplitMenuCellFactory() {}

 public SplitMenuCellFactory(List<MenuItemFactory<T>> items) {
  menuItems = items;
 }

 @Override
 public TableCell<S, T> call(TableColumn<S, T> param) {
  return new TableCell<S, T>() {
   @Override
   public void updateItem(T item, boolean empty) {
    super.updateItem(item, empty);
    if (empty) {
     setGraphic(null);
     setText(null);
    } else {
     setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
     if (getTableRow() != null) {
      setGraphic(new SplitMenuButtonFactory<>(menuItems, getTableRow().getIndex()).buildButton());
     }
    }
   }
  };
 }
}

MenuItemsFactory.java

package com.example.splimenubtn;

 import java.util.List;
 import javafx.scene.control.MenuItem;
 import javafx.scene.control.SplitMenuButton;

 public class SplitMenuButtonFactory<T> {

  private List<MenuItemFactory<T>> menuItems;
  private int rIndex = 0;

  public SplitMenuButtonFactory(List<MenuItemFactory<T>> items) {
   menuItems = items;
  }

  public SplitMenuButtonFactory(List<MenuItemFactory<T>> items, int rI) {
   menuItems = items;
   rIndex = rI;
  }

  public SplitMenuButton buildButton() {
   SplitMenuButton menuBtn = new SplitMenuButton();
   // menuBtn.getItems().addAll(menuItems);
   for (MenuItemFactory<?> mIF : menuItems) {
    MenuItem btn = mIF.setRowIndex(rIndex).buildMenuItem();
     if (mIF.isDefault()) {
     menuBtn.setText(btn.getText());
     menuBtn.setOnAction(btn.getOnAction());
    }
    menuBtn.getItems().add(btn);
   }
  return menuBtn;
 }
}

但是当我点击按钮时,我总能获得最后的价值。

如何在按钮所在的行中获取对象?

任何帮助或暗示我指向正确方向的建议都表示赞赏。

1 个答案:

答案 0 :(得分:1)

只需使用TableCell检索onAction事件处理程序中的值(或您在未向我们展示的SplitMenuButtonFactory产品中使用的任何内容)。

简化示例

public static SplitMenuButton createSplitMenuButton(final TableCell cell) {
    SplitMenuButton result = new SplitMenuButton();

    result.setOnAction(evt -> {
        TableRow row = cell.getTableRow();
        System.out.println("row item: " +row.getItem());
    });

    return result;
}

此外,最好在单元格中重复使用SplitMenuButton并更新它,而不是每次单元格项目更改时都将其重新标记。

@Override
public TableCell<S, T> call(TableColumn<S, T> param) {
    return new TableCell<S, T>() {

        private final SplitMenuButton button = createSplitMenuButton(this);

        {
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        }

        @Override
        public void updateItem(T item, boolean empty) {
            super.updateItem(item, empty);

            if (empty) {
                setGraphic(null);
            } else {
                updateMenuButton(button, item); // placeholder for updating the button according to the new item

                setGraphic(button);
            }
        }
    };
}
相关问题