Article View: borland.public.jbuilder.java.swing
Article #12531Re: Advanced JTree (with images and checkboxes)
From: Tad Frysinger
Date: Fri, 18 Dec 1998 00:00
Date: Fri, 18 Dec 1998 00:00
133 lines
5658 bytes
5658 bytes
In article <3677DB2E.F23F0D89@topsoft.minsk.by>, rds@topsoft.minsk.by
says...
> Hi!
> I need swing tree with more capabilities :
> different images for every node and
> checkboxes to select nodes.
> I think that it is necessary to overload rendering
> method.
>
> Maybe somebody has ready pieces of source code
> or any advices.
>
> Thanx in advance
> rds@topsoft.minsk.by
>
>
Here is an example of a renderer that dynamically decides whether it
should render a checkbox, or the default behavior. 'ETNode' is my own
class that simply holds values that the node needs. I also included a
custom paint, because by default in Swing, the checkbox doesn't let you
know when it has focus - on a tree this can be somewhat disconcerting:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.plaf.basic.*;
import com.objectspace.jgl.*;
import com.objectspace.jgl.predicates.*;
import com.objectspace.jgl.algorithms.*;
import com.objectspace.jgl.adapters.*;
class CheckBoxRenderer extends JCheckBox
implements TreeCellRenderer
{
public void CheckBoxRenderer()
{
}
public Component getTreeCellRendererComponent(JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf, int row,
boolean hasFocus)
{
ETNode node =
(ETNode)((DefaultMutableTreeNode)value).getUserObject();
if ( !node.isCheckable() )
{
return defaultRenderer.getTreeCellRendererComponent( tree,
node.getText(),
sel,
expanded,
leaf, row,
hasFocus);
}
focus = hasFocus;
setEnabled(tree.isEnabled());
setBackground( tree.getBackground() );
setForeground( tree.getForeground() );
setSelected( node.isSelected() );
setText( node.getText() );
return this;
}
public String getToolTipText()
{
return "";
}
/**
Paint a dotted line around the component, if it has focus. There is
a generic routine to do this in BasicGraphicsUtils. It requires you
have the appropriate rectangle for the graphics object you pass.
We use another generic routine from SwingUtils that does, among
other things, filling in the rectangle for the text portion of the
checkbox with the appropriate values we can use to draw the dotted
line to illustrate focus.
*/
public void paint(Graphics g)
{
super.paint( g );
if ( focus )
{
Rectangle viewRect = new Rectangle();
Rectangle textRect = new Rectangle();
Rectangle iconRect = new Rectangle();
Dimension size = new Dimension();
size = getSize();
viewRect.x = viewRect.y = 0;
viewRect.width = size.width;
viewRect.height = size.height;
iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
textRect.x = textRect.y = textRect.width = textRect.height = 0;
FontMetrics fm = g.getFontMetrics();
// This is the parent for the UI for any particular L&F
// for this component type.
BasicRadioButtonUI ui = (BasicRadioButtonUI)UIManager.getUI( this );
int icongap = ui.getDefaultTextIconGap(this);
Icon altIcon = getIcon();
if ( altIcon == null )
altIcon = ui.getDefaultIcon();
// Determine the textRect values so we know where to draw the
// dashed line
SwingUtilities.layoutCompoundLabel( this,
fm,
getText(),
altIcon,
getVerticalAlignment(),
getHorizontalAlignment(),
getVerticalTextPosition(),
getHorizontalTextPosition(),
viewRect,
iconRect,
textRect,
icongap );
BasicGraphicsUtils.drawDashedRect(g,
textRect.x,
textRect.y,
textRect.width,
textRect.height);
}
}
private DefaultTreeCellRenderer defaultRenderer = new
DefaultTreeCellRenderer();
private boolean focus = false;
}
Tad Frysinger
[TeamB]
Technical Details
Message-ID:
<MPG.10e43be7a10f8f30989965@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3677DB2E.F23F0D89@topsoft.minsk.by>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,37b42eec8a6ce857 X-Google-Attributes: gid111d45,public From: tfrysinger@earthlink.net (Tad Frysinger) Subject: Re: Advanced JTree (with images and checkboxes) Date: 1998/12/18 Message-ID: <MPG.10e43be7a10f8f30989965@forums.borland.com> X-Deja-AN: 423664620 References: <3677DB2E.F23F0D89@topsoft.minsk.by> Organization: Freezone Enterprises Newsgroups: borland.public.jbuilder.java.swing