JTree+JCheckBox.树节点是复选框,初始字体是默认,在选中后变为Bold。但这样JCheckBox的文字变宽,就会被裁掉一部分显示成省略号。在选中时调整继承了JCheckBox的TreeCellRenderer的size(setsize)没有作用。如何调整是的文字不被裁掉?多谢

解决方案 »

  1.   

    import java.awt.*;import javax.swing.*;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import javax.swing.tree.TreeCellRenderer;public class TreeCellRendererTest {
        public static void main(String[] args) {
            JTree tree = new JTree();
            tree.setCellRenderer(new TestRenderer());
            JScrollPane sp = new JScrollPane(tree);
            JFrame f = new JFrame();
            f.getContentPane().add(sp, BorderLayout.CENTER);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(800, 600);
            f.setLocationRelativeTo(null);
            f.show();
        }    private static class TestRenderer extends DefaultTreeCellRenderer 
            implements TreeCellRenderer {
            private Font boldFont = null;
            private Font font = null;
            
            public Component getTreeCellRendererComponent(
                    JTree tree, Object value, boolean selected, 
                    boolean expanded, boolean leaf, int row, boolean hasFocus) {            super.getTreeCellRendererComponent(
                        tree, value, selected, expanded, leaf, row, hasFocus);            if (font == null) {
                    font = getFont().deriveFont(Font.PLAIN);
                    boldFont = getFont().deriveFont(Font.BOLD);
                }
                if (selected == true) {
                    setFont(boldFont);
                }
                else {
                    setFont(font);
                }
                return this;
            }        public Dimension getPreferredSize() {
                Font oldFont = getFont();
                setFont(oldFont.deriveFont(Font.BOLD));
                Dimension d = super.getPreferredSize();
                setFont(oldFont);
                return d;
            }
        }
    }
      

  2.   


    搂主,JTree+JCheckBox的方法代码,能否贴出来共享一下
      

  3.   

    public class CheckBoxRenderer extends JCheckBox implements TreeCellRenderer { private Map fontsMap = new HashMap(); private Map colorsMap = new HashMap(); private final static String FONT_BOLD = "boldfont"; private final static String FONT_ITALIC = "italicfont"; private final static String FONT_DEFAULT = "plainfont"; private final static String COLOR_RED = "redcolor"; private final static String COLOR_BLACK = "blackcolor"; private final static String COLOR_BLUE = "bluecolor"; private final static String COLOR_PARTIALSELECTED = "graycolor"; private void loadColors() {
    colorsMap.put(CheckRenderer.COLOR_BLACK, Color.BLACK);
    colorsMap.put(CheckRenderer.COLOR_RED, Color.RED);
    colorsMap.put(CheckRenderer.COLOR_BLUE, Color.BLUE);
    colorsMap.put(CheckRenderer.COLOR_PARTIALSELECTED, new Color(0,0,160));
    } private void loadFonts() { fontsMap.put(CheckRenderer.FONT_DEFAULT, new Font("Arial", Font.PLAIN,
    11));
    fontsMap.put(CheckRenderer.FONT_BOLD, new Font("Arial", Font.BOLD, 11)); } public CheckRenderer() {
    loadFonts();
    loadColors();
    } public Component getTreeCellRendererComponent(JTree tree, Object value,
    boolean selected, boolean expanded, boolean leaf, int row,
    boolean hasFocus) { String stringValue = tree.convertValueToText(value, selected, expanded,
    leaf, row, hasFocus); PlanTreeNode node = (PlanTreeNode) value;

    if (hasFocus) {
    this.setBackground(UIManager.getColor("Tree.selectionBackground"));
    setFont((Font) fontsMap.get(CheckRenderer.FONT_BOLD));
    } else {
    this.setBackground(tree.getBackground());
    setFont((Font) fontsMap.get(CheckRenderer.FONT_DEFAULT));
    }

    return this;
    }


    public Dimension getPreferredSize(){
        Font oldFont = getFont();
            setFont((Font)fontsMap.get(FONT_BOLD));
            Dimension d = super.getPreferredSize();
            setFont(oldFont);        
            return d;
    }

    }