Results 1 to 6 of 6
  1. #1
    kmm1977 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Default JTree rendering problem

    Hallo
    +
    my aim to set different fonts on different nodes of a tree , and therefore i need a renderer for each node. I wrote the following renderer. now i am not sure where exactly or how i should call this renderer ?

    I wrote a renderer t


    public class TreeCellBoldRenderer extends DefaultTreeCellRenderer {

    /**
    *
    */
    private static final long serialVersionUID = 1L;

    public Component getTreeCellRendererComponent(JTree tree, Object obj,
    boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {

    Font f = this.getFont();
    f.deriveFont(Font.BOLD);
    this.setFont(f);
    //setText("Node Name");

    return this;
    }
    }
    Regards

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    You set it to your JTree:
    JTree (Java Platform SE 6)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    kmm1977 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Default jtree problem

    I did that and it paints all the nodes with the font ?
    But i wanted only specific nodes i.e ( refer code below )
    i want only the red nodes bold


    Java Code:
    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) {			
    			
    			paintNodes(myNode);
    		}	
    		
    		return this;
    	}
    
    	private void paintNodes( MyTreeNode node)
    	{
    		if(nodeNotFound(node)){
    			setForeground(Color.RED); 
    	 		navigator.navigatorTree.setCellRenderer(new TreeCellBoldRenderer());
    	
    		}else{				
    			setForeground(new Color(1,125,3)); 
    		}		
    		navigator.navigatorTree.repaint();
    	}

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Don't set a cell renderer in a get...RendererComponent. Set the font and foreground instead.

    To get better help sooner, post a SSCCE : Java Glossary .

    db

  5. #5
    kmm1977 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Default jtree problem

    ok i managed to make runnable code

    what i want is to distinguish each node with different fonts or atleast make some of them BOLD

    how should i extend this code ?

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Font;
     
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultTreeCellRenderer;
    
    
    
    public class TreeNode {
    	
      public static void main(String args[]) {
        JFrame frame = new JFrame("Changed Renderer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTree tree = new JTree();
        DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
        
        int rowHeight = tree.getRowHeight();
        if (rowHeight <= 0) {
          tree.setRowHeight(rowHeight - 1);
        }
     
        JScrollPane scrollPane = new JScrollPane(tree);
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.setSize(300, 150);
        frame.setVisible(true);
      }
    }

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

Similar Threads

  1. Problem in JSP rendering
    By srkumarj2ee@gmail.com in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2009, 03:02 AM
  2. Mbox File and JTree Problem.
    By dracuswolf in forum AWT / Swing
    Replies: 1
    Last Post: 07-26-2009, 06:17 AM
  3. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 12:59 PM
  4. JLabel Rendering
    By random4534 in forum New To Java
    Replies: 3
    Last Post: 12-16-2008, 09:55 AM
  5. JTree Problem
    By ngpamela in forum XML
    Replies: 0
    Last Post: 12-10-2008, 05:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •