JGoTextNode and icon

I have a JGoTextNode. It has a icon and label. I want to update both
dynamically after they are painted on the canvas. Is this possible?


Sure. Here’s how you change the text of a node:
JGoTextNode n = …;
n.setText(“some new text”);
The value of getLabel() is actually the JGoText object that displays the text, so you can just set any of its properties, including font-related properties, color etc.
I’m not sure what you mean by changing the icon. JGoTextNode doesn’t have an image (implemented by JGoImage) by default, but it does have a Background object that you could modify or replace. If the Background object is a JGoDrawable, as it is by default, you can just set its brush or pen:
n.setBrush(new JGoBrush(Color.blue));
However, if you really wanted to have an image as the background instead of a rectangle, you could either use the JGoIconicNode class (which just has one port instead of the four that JGoTextNode has), or you could replace the JGoTextNode’s background with a JGoImage:
JGoImage img = new JGoImage();
img.setSize(…, …);
n.setAutoResizes(false); // don’t change size of image as text changes
By setting JGoTextNode.setAutoResizes(false), the text will be truncated to fit inside the Background object, in this case your JGoImage that you sized the way you wanted.

If you wanted just a single port on the node, instead of four ports with one at the middle of each side, you could use the JGoIconicNode. Here’s how you modify its image:
JGoIconicNode n = …;
n.getImage().loadImage(YourApp.class.getResource(“new_image. png”));

Hi Walter,
Thank you for your quick response. I am trying to change the
background on a JGoTextNode. The JGoTextNode is already
painted on the doc. Based on an event, I want to update the
background on the JGoTextNode. I tried
JGoImage img = new JGoImage();
img.setSize(…, …);
n.setAutoResizes(false); // don’t change size of image as text changes
This does not update the background. Is there a repaint that I need
to call?

Just wanted to add that I tried
view.repaint() and doc.redo() but it still does not update the JGoTextNode background.
Please advise.

You shouldn’t have to worry about painting at all. You just need to create the JGoObjects that you need and add them to your JGoDocument. All JGoViews that are displaying that JGoDocument will be automatically kept up-to-date as changes are made to your document objects.
Are you finding that changing the text does update automatically?
You couldn’t have used the code I presented, because it needed to be customized for your application. What code did you try?

node is JGoTextNode
When I try to update the background dynamically, I do:
JGoImage img = new JGoImage();
Image image = getSpecialImage();
img.loadImage(image, true);
img.setSize(32, 32);

OK, I just tried it, and it worked for me:
JGoObject obj = myView.getSelection().getPrimarySelection();
if (obj != null && obj instanceof JGoTextNode) {
JGoTextNode node = (JGoTextNode)obj;
//node.setAutoResize(false); …assumed to be already initialized this way
JGoImage img = new JGoImage();
img.loadImage(Demo1.class.getResource(“doc.gif”), true);
I executed this code in a keystroke event handler. (I also changed the code to set the size and position of the new image to be the same as the old image, but of course that doesn’t affect whether it works or not.)

I tried it again. The only difference is that I am clicking on the
node, node is highlighted and now I want to update the node icon.
Here is my node class

public class MyNode extends JGotTextNode{
private JGoObject nodeObject;
public MyNode(String name) {
nodeObject = getNodeObject();
protected JGoObject getNodeObject() {
JGoImage img = new JGoImage();
Image image = blah…;
if (image != null) {
img.loadImage(image, true);
img.setSize(32, 32);
return img;
public void layoutChildren(JGoObject childchanged) {
if (isInitializing())
JGoText label = getLabel();
if (label == null)
JGoObject back = getBackground();
if (back != null) {
Insets insets = getInsets();
if (isAutoResize()) {
back.setBoundingRect(label.getLeft() - insets.left, label
- insets.top, label.getWidth() + insets.left
+ insets.right, label.getHeight() + insets.top
+ insets.bottom);
} else {
int maxLabelWidth = Math.max(back.getWidth()
- (insets.left + insets.right), 0);
int maxLabelHeight = Math.max(back.getHeight()
- (insets.top + insets.bottom), 0);
// label.recalcBoundingRect();
int labelHeight = Math.min(label.getHeight(), maxLabelHeight);
int labelLeft = back.getLeft() + insets.left;
int labelTop = back.getTop() + insets.top
+ (maxLabelHeight - labelHeight) / 2;
label.setBoundingRect(labelLeft, labelTop, maxLabelWidth,
labelHeight + 10);
if (nodeObject != null)
label.setSpotLocation(TopCenter, nodeObject, BottomCenter);
if (getRightPort() != null)
getRightPort().setSpotLocation(TopLeft, back, TopRight);
if (getBottomPort() != null)
getBottomPort().setSpotLocation(TopCenter, back, BottomCenter);
if (getLeftPort() != null)
getLeftPort().setSpotLocation(TopLeft, back, TopLeft);

I’m not sure what you wanted, but this works as I expect it would:
public class DecoratedTextNode extends JGoTextNode {
public DecoratedTextNode() {}
public DecoratedTextNode(String s) {
JGoImage img = new JGoImage();
img.loadImage(Demo1.class.getResource(myImgName), true);
img.setSize(32, 32);
insertObjectAfter(findObject(getBackground()), img);
myDecoration = img;
public JGoObject copyObject(JGoCopyEnvironment env) {
DecoratedTextNode newobj = (DecoratedTextNode)super.copyObject(env);
newobj.myDecoration = (JGoImage)env.get(myDecoration);
return newobj;
public JGoObject removeObjectAtPos(JGoListPosition pos) {
JGoObject obj = super.removeObjectAtPos(pos);
if (obj == myDecoration) myDecoration = null;
return obj;
public void layoutChildren(JGoObject child) {
if (!isInitializing() && getDecoration() != null && getBackground() != null) {
getDecoration().setSpotLocation(BottomCenter, getBackground(), TopCenter);
public boolean doMouseDblClick(int modifiers, Point dc, Point vc, JGoView view) {
if (getDecoration() != null) {
if (myImgName.equals(“star.gif”)) myImgName = “doc.gif”;
else myImgName = “star.gif”;
getDecoration().loadImage(Demo1.class.getResource(myImgName) , true);
return true;
public JGoImage getDecoration() { return myDecoration; }
private JGoImage myDecoration;
private String myImgName = “star.gif”;
Notice that the double-click handler toggles the image that is shown.