This is a follow up question based on TreeView in a table cell Java FX 8
I am having a table in which a particular column has to popup a treeview on click.
I
I have created my own class of TreeViewPopupTableCell to do this. Basically what you get is, when a table cell is left clicked (first click will choose the row,second will choose the cell) you get a popup with a treeview loaded in it.
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> extends
TableCell
{
private static final String DEFAULT_STYLE_CLASS = "combo-box-popup";
private PopupControl popup;
private TreeViewWithSetItems treeviewSetItems;
private static final Logger log = Logger.getLogger(TreeViewPopup.class);
private ObservableList treeList;
private double popupXlocation = 0.0;
private double popupYlocation = 0.0;
public TreeViewPopupTableCell(T hierarchyObj)
{
treeviewSetItems = new TreeViewWithSetItems<>(new TreeItem());
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() {
@Override
public void handle(MouseEvent event)
{
popupXlocation = event.getScreenX();
popupYlocation = event.getScreenY();
}
});
treeviewSetItems.addEventHandler(KeyEvent.KEY_PRESSED,
new EventHandler() {
@Override
public void handle(KeyEvent event)
{
if (event.getCode().equals(KeyCode.ESCAPE))
{
popup.hide();
}
}
});
treeviewSetItems.getSelectionModel().selectedItemProperty()
.addListener(new ChangeListener>() {
@Override
public void changed(
ObservableValue extends TreeItem> observable,
TreeItem oldValue, TreeItem 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() {
@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 {
/**
* The children collection, which represents the recursive nature of the hierarchy.
* Each child is again a {@link HierarchyData}.
*
* @return A list of children.
*/
ObservableList getChildren();
StringProperty nameProperty();
}
To use this cell, in your tableview set the cellfactory as follows.
col4.setCellFactory(new Callback, TableCell>() {
@Override
public TableCell call(
TableColumn param)
{
return new TreeViewPopupTableCell(T obj);
}
}