gpt4 book ai didi

java - 如何确保代码在继承时按特定顺序执行

转载 作者:行者123 更新时间:2023-12-01 08:58:38 25 4
gpt4 key购买 nike

我有一个扩展TableView的父类(super class),我想要制作的每个表都应该继承它。

这行代码需要为每个从父类(super class)继承并需要 ContextMenu 的类执行:

// Sets the context menu for each specific row. This way no
// context menu will appear if we click on a row that is
// empty.
setRowFactory(r -> {
TableRow<ObservableList<String>> row = new TableRow<ObservableList<String>>() {
public void updateItem(ObservableList<String> item, boolean empty) {
super.updateItem(item, empty);
if (isEmpty()) {
setContextMenu(null);
} else {
setContextMenu(contextMenu);
}
}
};
});

问题是 ContextMenu 需要在执行此代码之前实例化,因此我无法放入父类(super class)的构造函数。

如果表需要 ContextMenu(并非总是如此),如何确保从父类(super class)继承的每个类都按此顺序执行以下步骤:

  1. 使用 MenuItem 实例实例化 ContextMenu(理论上,我猜 MenuItem 实例可以稍后实例化并添加)。
  2. 使用第 1 步中实例化的上下文菜单调用上面的“setRowFactory”代码。

我在解决这个问题上进行了几次迭代,但仍然未能找到令人满意的解决方案。也许我遗漏了一些明显的东西。

编辑:如果您能以某种方式禁止用户使用 TableView 的原始 setContextMenu 方法,以确保 `ContextMenu正在以正确的方式添加。但我想这是不可能的。

最佳答案

看起来您只是试图禁止在表中的空行上显示上下文菜单。更简单的方法是

public class MyTableView<T> extends TableView<T> {

public MyTableView() {
setRowFactory(tv -> {
TableRow<T> row = new TableRow<>();
row.setOnContextMenuRequested(e -> {
if (row.isEmpty()) {
e.consume();
}
}
return row ;
});
}

// ...
}

要回答您提出的问题(确保您仅(且始终)在调用 setContextMenu 之后调用 setRowFactory):

你可以这样做

public class MyTableView<T> extends TableView<T> {

private final ContextMenu contextMenu ;

public MyTableView(ContextMenu contextMenu) {
this.contextMenu = contextMenu ;

setRowFactory(r -> {
TableRow<ObservableList<String>> row = new TableRow<ObservableList<String>>() {
public void updateItem(ObservableList<String> item, boolean empty) {
super.updateItem(item, empty);
if (isEmpty()) {
setContextMenu(null);
} else {
setContextMenu(contextMenu);
}
}
};
});

}

// ...
}

public class MyTableView<T> extends TableView<T> {

private ContextMenu rowContextMenu ;

public ContextMenu getRowContextMenu() {
return rowContextMenu ;
}

public void setRowContextMenu(ContextMenu contextMenu) {
this.rowContextMenu = contextMenu ;

setRowFactory(r -> new TableRow<ObservableList<String>>() {
public void updateItem(ObservableList<String> item, boolean empty) {
super.updateItem(item, empty);
if (isEmpty()) {
setContextMenu(null);
} else {
setContextMenu(rowContextMenu);
}
}
});
}

// ...
}

甚至

public class MyTableView<T> extends TableView<T> {

public MyTableView() {
contextMenuProperty().addListener((obs, oldContextMenu, newContextMenu) -> {
if (newContextMenu == null) {
setRowFactory(r -> new TableRow<>());
} else
setRowFactory(r -> new TableRow<ObservableList<String>>() {
public void updateItem(ObservableList<String> item, boolean empty) {
super.updateItem(item, empty);
if (isEmpty()) {
setContextMenu(null);
} else {
setContextMenu(newContextMenu);
}
}
});
}
});
}

// ...
}

关于java - 如何确保代码在继承时按特定顺序执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41873170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com