Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2004

Tip: Looking for answers? Try searching our database.

Changing Font in JTree: Width too short causes '...' as part of node.

Thread view: 
Rob Jones - 18 Mar 2004 12:23 GMT
Hi again everyone.

Another problem with a JTree and custom fonts.
I want to highlight the selected node and path
by making the font for those items bold.
Code below (self contained compiling example) does this.
But....
Once you select a node (open colors, click on blue)
The 'blue' becomes 'bl...'
This is normal behaviour for a JLabel (which the
nodes are based on) when it doesn't have enough
space. Except that here there is enough space but
the JLabel obviously thinks there isnt.

Any and all ideas gratefully appreciated

Thanks

Rob

import java.awt.*;
import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;

public class TreeTest2 extends javax.swing.JFrame
{
    public class StructureTreeCellRenderer extends DefaultTreeCellRenderer
    {

        Font treeFont = null;
        Font treeFontBold = null;

        public StructureTreeCellRenderer()
        {
        }

        //Sets the value of the current tree cell to value.
        public Component getTreeCellRendererComponent(JTree tree,
Object value, boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus)
        {
            if (expanded || selected)
                setFont(treeFontBold);
            else
                setFont(treeFont);
            super.getTreeCellRendererComponent(tree, value, selected,
expanded, leaf, row, hasFocus);
            setIcon(null);
            return this;
        }

        public void setBaseFont(Font baseFont)
        {
            //bold is causing ....
            setFont(baseFont);
            treeFont = baseFont;
            treeFontBold = baseFont.deriveFont( Font.BOLD );
        }
    }

    private JTree myTree;

    public TreeTest2()
    {
        try
        {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e){ System.out.println("No system laf");}
        myTree = new JTree();
        myTree.setRowHeight(0);
        StructureTreeCellRenderer renderer = new
StructureTreeCellRenderer();
        renderer.setBaseFont(new Font("Dialog",0,16));
        myTree.setCellRenderer(renderer);
        this.getContentPane().add(myTree);
        this.setSize(300,200);
    }

    public static void main(String[] args)
    {
        TreeTest2 inst = new TreeTest2();
        inst.setVisible(true);
    }

}
Manish Hatwalne - 18 Mar 2004 16:23 GMT
I am also facing this problem with JList. Dunno how can I solve it!!!

- Manish

> Hi again everyone.
>
[quoted text clipped - 83 lines]
>
> }
Babu Kalakrishnan - 18 Mar 2004 16:39 GMT
> Hi again everyone.
>
[quoted text clipped - 9 lines]
> space. Except that here there is enough space but
> the JLabel obviously thinks there isnt.

Happens because the Tree doesn't realize that the size of the node (I
mean the size needed to display it) changes with the selection state.
(Bold characters generally use up more space than plain ones).

The best way to overcome this is to make the model fire a NodeChanged
event whenever a new node is selected. For example :

>         renderer.setBaseFont(new Font("Dialog",0,16));
>         myTree.setCellRenderer(renderer);

        myTree.addTreeSelectionListener(new
            TreeSelectionListener()
        {
            public void valueChanged(TreeSelectionEvent e)
            {
                TreeNode node =
TreeNode)e.getNewLeadSelectionPath().getLastPathComponent();
                ((DefaultTreeModel)myTree.getModel()).nodeChanged(node);
            }
        });

>         this.getContentPane().add(myTree);

should fix the problem. (Sorry if the formatting is screwed up)
Also change the references to the DefaultTreeModel and TreeNodes to
appropriate types in case you're using custom models / custom nodes.

Actually another nodeChanged event needs to be sent for the node which
was deseleced as well so that the size allocated to that reduces also.

BK
Rob Jones - 18 Mar 2004 17:59 GMT
>> Once you select a node (open colors, click on blue)
>> The 'blue' becomes 'bl...'
[quoted text clipped - 11 lines]
>
> should fix the problem. (Sorry if the formatting is screwed up)
Yep. Fixes it.
Cool.
Thanks.

Rob


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.