Swing之JTree篇:设置树选中节点背景色

Swing之JTree篇:设置树选中节点背景色

2023年6月22日发(作者:)

Swing之JTree篇:设置树选中节点背景⾊这个例⼦通过实现TreeCellRenderer接⼝来改变选中节点背景⾊,对树节点操作很多都是实现这个接⼝。话不多说,通过例⼦来看效果import Layout; import ; import ent; import ion; trics; import Adapter; import Event; edReader; import ption; import Reader; import Factory; ; import out; import ; import ; ; import lPane; import rea; import ; tilities; import ger; import IResource; tMutableTreeNode; import llRenderer; public class MultiLineTreeExampleextends JFrame { public MultiLineTreeExample() { super("Multi-Line JTree Example"); String[] strs = { "swing", // 0"package", // 1 "/n" + "", // 2 "", // 3 "JTree" }; // 4 DefaultMutableTreeNode[]nodes = new DefaultMutableTreeNode[]; for (int i = 0; i < ; i++) { nodes[i] = newDefaultMutableTreeNode(strs[i]); } nodes[0].add(nodes[1]); nodes[1].add(nodes[2]); nodes[1].add(nodes[3]);nodes[0].add(nodes[4]); JTree tree = new JTree(nodes[0]); table(true); lRenderer(newMultiLineCellRenderer()); JScrollPane sp = new JScrollPane(); wport().add(tree); getContentPane().add(sp,); } public static void main(String args[]) { try {kAndFeel("sLookAndFeel"); } catch (Exception evt) {}MultiLineTreeExample frame = new MultiLineTreeExample(); dowListener(new WindowAdapter() { public voidwindowClosing(WindowEvent e) { (0); } }); e(300, 150); ible(true); } } //实现接⼝ classMultiLineCellRenderer extends JPanel implements TreeCellRenderer { protected JLabel icon; protected TreeTextArea text;public MultiLineCellRenderer() { setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); icon = new JLabel() { public voidsetBackground(Color color) { if (color instanceof ColorUIResource) color = null; kground(color); } }; add(icon);add(HorizontalStrut(4)); add(text = new TreeTextArea()); } public ComponentgetTreeCellRendererComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row,boolean hasFocus) { String stringValue = tValueToText(value, isSelected, expanded, leaf, row, hasFocus);setEnabled(led()); t(stringValue); ect(isSelected); us(hasFocus); if (leaf) {n(n("on")); } else if (expanded) { n(n("on")); }else { n(n("Icon")); } return this; } public Dimension getPreferredSize() { DimensioniconD = ferredSize(); Dimension textD = ferredSize(); int height = < ? : ; return new Dimension( + , height); } public void setBackground(Colorcolor) { if (color instanceof ColorUIResource) color = null; kground(color); } class TreeTextArea extendsJTextArea { Dimension preferredSize; TreeTextArea() { setLineWrap(true); setWrapStyleWord(true); setOpaque(true);t("dasfasdfasdfadsfa"); } public void setBackground(Color color) { if (color instanceof ColorUIResource) color =null; kground(color); } public void setPreferredSize(Dimension d) { if (d != null) { preferredSize = d; } } publicDimension getPreferredSize() { return preferredSize; } public void setText(String str) { FontMetrics fm =getToolkit().getFontMetrics(getFont()); BufferedReader br = new BufferedReader(new StringReader(str)); String line; intmaxWidth = 0, lines = 0; try { while ((line = ne()) != null) { int width = eStringWidth(fm, line); if(maxWidth < width) { maxWidth = width; } lines++; } } catch (IOException ex) { tackTrace(); } lines = (lines < 1) ? 1 :lines; int height = ght() * lines; setPreferredSize(new Dimension(maxWidth + 6, height)); t(str); } voidsetSelect(boolean isSelected) { Color bColor; if (isSelected) { bColor = or("ionBackground"); }else { bColor = or("ckground"); } kground(bColor); } void setFocus(booleanhasFocus) { if (hasFocus) { Color lineColor = UIManager .getColor("ionBorderColor");setBorder(LineBorder(lineColor)); } else { setBorder(EmptyBorder(1, 1, 1, 1)); } } }} import Layout; import ; import ent; import ion; trics; import Adapter; import Event; edReader; import ption; import Reader; import Factory; ; import out; import ; import ; ; import lPane; import rea; import ; tilities; import ger; import IResource; tMutableTreeNode; import llRenderer; public class MultiLineTreeExampleextends JFrame { public MultiLineTreeExample() { super("Multi-Line JTree Example"); String[] strs = { "swing", // 0"package", // 1 "/n" + "", // 2 "", // 3 "JTree" }; // 4 DefaultMutableTreeNode[]nodes = new DefaultMutableTreeNode[]; for (int i = 0; i < ; i++) { nodes[i] = newDefaultMutableTreeNode(strs[i]); } nodes[0].add(nodes[1]); nodes[1].add(nodes[2]); nodes[1].add(nodes[3]);nodes[0].add(nodes[4]); JTree tree = new JTree(nodes[0]); table(true); lRenderer(newMultiLineCellRenderer()); JScrollPane sp = new JScrollPane(); wport().add(tree); getContentPane().add(sp,); } public static void main(String args[]) { try {kAndFeel("sLookAndFeel"); } catch (Exception evt) {}MultiLineTreeExample frame = new MultiLineTreeExample(); dowListener(new WindowAdapter() { public voidwindowClosing(WindowEvent e) { (0); } }); e(300, 150); ible(true); } } //实现接⼝ classMultiLineCellRenderer extends JPanel implements TreeCellRenderer { protected JLabel icon; protected TreeTextArea text;public MultiLineCellRenderer() { setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); icon = new JLabel() { public voidsetBackground(Color color) { if (color instanceof ColorUIResource) color = null; kground(color); } }; add(icon);add(HorizontalStrut(4)); add(text = new TreeTextArea()); } public ComponentgetTreeCellRendererComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row,boolean hasFocus) { String stringValue = tValueToText(value, isSelected, expanded, leaf, row, hasFocus);setEnabled(led()); t(stringValue); ect(isSelected); us(hasFocus); if (leaf) {n(n("on")); } else if (expanded) { n(n("on")); }else { n(n("Icon")); } return this; } public Dimension getPreferredSize() { DimensioniconD = ferredSize(); Dimension textD = ferredSize(); int height = < ? : ; return new Dimension( + , height); } public void setBackground(Colorcolor) { if (color instanceof ColorUIResource) color = null; kground(color); } class TreeTextArea extendsJTextArea { Dimension preferredSize; TreeTextArea() { setLineWrap(true); setWrapStyleWord(true); setOpaque(true);t("dasfasdfasdfadsfa"); } public void setBackground(Color color) { if (color instanceof ColorUIResource) color =null; kground(color); } public void setPreferredSize(Dimension d) { if (d != null) { preferredSize = d; } } publicDimension getPreferredSize() { return preferredSize; } public void setText(String str) { FontMetrics fm =getToolkit().getFontMetrics(getFont()); BufferedReader br = new BufferedReader(new StringReader(str)); String line; intmaxWidth = 0, lines = 0; try { while ((line = ne()) != null) { int width = eStringWidth(fm, line); if(maxWidth < width) { maxWidth = width; } lines++; } } catch (IOException ex) { tackTrace(); } lines = (lines < 1) ? 1 :lines; int height = ght() * lines; setPreferredSize(new Dimension(maxWidth + 6, height)); t(str); } voidsetSelect(boolean isSelected) { Color bColor; if (isSelected) { bColor = or("ionBackground"); }else { bColor = or("ckground"); } kground(bColor); } void setFocus(booleanhasFocus) { if (hasFocus) { Color lineColor = UIManager .getColor("ionBorderColor");setBorder(LineBorder(lineColor)); } else { setBorder(EmptyBorder(1, 1, 1, 1)); } } }}

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1687382533a5912.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信