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]
# 1
Customize your TreeUI.
hiwaa at 2007-7-15 0:33:40 > top of Java-index,Desktop,Core GUI APIs...
# 2

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;

}

}

74philipa at 2007-7-15 0:33:40 > top of Java-index,Desktop,Core GUI APIs...
# 3
Thank you hiwa.I took a look at paint method of BasicTreeUI, but I think I need to put some more time to figure out what to do.If you have any specific tips, please pass on.Thanksdesiguy
desiguya at 2007-7-15 0:33:40 > top of Java-index,Desktop,Core GUI APIs...
# 4

Thank you Philip for putting efforts.

I tried this piece of code, but it just enlarges the node icons.

I don't need to make the node icons larger or smaller, I want the connecting lines to be proper oriented.

To see what I mean, assign a higher value(say 5.25) to scale in getScaledIcons method.

You will get the icons bigger, but the vertical connecting lines remains at same place.

Please let me know if I missed any point you wanted to convey.

Thanks

desiguy

desiguya at 2007-7-15 0:33:40 > top of Java-index,Desktop,Core GUI APIs...
# 5
Hi, try the following code:JTree yourTree = new JTree();int yourSpace = 20;BasicTreeUI treeUI = (BasicTreeUI) yourTree.getUI();treeUI.setLeftChildIndent(yourSpace);Ciao
tbox98a at 2007-7-15 0:33:40 > top of Java-index,Desktop,Core GUI APIs...