Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在重用tableview上下文菜单时确定JavaFX表行详细信息_Javafx_Javafx 8 - Fatal编程技术网

在重用tableview上下文菜单时确定JavaFX表行详细信息

在重用tableview上下文菜单时确定JavaFX表行详细信息,javafx,javafx-8,Javafx,Javafx 8,我有一个类似于示例的要求 在EventHandler回调中,如何确定单击了哪一行 @Override public void handle(ActionEvent event) { // how do I get the row details when reusing context menu and handler code? } 我之所以共享关联菜单,是因为我必须将状态为“全局”的CheckMenuItem添加到表中,也就是说,如果在任何一行上选中了它,我希望在单击表中的任何其他行时

我有一个类似于示例的要求

在EventHandler回调中,如何确定单击了哪一行

@Override
public void handle(ActionEvent event) {
  // how do I get the row details when reusing context menu and handler code?
}

我之所以共享关联菜单,是因为我必须将状态为“全局”的CheckMenuItem添加到表中,也就是说,如果在任何一行上选中了它,我希望在单击表中的任何其他行时将其显示为选中状态。

使用行工厂和每行一个关联菜单,就像您链接的问题一样

对于“全局”
CheckMenuItem
,创建一个
BooleanProperty
,并将选中的
CheckMenuItem
s属性双向绑定到它

SSCCE:

import java.util.function.Function;
import java.util.stream.IntStream;

import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TableWithContextMenu extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Item> table = new TableView<>();
        table.getColumns().add(column("Item", Item::nameProperty));
        table.getColumns().add(column("Value", Item::valueProperty));

        BooleanProperty globalSelection = new SimpleBooleanProperty();

        table.setRowFactory(t -> {
            TableRow<Item> row = new TableRow<>();
            ContextMenu contextMenu = new ContextMenu();
            MenuItem item1 = new MenuItem("Do something");
            item1.setOnAction(e -> System.out.println("Do something with "+row.getItem().getName()));
            MenuItem item2 = new MenuItem("Do something else");
            item2.setOnAction(e -> System.out.println("Do something else with "+row.getItem().getName()));

            CheckMenuItem item3 = new CheckMenuItem("Global selection");
            item3.selectedProperty().bindBidirectional(globalSelection);

            contextMenu.getItems().addAll(item1, item2, new SeparatorMenuItem(), item3);
            row.emptyProperty().addListener((obs, wasEmpty, isEmpty) -> {
                if (isEmpty) {
                    row.setContextMenu(null);
                } else {
                    row.setContextMenu(contextMenu);
                }
            });
            return row ;
        });

        IntStream.rangeClosed(1, 25).mapToObj(i -> new Item("Item "+i, i)).forEach(table.getItems()::add);

        primaryStage.setScene(new Scene(new BorderPane(table), 800, 600));
        primaryStage.show();
    }

    private <S,T> TableColumn<S,T> column(String title, Function<S, ObservableValue<T>> property) {
        TableColumn<S,T> col = new TableColumn<>(title);
        col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
        return col ;
    }

    public static class Item {
        private final IntegerProperty value = new SimpleIntegerProperty();
        private final StringProperty name = new SimpleStringProperty();

        public Item(String name, int value) {
            setName(name);
            setValue(value);
        }

        public final IntegerProperty valueProperty() {
            return this.value;
        }

        public final int getValue() {
            return this.valueProperty().get();
        }

        public final void setValue(final int value) {
            this.valueProperty().set(value);
        }

        public final StringProperty nameProperty() {
            return this.name;
        }

        public final String getName() {
            return this.nameProperty().get();
        }

        public final void setName(final String name) {
            this.nameProperty().set(name);
        }


    }

    public static void main(String[] args) {
        launch(args);
    }
}
import java.util.function.function;
导入java.util.stream.IntStream;
导入javafx.application.application;
导入javafx.beans.property.BooleanProperty;
导入javafx.beans.property.IntegerProperty;
导入javafx.beans.property.SimpleBoleAnProperty;
导入javafx.beans.property.SimpleIntegerProperty;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.property.StringProperty;
导入javafx.beans.value.observeValue;
导入javafx.scene.scene;
导入javafx.scene.control.CheckMenuItem;
导入javafx.scene.control.ContextMenu;
导入javafx.scene.control.MenuItem;
导入javafx.scene.control.SeparatorMenuItem;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableRow;
导入javafx.scene.control.TableView;
导入javafx.scene.layout.BorderPane;
导入javafx.stage.stage;
公共类TableWithContextMenu扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
TableView table=新TableView();
table.getColumns().add(列(“项”,项::nameProperty));
table.getColumns().add(column(“Value”,Item::valueProperty));
BooleanProperty globalSelection=新的SimpleBoleAnProperty();
表3.setRowFactory(t->{
TableRow行=新TableRow();
ContextMenu ContextMenu=新建ContextMenu();
MenuItem1=新MenuItem(“做点什么”);
item1.setOnAction(e->System.out.println(“使用“+row.getItem().getName()”)执行操作);
MenuItem2=新MenuItem(“做其他事情”);
item2.setOnAction(e->System.out.println(“使用“+row.getItem().getName()”)执行其他操作);
CheckMenuItem3=新的CheckMenuItem(“全局选择”);
item3.selectedProperty().BindBidirective(全局选择);
contextMenu.getItems().addAll(item1,item2,新分隔符numitem(),item3);
row.emptyProperty().addListener((obs,waspempty,isEmpty)->{
如果(我是空的){
row.setContextMenu(空);
}否则{
row.setContextMenu(contextMenu);
}
});
返回行;
});
IntStream.rangeClosed(1,25).mapToObj(i->newitem(“Item”+i,i)).forEach(table.getItems()::add);
设置场景(新场景(新边框窗格(表),800600));
primaryStage.show();
}
私有表列(字符串标题、函数属性){
TableColumn col=新的TableColumn(标题);
col.setCellValueFactory(cellData->property.apply(cellData.getValue());
返回列;
}
公共静态类项{
私有最终IntegerProperty值=新的SimpleIntegerProperty();
私有最终StringProperty名称=新SimpleStringProperty();
公共项(字符串名称、int值){
集合名(名称);
设置值(值);
}
公共最终整数属性值属性(){
返回此.value;
}
公共最终整数getValue(){
返回此.valueProperty().get();
}
公共最终无效设置值(最终整数值){
this.valueProperty().set(值);
}
公共最终字符串属性nameProperty(){
返回此.name;
}
公共最终字符串getName(){
返回此.nameProperty().get();
}
公共最终void集合名(最终字符串名){
this.nameProperty().set(name);
}
}
公共静态void main(字符串[]args){
发射(args);
}
}