Connecting lines in JTree
Hi,
I have a JTree with each node having bigger icons, and bigger font.
So the connecting lines are kind of misplaced
for example the vertical line that connect the root node to it's children doesn't come from the center of the root node rather it is placed leftwards from the root node center.
Is it possible to shift the connecting lines in a JTree?
Thanks
-desiguy
[413 byte] By [
desiguya] at [2007-10-3 5:53:17]

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.tree.*;
public class TreeLines {
private Icon[] getScaledIcons(Component c) {
String[] iconIds = { "closed", "collapsed", "expanded", "leaf", "open" };
Icon[] icons = new Icon[iconIds.length];
for(int j = 0; j < icons.length; j++) {
icons[j] = UIManager.getIcon("Tree." + iconIds[j] + "Icon");
}
ImageIcon[] imageIcons = new ImageIcon[icons.length];
double scale = 1.25;
for(int j = 0; j < icons.length; j++) {
int w = (int)(scale * icons[j].getIconWidth());
int h = (int)(scale * icons[j].getIconHeight());
int type = BufferedImage.TYPE_INT_ARGB_PRE;
BufferedImage image = new BufferedImage(w, h, type);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
g2.setTransform(at);
icons[j].paintIcon(c, g2, 0, 0);
g2.dispose();
imageIcons[j] = new ImageIcon(image);
}
//JOptionPane.showMessageDialog(null, imageIcons, "", JOptionPane.PLAIN_MESSAGE);
//JOptionPane.showMessageDialog(null, icons, "", JOptionPane.PLAIN_MESSAGE);
return imageIcons;
}
private void expandTree(JTree tree)
{
DefaultMutableTreeNode root = (DefaultMutableTreeNode)tree.getModel().getRoot();
java.util.Enumeration e = root.breadthFirstEnumeration();
while(e.hasMoreElements())
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.nextElement();
if(node.isLeaf()) break;
int row = tree.getRowForPath(new TreePath(node.getPath()));
tree.expandRow(row);
}
}
private JScrollPane getContent() {
JTree tree = new JTree();
expandTree(tree);
return new JScrollPane(tree);
}
public static void main(String[] args) {
TreeLines treeLines = new TreeLines();
Icon[] icons = treeLines.launchDialog();
UIManager.put("Tree.closedIcon",new IconUIResource(icons[0]));
UIManager.put("Tree.collapsedIcon", new IconUIResource(icons[1]));
UIManager.put("Tree.expandedIcon", new IconUIResource(icons[2]));
UIManager.put("Tree.leafIcon",new IconUIResource(icons[3]));
UIManager.put("Tree.openIcon",new IconUIResource(icons[4]));
UIManager.put("Tree.font", new FontUIResource(
new Font("Dialog", Font.PLAIN, 18)));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(treeLines.getContent());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
/** To get and scale some images to use. */
private Icon[] launchDialog() {
JTree tree = new JTree();
JDialog d = new JDialog(new JFrame(), false);
d.getContentPane().add(tree);
d.pack();
Icon[] icons = getScaledIcons(tree);
d.dispose();
return icons;
}
}