我知道google上有关于JTree图标更改的相关资料,但我仍然无法理清我的简单需求。
我需要更改 JTree 的
Root
元素的图标,使其与 node
和 leaf
不同。
我设法更改节点和叶子图标,但默认箭头图标(图中红色突出显示)仍然存在,我必须单击它才能向下钻取树,而我想单击自定义图像图标以向下钻取树.
/** CHANGE JTREE DEFAULT ICONS **/
private class userRendererForTree extends DefaultTreeCellRenderer {
private static final long serialVersionUID = 1L;
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
if (leaf) {
setIcon(setImageIcon(AppConstants.BOM_ROUTE_NODE_LEAF));
} else {
setIcon(setImageIcon(AppConstants.BOM_ROUTE_NODE_RIGHT));
}
if (expanded) {
setIcon(setImageIcon(AppConstants.BOM_ROUTE_NODE_BOTTOM));
}
return this;
}
}
/** LOAD IMAGE ICON FROM RESOURCES **/
private ImageIcon setImageIcon(String path){
Image image = null;
ImageIcon icon = null;
try {
image = LoadResource.getImageFromResourceAsURL(path);
image = image.getScaledInstance(10, 10, Image.SCALE_SMOOTH);
icon = new ImageIcon(image);
return icon;
} catch (Exception e) {
e.printStackTrace();
}
return icon;
}
渲染器的调用:-
JTree BomTree = new JTree(createTreeModel());
scrollPane.setColumnHeaderView(BomTree);
BomTree.setCellRenderer(new userRendererForTree());
我很抱歉,你的意思是你想要替换展开/折叠手柄,而不是自定义单元格的展开/关闭图标......因为
JTree
并不令人困惑🙄
为此,您需要提供自己的外观实现或自定义现有的 UI 属性。
感谢 TrashGod,第二种方法相对简单,只需记住,这会影响应用程序中的所有
JTree
。
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.put("Tree.expandedIcon", new ImageIcon(ImageIO.read(getClass().getResource("/images/icons8-expand-arrow-24.png"))));
UIManager.put("Tree.collapsedIcon", new ImageIcon(ImageIO.read(getClass().getResource("/images/icons8-collapse-arrow-24.png"))));
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
public class TestPane extends JPanel {
public TestPane() throws IOException {
setLayout(new BorderLayout());
JTree tree = new JTree();
tree.setShowsRootHandles(true);
List<Item> items = new ArrayList<>(8);
items.add(new Item("Bowle",
new Item[]{
new Item("Apple"),
new Item("Pear"),
new Item("Orangle")
}));
items.add(new Item("Freezer",
new Item[]{
new Item("Top draw", new Item[]{
new Item("Chicken"),
new Item("Steak"),
new Item("Fish")}),
new Item("Bottom draw", new Item[]{
new Item("Pears"),
new Item("Chips"),
new Item("Ice cream")})
}));
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("My stuff");
for (Item item : items) {
populate(rootNode, item);
}
DefaultTreeModel model = new DefaultTreeModel(rootNode);
tree.setModel(model);
tree.setCellRenderer(new ItemTreeCellRenderer());
add(new JScrollPane(tree));
}
protected void populate(DefaultMutableTreeNode parent, Item item) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(item);
parent.add(node);
for (Item child : item.getContents()) {
populate(node, child);
}
}
}
class ItemTreeCellRenderer extends DefaultTreeCellRenderer {
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
if (value instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
Object userValue = node.getUserObject();
if (userValue instanceof Item) {
Item item = (Item) userValue;
value = item.getName();
}
}
return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
}
}
public class Item {
private String name;
private List<Item> contents;
public Item(String name) {
this.name = name;
this.contents = new ArrayList<>(8);
}
public Item(String name, Item[] children) {
this.name = name;
this.contents = new ArrayList<>(Arrays.asList(children));
}
public String getName() {
return name;
}
public List<Item> getContents() {
return contents;
}
}
}