I am trying to remove the folder symbol from node of JTree which comes by default. How can I accomplish this?
How to remove folder symbol which comes in front of each node from JTree in java
Just for reference, here's a complete example:
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.UIManager;
/** @see http://stackoverflow.com/questions/5260223 */
public class JTreeLite {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
//@Override
public void run() {
createGUI();
}
});
}
private static void createGUI() {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Icon empty = new TreeIcon();
UIManager.put("Tree.closedIcon", empty);
UIManager.put("Tree.openIcon", empty);
UIManager.put("Tree.collapsedIcon", empty);
UIManager.put("Tree.expandedIcon", empty);
UIManager.put("Tree.leafIcon", empty);
JTree jt = new JTree();
frame.add(jt);
frame.pack();
frame.setSize(300, 400);
frame.setVisible(true);
}
}
class TreeIcon implements Icon {
private static int SIZE = 0;
public TreeIcon() {
}
public int getIconWidth() {
return SIZE;
}
public int getIconHeight() {
return SIZE;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
System.out.println(c.getWidth() + " " + c.getHeight() + " " + x + " " + y);
}
}
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) jTree.getCellRenderer();
renderer.setLeafIcon(null);
renderer.setClosedIcon(null);
renderer.setOpenIcon(null);
The jtree uses CellRender, for example DefaultTreeCellRenderer, and you can dasable or change default nodes icon. Also, you can create custom cellRender and defined more dificult logic for you icon scheme.
tree.setCellRenderer(new DefaultTreeCellRenderer() {
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
JLabel component = (JLabel) super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
ImageIcon iconPath = ((WizardNode) value).getIcon();
component.setIcon(iconPath);
return this;
}
});
post some explanation to your code and don't just leave a snippet! –
Soursop
The Swing tutorial talks specifically about doing this:
http://download.oracle.com/javase/tutorial/uiswing/components/tree.html#display
You can easily change the default icon used for leaf, expanded branch, or collapsed branch nodes. To do so, you first create an instance of DefaultTreeCellRenderer.
Here's an example; keep the size but don't draw anything. –
Amye
© 2022 - 2024 — McMap. All rights reserved.