# How to set indent spacing of a tree in libGDX scene2d?

I've been trying to create a customized UI using scene2d library in libGDX. However I am having troubles as I'm trying to set a custom indent spacing on my Tree object.

This is the current situation which I am having.

This is how I would prefer it to look like.

All I want is that I could be able to change the size of the margin which is caused by nodes being on different levels. How can I achieve this?

As the nodes are just Actors, you could place you node content (the labels and sliders) first in some container like Table or Container and give the actual content a bit of margin in that wrapper container.

After you explanation about what you want to achieve the above won't work as you want to move the plus and minus buttons that are used to expand and collapse the Tree's Tree.Nodes.

I don't think the Scene2D Tree implementation will allow you to set the indentation level to be anything other than the max of the width of the plus and the minus Drawables. But there are always ways around that. The bad news is that the field you need to alter is a private field so simply inheriting Tree won't allow you to fix it, but if you're willing to get a bit dirty you can achieve what you're looking for using some reflection.

Instead of doing something like this

Tree tree = new Tree(skin);

// Add nodes to tree here


You could do this;

// First create an instance of Tree where the setStyle method is overridden
tree = new Tree(skin) {
@Override
public void setStyle(TreeStyle style) {
super.setStyle(style);

// After having called the base class's setStyle,
// use horrible reflection to find and alter the indentSpacing field.
try {
Field field = Tree.class.getDeclaredField("indentSpacing");
field.setAccessible(true);
field.set(this, 8); // This is how much you want each plus and minus indented.
// Normally this is set to something like
//     Math.max(style.plus.getMinWidth(), style.minus.getMinWidth())
System.out.println(field);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
};

// You will likely want to play around with this to make sure the node
// content is not rendered too close to the plus/minus
tree.setIconSpacing(4, 4);


This is obviously not ideal, but will likely be easier than creating a new version of Tree and get the pull request accepted. Only good thing about this approach is that it involves almost no typing for you.
Alternatively you can just copy the Tree code and change it accordingly.
• Thanks for your answer. I've done some testing but it seems like this is a little bit trickier problem. I think the Tree creates it's own containers for every node, so it's impossible to "reach out" those boundaries without accessing and changing the way Tree creates and sets those node boundaries. – Zigur Dec 14 '15 at 16:47