use of javax.swing.tree.ExpandVetoException in project java-swing-tips by aterai.
the class EmptyIcon method updateUI.
@Override
public void updateUI() {
setCellRenderer(null);
removeTreeWillExpandListener(listener);
super.updateUI();
setUI(new BasicTreeUI() {
@Override
public Rectangle getPathBounds(JTree tree, TreePath path) {
if (Objects.nonNull(tree) && Objects.nonNull(treeState)) {
return getTreePathBounds(path, tree.getInsets(), new Rectangle());
}
return null;
}
private Rectangle getTreePathBounds(TreePath path, Insets insets, Rectangle bounds) {
Rectangle rect = treeState.getBounds(path, bounds);
if (Objects.nonNull(rect)) {
rect.width = tree.getWidth();
rect.y += insets.top;
}
return rect;
}
});
UIManager.put("Tree.repaintWholeRow", Boolean.TRUE);
TreeCellRenderer r = getCellRenderer();
setCellRenderer((tree, value, selected, expanded, leaf, row, hasFocus) -> {
Component c = r.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
c.setBackground(selected ? SELECTED_COLOR : tree.getBackground());
if (c instanceof JComponent) {
((JComponent) c).setOpaque(true);
}
return c;
});
setOpaque(false);
setRootVisible(false);
// https://ateraimemo.com/Swing/TreeNodeCollapseVeto.html
listener = new TreeWillExpandListener() {
@Override
public void treeWillExpand(TreeExpansionEvent e) {
// throw new ExpandVetoException(e, "Tree expansion cancelled");
}
@Override
public void treeWillCollapse(TreeExpansionEvent e) throws ExpandVetoException {
throw new ExpandVetoException(e, "Tree collapse cancelled");
}
};
addTreeWillExpandListener(listener);
}
use of javax.swing.tree.ExpandVetoException in project java-swing-tips by aterai.
the class BackgroundTask method treeWillExpand.
@Override
public void treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException {
TreePath path = e.getPath();
Object o = path.getLastPathComponent();
if (o instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) o;
File file = (File) node.getUserObject();
String name = file.getName();
// System.out.println(name);
if (!name.isEmpty() && name.codePointAt(0) == '.') {
throw new ExpandVetoException(e, "Tree expansion cancelled");
}
}
}
Aggregations