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