这是基于TreeView in a table cell Java FX 8
的后续问题我有一张表,其中特定列必须在点击时弹出树视图。
我正在定义表列的cellfactory,如下所示
col4.setCellFactory(new Callback<TableColumn<User,DepartmentTree>, TableCell<User,DepartmentTree>>() {
@Override
public TableCell<User, DepartmentTree> call(
TableColumn<User,DepartmentTree> param)
{
TableCell<User, DepartmentTree> deptCombo = new TableCell<User,DepartmentTree>()
{
@Override
public void startEdit()
{
setGraphic(testtree);
}
@Override
public void cancelEdit()
{
setGraphic(null);
setText("");
}
@Override
public void updateItem(DepartmentTree item, boolean empty)
{
super.updateItem(item, empty);
setText(item.nameProperty().get());
}
};
return deptCombo;
}
});
这导致树视图按照第一张图像显示在单元格内。但是我希望树视图像第二个图像一样弹出单元格(第二个图像使用组合框内的树视图是不正确的实现)
弹出窗口仅仅通过样式显示还是我必须修改任何代码?
任何指导都会有所帮助。
答案 0 :(得分:2)
我创建了自己的TreeViewPopupTableCell类来执行此操作。基本上你得到的是,当左键单击一个表格单元格时(第一次单击将选择行,第二次将选择单元格),你会得到一个加载了树视图的弹出窗口。
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.PopupControl;
import javafx.scene.control.Skin;
import javafx.scene.control.Skinnable;
import javafx.scene.control.TableCell;
import javafx.scene.control.TreeItem;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
public class TreeViewPopupTableCell<S, T extends HierarchyData<T>> extends
TableCell<S, T>
{
private static final String DEFAULT_STYLE_CLASS = "combo-box-popup";
private PopupControl popup;
private TreeViewWithSetItems<T> treeviewSetItems;
private static final Logger log = Logger.getLogger(TreeViewPopup.class);
private ObservableList<T> treeList;
private double popupXlocation = 0.0;
private double popupYlocation = 0.0;
public TreeViewPopupTableCell(T hierarchyObj)
{
treeviewSetItems = new TreeViewWithSetItems<>(new TreeItem<T>());
treeList = FXCollections.observableArrayList();
treeList.add(hierarchyObj);
treeviewSetItems.setShowRoot(false);
treeviewSetItems.setItems(treeList);
treeviewSetItems.getStyleClass().add(DEFAULT_STYLE_CLASS);
this.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event)
{
popupXlocation = event.getScreenX();
popupYlocation = event.getScreenY();
}
});
treeviewSetItems.addEventHandler(KeyEvent.KEY_PRESSED,
new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event)
{
if (event.getCode().equals(KeyCode.ESCAPE))
{
popup.hide();
}
}
});
treeviewSetItems.getSelectionModel().selectedItemProperty()
.addListener(new ChangeListener<TreeItem<T>>() {
@Override
public void changed(
ObservableValue<? extends TreeItem<T>> observable,
TreeItem<T> oldValue, TreeItem<T> newValue)
{
if (oldValue == null)
return;
log.debug("Value changed");
popup.hide();
commitEdit(newValue.getValue());
}
});
}
@Override
public void startEdit()
{
T ChosenObject = this.getTableColumn().getCellData(
this.getTableRow().getIndex());
treeviewSetItems.getSelectionModel().select(
treeviewSetItems.getindexofItem(ChosenObject));
popup = new PopupControl();
popup.getStyleClass().add(DEFAULT_STYLE_CLASS);
popup.setAutoHide(true);
popup.setAutoFix(true);
popup.setHideOnEscape(true);
popup.setX(popupXlocation);
popup.setY(popupYlocation);
popup.setSkin(new Skin<Skinnable>() {
@Override
public Skinnable getSkinnable()
{
return null;
}
@Override
public Node getNode()
{
return treeviewSetItems;
}
@Override
public void dispose()
{
}
});
popup.show(this.getScene().getWindow());
}
@Override
public void cancelEdit()
{
log.debug("Cancel edit is being called");
}
@Override
public void updateItem(T item, boolean empty)
{
super.updateItem(item, empty);
setText(item.nameProperty().get());
}
}
public interface HierarchyData<T extends HierarchyData> {
/**
* The children collection, which represents the recursive nature of the hierarchy.
* Each child is again a {@link HierarchyData}.
*
* @return A list of children.
*/
ObservableList<T> getChildren();
StringProperty nameProperty();
}
要使用此单元格,请在tableview中按如下方式设置cellfactory。
col4.setCellFactory(new Callback<TableColumn<S,T>, TableCell<S,T>>() {
@Override
public TableCell<S, T> call(
TableColumn<S,T> param)
{
return new TreeViewPopupTableCell<S, T>(T obj);
}
}