正确的方式使用JTextPane创建Class,JTree,DefaultTreeCellRenderer

编程入门 行业动态 更新时间:2024-10-15 06:20:12
本文介绍了正确的方式使用JTextPane创建Class,JTree,DefaultTreeCellRenderer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个类是为了使用JTree在叶子中显示多种颜色...

该类是 TextPaneDefaultTreeCellRenderer

import java.awt。*; import java.util。*; import javax.swing。*; import javax.swing.text。*; import javax.swing.tree。*; 公共类TextPaneDefaultTreeCellRenderer扩展DefaultTreeCellRenderer { TextPaneTreeCellRenderer textPaneScrollPane = new TextPaneTreeCellRenderer(); public TextPaneDefaultTreeCellRenderer(){ initialize(); } private void initialize(){ textPaneScrollPane.setBackgroundNonSelectionColor(getBackgroundNonSelectionColor()); textPaneScrollPane.setBackgroundSelectionColor(getBackgroundSelectionColor()); textPaneScrollPane.setTextNonSelectionColor(getTextNonSelectionColor()); textPaneScrollPane.setTextSelectionColor(getTextSelectionColor()); } @Override public Component getTreeCellRendererComponent(JTree tree, Object value,boolean selected,boolean expanded,boolean leaf, int row,boolean hasFocus){ if(leaf){ DefaultMutableTreeNode node =(DefaultMutableTreeNode)value; Object obj = node.getUserObject(); if(obj!= null){ if(obj instanceof DecoratedText || obj instanceof DecoratedText []){ if(textPaneScrollPane == null){ System.out。 println(textPaneScrollPane:+ textPaneScrollPane); } 返回textPaneScrollPane.getTreeCellRendererComponent(树,值,选中,展开,叶,行,hasFocus); } } } 返回super.getTreeCellRendererComponent(树,值,选中,展开,叶,行,hasFocus); } } class TextPaneTreeCellRenderer扩展DefaultTreeCellRenderer { JTextPane textPane; public TextPaneTreeCellRenderer(){ textPane = new JTextPane(); add(textPane); } @Override public void setBackgroundNonSelectionColor(Color color){ this.backgroundNonSelectionColor = color; } @Override public void setBackgroundSelectionColor(Color color){ this.backgroundSelectionColor = color; } @Override public void setTextNonSelectionColor(Color color){ this.textNonSelectionColor = color; } @Override public void setTextSelectionColor(Color color){ this.textSelectionColor = color; } @Override public Component getTreeCellRendererComponent(JTree tree, Object value,boolean selected,boolean expanded,boolean leaf, int row,boolean hasFocus){ DefaultMutableTreeNode node =(DefaultMutableTreeNode)value; Object object = node.getUserObject(); if(selected){ setForeground(textSelectionColor); setBackground(backgroundSelectionColor); textPane.setForeground(textSelectionColor); textPane.setBackground(backgroundSelectionColor); } else { setForeground(textNonSelectionColor); setBackground(backgroundNonSelectionColor); textPane.setForeground(textNonSelectionColor); textPane.setBackground(backgroundNonSelectionColor); } textPane.setText(); Document doc = textPane.getStyledDocument(); if(object!= null&& object instanceof DecoratedText){ DecoratedText decText =(DecoratedText)object; try { doc.insertString(doc.getLength(),decText.getText(),getAttributeSet(decText)); } catch(BadLocationException ex){ Logger.getLogger(TextPaneTreeCellRenderer.class.getName())。log(Level.SEVERE,null,ex); } }否则if(object!= null&& object instanceof DecoratedText []){ DecoratedText [] arrayDecText =(DecoratedText [])object; for(DecoratedTextText decText:arrayDecText){ try { //doc.insertString(doc.getLength(),decText.getText(),getAttributeSet(decText)); doc.insertString(doc.getLength(),decText.getText(),decText.getAttributeSet(textPane)); } catch(BadLocationException ex){ System.out.println(decText:+ decText); System.out.println(arrayDecText:+ arrayDecText); System.out.println(doc:+ doc.getLength()); Logger.getLogger(TextPaneTreeCellRenderer.class.getName())。log(Level.SEVERE,null,ex); } } } else {返回new DefaultTreeCellRenderer()。getTreeCellRendererComponent(tree, value,leaf,expanded,leaf,row,hasFocus); } 返回textPane; } private SimpleAttributeSet getAttributeSet(DecoratedText decoratedText){ SimpleAttributeSet attrSet = new SimpleAttributeSet(); if(decoratedText!= null){ if(decoratedText.getBackground()!= null){ StyleConstants.setBackground(attrSet,decoratedText.getBackground()); } else { StyleConstants.setBackground(attrSet,textPane.getBackground()); } if(decoratedText.getForeground()!= null){ StyleConstants.setForeground(attrSet,decoratedText.getForeground()); } else { StyleConstants.setForeground(attrSet,textPane.getForeground()); } 字体字体; if(decoratedText.getFont()!= null){ font = decoratedText.getFont(); } else { font = textPane.getFont(); } StyleConstants.setFontFamily(attrSet,font.getFamily()); StyleConstants.setItalic(attrSet,font.isItalic()); StyleConstants.setBold(attrSet,font.isBold()); StyleConstants.setFontSize(attrSet,font.getSize()); } 返回attrSet; } }

替代类是 TreeCellRendererTextPane

import java.awt.Component; import java.util.logging。*; import javax.swing。*; import javax.swing.text。*; import javax.swing.tree。*; 公共类TreeCellRendererTextPane扩展JTextPane实现TreeCellRenderer { @Override public Component getTreeCellRendererComponent(JTree tree, Object value,boolean selected,boolean expanded, boolean leaf, int row,boolean hasFocus){ if(leaf){ DefaultMutableTreeNode node =(DefaultMutableTreeNode)value; Object obj = node.getUserObject(); if(obj!= null){ if(obj instanceof DecoratedText || obj instanceof DecoratedText []){ this.setText(); Document doc = this.getStyledDocument(); if(obj instanceof DecoratedText){ DecoratedText decText =(DecoratedText)obj; try { doc.insertString(doc.getLength(),decText.getText(),decText.getAttributeSet(this)); } catch(BadLocationException ex){ Logger.getLogger(TextPaneTreeCellRenderer.class.getName())。log(Level.SEVERE,null,ex); } } if(obj instanceof DecoratedText []){ DecoratedText [] arrayDecText =(DecoratedText [])obj; for(DecoratedTextText decText:arrayDecText){ try { doc.insertString(doc.getLength(),decText.getText(),decText.getAttributeSet(this)); } catch(BadLocationException ex){ System.out.println(decText:+ decText); System.out.println(arrayDecText:+ arrayDecText); System.out.println(doc:+ doc.getLength()); Logger.getLogger(TextPaneTreeCellRenderer.class.getName())。log(Level.SEVERE,null,ex); } } } } else {返回新的DefaultTreeCellRenderer()。getTreeCellRendererComponent(树,值,叶子,展开,叶子,行, hasFocus); } } } 返回此; } }

解决方案

第一个类(TextPaneDefaultTreeCellRenderer)的问题是这个nullpointerexception使用nimbus LAF,...

即使使用 NimbusLook我的环境中的AndFeel (1.8.0_181,Windows 10) NullPointerException 不会发生 TextPaneDefaultTreeCellRenderer ...

文件夹被省略

在 JPanel中为图标布局 JTextPane 和 JLabel 是可行的并将其用作 TreeCellRenderer 。

import java.awt。*; import java.awt.event。*; import java.util。*; import java.util.logging。*; import javax.swing。*; import javax.swing.text。*; import javax.swing.tree。*; public class TextPaneTreeCellRendererTest { public Component makeUI(){ JTree tree0 = new JTree(makeModel()); // JTree tree1 = new JTree(makeModel()); // tree1.setCellRenderer(new TextPaneDefaultTreeCellRenderer()); // JTree tree2 = new JTree(makeModel()); // tree2.setCellRenderer(new TextPaneTreeCellRenderer()); // JTree tree3 = new JTree(makeModel()); // tree3.setCellRenderer(new TreeCellRendererTextPane()); JTree树=新JTree(makeModel()); tree.setRowHeight(24); tree.setCellRenderer(new CustomTreeCellRenderer()); JTabbedPane tabs = new JTabbedPane(); tabs.add(DefaultTreeCellRenderer,新的JScrollPane(tree0)); // tabs.add(TextPaneDefaultTreeCellRenderer,新的JScrollPane(tree1)); // tabs.add(TextPaneTreeCellRenderer,新的JScrollPane(tree2)); // tabs.add(TreeCellRendererTextPane,新的JScrollPane(tree3)); tabs.add(CustomTreeCellRenderer,新的JScrollPane(树)); 返回标签; } 私有静态TreeModel makeModel(){ DefaultMutableTreeNode root = new DefaultMutableTreeNode(/ Users /); DefaultMutableTreeNode parent; parent = new DefaultMutableTreeNode(< html> / Users /...< span style ='color:red'> colors:< span style ='color:blue'>蓝色); root.add(parent); String txt =< html>< span style ='color:red'> Line 6< / span>:< span style ='color:blue'> !!!!!!! !on< span style ='颜色:绿色'> CentOS< / span> 7< / span>; parent.add(new DefaultMutableTreeNode(txt)); parent.add(new DefaultMutableTreeNode(new DecoratedText(blue,Color.BLUE))); parent.add(new DefaultMutableTreeNode(new DecoratedText(pink,Color.PINK))); parent.add(new DefaultMutableTreeNode(new DecoratedText(red,Color.RED))); 返回新的DefaultTreeModel(root); } public static void main(String [] args){ EventQueue.invokeLater(() - > { try { UIManager.setLookAndFeel(javax .swing.plaf.nimbus.NimbusLookAndFeel); } catch(异常e){ e.printStackTrace(); } JFrame f = new JFrame(); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.getContentPane()。add(new TextPaneTreeCellRendererTest()。makeUI()); f.setSize(640,480); f.setLocationRelativeTo(null); f.setVisible(true); }); } } class DecoratedText { private String text; 私人彩色背景; // = new JLabel()。getBackground(); private Color foreground; // = new JLabel()。getForeground(); private Font字体; // = new JLabel()。getFont(); public DecoratedText(String text){ this.text = text; } public DecoratedText(String text,Font font){ this.text = text; this.font = font; } public DecoratedText(String text,Color foreground){ this.text = text; this.foreground = foreground; } public DecoratedText(String text,Color foreground,Font font){ this.text = text; this.foreground = foreground; this.font = font; } public DecoratedText(彩色背景,字符串文本){ this.background = background; this.text = text; } public DecoratedText(彩色背景,字符串文字,字体字体){ this.background = background; this.text = text; this.font = font; } public DecoratedText(彩色背景,字符串文字,彩色前景){ this.background = background; this.text = text; this.foreground = foreground; } public DecoratedText(彩色背景,字符串文字,彩色前景,字体字体){ this.background = background; this.text = text; this.foreground = foreground; this.font = font; } public String getText(){ return text; } public void setText(String text){ this.text = text; } public Color getBackground(){ return background; } public void setBackground(Color background){ this.background = background; } public Color getForeground(){ return foreground; } public void setForeground(Color foreground){ this.foreground = foreground; } public Font getFont(){ return font; } public void setFont(Font font){ this.font = font; } @Override public String toString(){ returnDecoratedText {+text =+ text +,background =+ background +,前景=+前景+,字体=+字体+}; } public SimpleAttributeSet getAttributeSet(JTextPane textPane){ SimpleAttributeSet attrSet = new SimpleAttributeSet(); if(getBackground()!= null){ StyleConstants.setBackground(attrSet,getBackground()); } else { StyleConstants.setBackground(attrSet,textPane.getBackground()); } if(getForeground()!= null){ StyleConstants.setForeground(attrSet,getForeground()); } else { StyleConstants.setForeground(attrSet,textPane.getForeground()); } 字体字体; if(getFont()!= null){ font = getFont(); } else { font = textPane.getFont(); } StyleConstants.setFontFamily(attrSet,font.getFamily()); StyleConstants.setItalic(attrSet,font.isItalic()); StyleConstants.setBold(attrSet,font.isBold()); StyleConstants.setFontSize(attrSet,font.getSize()); 返回attrSet; } } 类CustomTreeCellRenderer扩展JPanel实现TreeCellRenderer { private DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(); 私人决赛JLabel icon = new JLabel(); private final JTextPane textPane = new JTextPane(); protected CustomTreeCellRenderer(){ super(new BorderLayout()); textPane.setContentType(text / html); textPane.setEditable(false); textPane.setOpaque(false); icon.setOpaque(false); setOpaque(false); add(icon,BorderLayout.WEST); add(textPane); } @Override public Component getTreeCellRendererComponent( JTree tree,Object value,boolean selected,boolean expanded, boolean leaf,int row,boolean hasFocus){ JLabel l =(JLabel)renderer.getTreeCellRendererComponent( tree,value,selected,expanded,leaf,row,hasFocus); 颜色bgColor; 颜色fgColor; if(selected){ bgColor = renderer.getBackgroundSelectionColor(); fgColor = renderer.getTextSelectionColor(); } else { bgColor = Optional.ofNullable(renderer.getBackgroundNonSelectionColor()) .orElse(renderer.getBackground()); fgColor = Optional.ofNullable(renderer.getTextNonSelectionColor()) .orElse(renderer.getForeground()); } UIDefaults defaults = new UIDefaults(); defaults.put(TextPane [Enabled] .backgroundPainter,bgColor); textPane.putClientProperty(Nimbus.Overrides,默认值); textPane.putClientProperty(Nimbus.Overrides.InheritDefaults,true); textPane.setBackground(bgColor); textPane.setForeground(fgColor); DefaultMutableTreeNode node =(DefaultMutableTreeNode)value; Object object = node.getUserObject(); textPane.setText(); Document doc = textPane.getStyledDocument(); if(object instanceof DecoratedText){ DecoratedText decText =(DecoratedText)对象; try { doc.insertString(doc.getLength(),decText.getText(),decText.getAttributeSet(textPane)); } catch(BadLocationException ex){ ex.printStackTrace(); } } else if(object instanceof DecoratedText []){ DecoratedText [] arrayDecText =(DecoratedText [])object; for(DecoratedTextText decText:arrayDecText){ try { doc.insertString(doc.getLength(),decText.getText(),decText.getAttributeSet(textPane)); } catch(BadLocationException ex){ ex.printStackTrace(); } } } else { textPane.setText(Objects.toString(value)); } icon.setIcon(l.getIcon()); icon.setBorder(BorderFactory.createEmptyBorder(0,0,0,l.getIconTextGap())); 返回此; } @Override public void updateUI(){ super.updateUI(); renderer = new DefaultTreeCellRenderer(); } }

I have this class in order to show multiple colors in a leaf using JTree...

The class is TextPaneDefaultTreeCellRenderer

import java.awt.*; import java.util.*; import javax.swing.*; import javax.swing.text.*; import javax.swing.tree.*; public class TextPaneDefaultTreeCellRenderer extends DefaultTreeCellRenderer { TextPaneTreeCellRenderer textPaneScrollPane = new TextPaneTreeCellRenderer(); public TextPaneDefaultTreeCellRenderer() { initialize(); } private void initialize() { textPaneScrollPane.setBackgroundNonSelectionColor(getBackgroundNonSelectionColor()); textPaneScrollPane.setBackgroundSelectionColor(getBackgroundSelectionColor()); textPaneScrollPane.setTextNonSelectionColor(getTextNonSelectionColor()); textPaneScrollPane.setTextSelectionColor(getTextSelectionColor()); } @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { if (leaf) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; Object obj = node.getUserObject(); if (obj != null) { if (obj instanceof DecoratedText || obj instanceof DecoratedText[]) { if (textPaneScrollPane == null) { System.out.println("textPaneScrollPane:" + textPaneScrollPane); } return textPaneScrollPane.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); } } } return super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); } } class TextPaneTreeCellRenderer extends DefaultTreeCellRenderer { JTextPane textPane; public TextPaneTreeCellRenderer() { textPane = new JTextPane(); add(textPane); } @Override public void setBackgroundNonSelectionColor(Color color) { this.backgroundNonSelectionColor = color; } @Override public void setBackgroundSelectionColor(Color color) { this.backgroundSelectionColor = color; } @Override public void setTextNonSelectionColor(Color color) { this.textNonSelectionColor = color; } @Override public void setTextSelectionColor(Color color) { this.textSelectionColor = color; } @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; Object object = node.getUserObject(); if (selected) { setForeground(textSelectionColor); setBackground(backgroundSelectionColor); textPane.setForeground(textSelectionColor); textPane.setBackground(backgroundSelectionColor); } else { setForeground(textNonSelectionColor); setBackground(backgroundNonSelectionColor); textPane.setForeground(textNonSelectionColor); textPane.setBackground(backgroundNonSelectionColor); } textPane.setText(""); Document doc = textPane.getStyledDocument(); if (object != null && object instanceof DecoratedText) { DecoratedText decText = (DecoratedText) object; try { doc.insertString(doc.getLength(), decText.getText(), getAttributeSet(decText)); } catch (BadLocationException ex) { Logger.getLogger(TextPaneTreeCellRenderer.class.getName()).log(Level.SEVERE, null, ex); } } else if (object != null && object instanceof DecoratedText[]) { DecoratedText[] arrayDecText = (DecoratedText[]) object; for (DecoratedText decText : arrayDecText) { try { //doc.insertString(doc.getLength(), decText.getText(), getAttributeSet(decText)); doc.insertString(doc.getLength(), decText.getText(), decText.getAttributeSet(textPane)); } catch (BadLocationException ex) { System.out.println("decText:" + decText); System.out.println("arrayDecText:" + arrayDecText); System.out.println("doc:" + doc.getLength()); Logger.getLogger(TextPaneTreeCellRenderer.class.getName()).log(Level.SEVERE, null, ex); } } } else { return new DefaultTreeCellRenderer().getTreeCellRendererComponent(tree, value, leaf, expanded, leaf, row, hasFocus); } return textPane; } private SimpleAttributeSet getAttributeSet(DecoratedText decoratedText) { SimpleAttributeSet attrSet = new SimpleAttributeSet(); if (decoratedText != null) { if (decoratedText.getBackground() != null) { StyleConstants.setBackground(attrSet, decoratedText.getBackground()); } else { StyleConstants.setBackground(attrSet, textPane.getBackground()); } if (decoratedText.getForeground() != null) { StyleConstants.setForeground(attrSet, decoratedText.getForeground()); } else { StyleConstants.setForeground(attrSet, textPane.getForeground()); } Font font; if (decoratedText.getFont() != null) { font = decoratedText.getFont(); } else { font = textPane.getFont(); } StyleConstants.setFontFamily(attrSet, font.getFamily()); StyleConstants.setItalic(attrSet, font.isItalic()); StyleConstants.setBold(attrSet, font.isBold()); StyleConstants.setFontSize(attrSet, font.getSize()); } return attrSet; } }

Alternative class is TreeCellRendererTextPane

import java.awt.Component; import java.util.logging.*; import javax.swing.*; import javax.swing.text.*; import javax.swing.tree.*; public class TreeCellRendererTextPane extends JTextPane implements TreeCellRenderer { @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { if (leaf) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; Object obj = node.getUserObject(); if (obj != null) { if (obj instanceof DecoratedText || obj instanceof DecoratedText[]) { this.setText(""); Document doc = this.getStyledDocument(); if (obj instanceof DecoratedText) { DecoratedText decText = (DecoratedText) obj; try { doc.insertString(doc.getLength(), decText.getText(), decText.getAttributeSet(this)); } catch (BadLocationException ex) { Logger.getLogger(TextPaneTreeCellRenderer.class.getName()).log(Level.SEVERE, null, ex); } } if (obj instanceof DecoratedText[]) { DecoratedText[] arrayDecText = (DecoratedText[]) obj; for (DecoratedText decText : arrayDecText) { try { doc.insertString(doc.getLength(), decText.getText(), decText.getAttributeSet(this)); } catch (BadLocationException ex) { System.out.println("decText:" + decText); System.out.println("arrayDecText:" + arrayDecText); System.out.println("doc:" + doc.getLength()); Logger.getLogger(TextPaneTreeCellRenderer.class.getName()).log(Level.SEVERE, null, ex); } } } } else { return new DefaultTreeCellRenderer().getTreeCellRendererComponent(tree, value, leaf, expanded, leaf, row, hasFocus); } } } return this; } }

As you can see the Root (not leaf is not shown properly, the folder are omitted, the words only shown the first letter)

My other class is DecoratedText:

import java.awt.*; import javax.swing.JTextPane; import javax.swing.text.*; public class DecoratedText { private String text; private Color background;// = new JLabel().getBackground(); private Color foreground;// = new JLabel().getForeground(); private Font font;// = new JLabel().getFont(); public DecoratedText(String text) { this.text = text; } public DecoratedText(String text, Font font) { this.text = text; this.font = font; } public DecoratedText(String text, Color foreground) { this.text = text; this.foreground = foreground; } public DecoratedText(String text, Color foreground, Font font) { this.text = text; this.foreground = foreground; this.font = font; } public DecoratedText(Color background, String text) { this.background = background; this.text = text; } public DecoratedText(Color background, String text, Font font) { this.background = background; this.text = text; this.font = font; } public DecoratedText(Color background, String text, Color foreground) { this.background = background; this.text = text; this.foreground = foreground; } public DecoratedText(Color background, String text, Color foreground, Font font) { this.background = background; this.text = text; this.foreground = foreground; this.font = font; } public String getText() { return text; } public void setText(String text) { this.text = text; } public Color getBackground() { return background; } public void setBackground(Color background) { this.background = background; } public Color getForeground() { return foreground; } public void setForeground(Color foreground) { this.foreground = foreground; } public Font getFont() { return font; } public void setFont(Font font) { this.font = font; } @Override public String toString() { return "DecoratedText{" + "text=" + text + ", background=" + background + ", foreground=" + foreground + ", font=" + font + "}"; } public SimpleAttributeSet getAttributeSet(JTextPane textPane) { SimpleAttributeSet attrSet = new SimpleAttributeSet(); if (getBackground() != null) { StyleConstants.setBackground(attrSet, getBackground()); } else { StyleConstants.setBackground(attrSet, textPane.getBackground()); } if (getForeground() != null) { StyleConstants.setForeground(attrSet, getForeground()); } else { StyleConstants.setForeground(attrSet, textPane.getForeground()); } Font font; if (getFont() != null) { font = getFont(); } else { font = textPane.getFont(); } StyleConstants.setFontFamily(attrSet, font.getFamily()); StyleConstants.setItalic(attrSet, font.isItalic()); StyleConstants.setBold(attrSet, font.isBold()); StyleConstants.setFontSize(attrSet, font.getSize()); return attrSet; } }

The problem with the first Class (TextPaneDefaultTreeCellRenderer) is this nullpointerexception using nimbus LAF, but with another LAF, aqua, metal, motif, gtk and windows the problem is not presented.

And Trying to do Divide and Conquer, this question is not about what fails? else is my class fine implemented?

Discover Reason of NullPointerException in Java Native Classes, SynthTreeUI using LAF Nimbus

解决方案

The problem with the first Class (TextPaneDefaultTreeCellRenderer) is this nullpointerexception using nimbus LAF, ...

Even when using NimbusLookAndFeel in my environment(1.8.0_181, Windows 10) NullPointerException does not occur for the TextPaneDefaultTreeCellRenderer...

the folder are omitted

It would be works fine to layout JTextPane and JLabel for icon in JPanel and use this as TreeCellRenderer.

import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.logging.*; import javax.swing.*; import javax.swing.text.*; import javax.swing.tree.*; public class TextPaneTreeCellRendererTest { public Component makeUI() { JTree tree0 = new JTree(makeModel()); // JTree tree1 = new JTree(makeModel()); // tree1.setCellRenderer(new TextPaneDefaultTreeCellRenderer()); // JTree tree2 = new JTree(makeModel()); // tree2.setCellRenderer(new TextPaneTreeCellRenderer()); // JTree tree3 = new JTree(makeModel()); // tree3.setCellRenderer(new TreeCellRendererTextPane()); JTree tree = new JTree(makeModel()); tree.setRowHeight(24); tree.setCellRenderer(new CustomTreeCellRenderer()); JTabbedPane tabs = new JTabbedPane(); tabs.add("DefaultTreeCellRenderer", new JScrollPane(tree0)); // tabs.add("TextPaneDefaultTreeCellRenderer", new JScrollPane(tree1)); // tabs.add("TextPaneTreeCellRenderer", new JScrollPane(tree2)); // tabs.add("TreeCellRendererTextPane", new JScrollPane(tree3)); tabs.add("CustomTreeCellRenderer", new JScrollPane(tree)); return tabs; } private static TreeModel makeModel() { DefaultMutableTreeNode root = new DefaultMutableTreeNode("/Users/"); DefaultMutableTreeNode parent; parent = new DefaultMutableTreeNode("<html>/Users/...<span style='color:red'>colors: <span style='color:blue'>blue"); root.add(parent); String txt = "<html><span style='color:red'>Line 6</span> : <span style='color:blue'>!!!!!!!! on <span style='Color:green'>CentOS</span> 7</span>"; parent.add(new DefaultMutableTreeNode(txt)); parent.add(new DefaultMutableTreeNode(new DecoratedText("blue", Color.BLUE))); parent.add(new DefaultMutableTreeNode(new DecoratedText("pink", Color.PINK))); parent.add(new DefaultMutableTreeNode(new DecoratedText("red", Color.RED))); return new DefaultTreeModel(root); } public static void main(String[] args) { EventQueue.invokeLater(() -> { try { UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } JFrame f = new JFrame(); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.getContentPane().add(new TextPaneTreeCellRendererTest().makeUI()); f.setSize(640, 480); f.setLocationRelativeTo(null); f.setVisible(true); }); } } class DecoratedText { private String text; private Color background; // = new JLabel().getBackground(); private Color foreground; // = new JLabel().getForeground(); private Font font; // = new JLabel().getFont(); public DecoratedText(String text) { this.text = text; } public DecoratedText(String text, Font font) { this.text = text; this.font = font; } public DecoratedText(String text, Color foreground) { this.text = text; this.foreground = foreground; } public DecoratedText(String text, Color foreground, Font font) { this.text = text; this.foreground = foreground; this.font = font; } public DecoratedText(Color background, String text) { this.background = background; this.text = text; } public DecoratedText(Color background, String text, Font font) { this.background = background; this.text = text; this.font = font; } public DecoratedText(Color background, String text, Color foreground) { this.background = background; this.text = text; this.foreground = foreground; } public DecoratedText(Color background, String text, Color foreground, Font font) { this.background = background; this.text = text; this.foreground = foreground; this.font = font; } public String getText() { return text; } public void setText(String text) { this.text = text; } public Color getBackground() { return background; } public void setBackground(Color background) { this.background = background; } public Color getForeground() { return foreground; } public void setForeground(Color foreground) { this.foreground = foreground; } public Font getFont() { return font; } public void setFont(Font font) { this.font = font; } @Override public String toString() { return "DecoratedText{" + "text=" + text + ", background=" + background + ", foreground=" + foreground + ", font=" + font + "}"; } public SimpleAttributeSet getAttributeSet(JTextPane textPane) { SimpleAttributeSet attrSet = new SimpleAttributeSet(); if (getBackground() != null) { StyleConstants.setBackground(attrSet, getBackground()); } else { StyleConstants.setBackground(attrSet, textPane.getBackground()); } if (getForeground() != null) { StyleConstants.setForeground(attrSet, getForeground()); } else { StyleConstants.setForeground(attrSet, textPane.getForeground()); } Font font; if (getFont() != null) { font = getFont(); } else { font = textPane.getFont(); } StyleConstants.setFontFamily(attrSet, font.getFamily()); StyleConstants.setItalic(attrSet, font.isItalic()); StyleConstants.setBold(attrSet, font.isBold()); StyleConstants.setFontSize(attrSet, font.getSize()); return attrSet; } } class CustomTreeCellRenderer extends JPanel implements TreeCellRenderer { private DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(); private final JLabel icon = new JLabel(); private final JTextPane textPane = new JTextPane(); protected CustomTreeCellRenderer() { super(new BorderLayout()); textPane.setContentType("text/html"); textPane.setEditable(false); textPane.setOpaque(false); icon.setOpaque(false); setOpaque(false); add(icon, BorderLayout.WEST); add(textPane); } @Override public Component getTreeCellRendererComponent( JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { JLabel l = (JLabel) renderer.getTreeCellRendererComponent( tree, value, selected, expanded, leaf, row, hasFocus); Color bgColor; Color fgColor; if (selected) { bgColor = renderer.getBackgroundSelectionColor(); fgColor = renderer.getTextSelectionColor(); } else { bgColor = Optional.ofNullable(renderer.getBackgroundNonSelectionColor()) .orElse(renderer.getBackground()); fgColor = Optional.ofNullable(renderer.getTextNonSelectionColor()) .orElse(renderer.getForeground()); } UIDefaults defaults = new UIDefaults(); defaults.put("TextPane[Enabled].backgroundPainter", bgColor); textPane.putClientProperty("Nimbus.Overrides", defaults); textPane.putClientProperty("Nimbus.Overrides.InheritDefaults", true); textPane.setBackground(bgColor); textPane.setForeground(fgColor); DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; Object object = node.getUserObject(); textPane.setText(""); Document doc = textPane.getStyledDocument(); if (object instanceof DecoratedText) { DecoratedText decText = (DecoratedText) object; try { doc.insertString(doc.getLength(), decText.getText(), decText.getAttributeSet(textPane)); } catch (BadLocationException ex) { ex.printStackTrace(); } } else if (object instanceof DecoratedText[]) { DecoratedText[] arrayDecText = (DecoratedText[]) object; for (DecoratedText decText : arrayDecText) { try { doc.insertString(doc.getLength(), decText.getText(), decText.getAttributeSet(textPane)); } catch (BadLocationException ex) { ex.printStackTrace(); } } } else { textPane.setText(Objects.toString(value)); } icon.setIcon(l.getIcon()); icon.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, l.getIconTextGap())); return this; } @Override public void updateUI() { super.updateUI(); renderer = new DefaultTreeCellRenderer(); } }

更多推荐

正确的方式使用JTextPane创建Class,JTree,DefaultTreeCellRenderer

本文发布于:2023-10-28 04:32:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535529.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   方式   JTextPane   DefaultTreeCellRenderer   JTree

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!