Need Help get Text From group
Closed this issue · 1 comments
ResnuKrestio commented
Hi,
I have a group contains 2 Text() dan 1 Rectangle().
How to get the text from the group so i can modify the text.
I'm trying to use for(IPrimitive<?> text : group.asGroup().getChildNodes()) but i don't know what should i do next.
Please Help me, thanks before.
Regards
SprocketNYC commented
You can do it two ways, find by ID, or find by Predicate
import com.ait.tooling.common.api.java.util.function.Predicate;
final Group group = new Group();
final Text text_1 = new Text("One").setID("text_1");
final Text text_2 = new Text("Two").setID("text_2");
group.add(text_1);
group.add(text_2);
for (Node<?> node : group.findByID("text_1"))
{
final Shape<?> shape = node.asShape();
if (null != shape)
{
if (shape.getShapeType().equals(ShapeType.TEXT))
{
final Text text = ((Text) shape);
Console.get().info(text.toJSONString());
}
}
}
final Predicate<Node<?>> match = new Predicate<Node<?>>()
{
@Override
public boolean test(Node<?> node)
{
final Shape<?> shape = node.asShape();
if (null != shape)
{
if (shape.getShapeType().equals(ShapeType.TEXT))
{
return true;
}
}
return false;
}
};
for (Node<?> node : group.find(match))
{
final Text text = ((Text) node);
Console.get().info(text.toJSONString());
}
Predicate is much more flexible.You can do any kind of logic and testing in there.
This works from any Container, Viewport, Layer, or Group.