julianfperez
julianfperez

Reputation: 1744

Double icon for a tree structure in Matlab

I have designed the following tree structure:

function tree
import javax.swing.*
import javax.swing.tree.*;
[I,map] = checkedIcon;
javaImage_checked = im2java(I,map);
[I,map] = uncheckedIcon;
javaImage_unchecked = im2java(I,map);

% javaImage_checked/unchecked are assumed to have the same width
iconWidth = javaImage_unchecked.getWidth;

% create top node
root = uitreenode('v0','root', 'Results',[], 0);
% create children with checkboxes
Node1 = uitreenode('v0','Node1', 'Position', [], false);
root.add(Node1);
Node11 = uitreenode('v0','Node11', 'Cartesians Coordinates', [], true);
Node11.setIcon(javaImage_checked);
Node1.add(Node11);
Node12 = uitreenode('v0','Node12', 'Spherical Coordinates', [], true);
Node12.setIcon(javaImage_unchecked);
Node1.add(Node12);
Node2 = uitreenode('v0','Node2', 'Velocity', [], false);
root.add(Node2);
Node3 = uitreenode('v0','Node3', 'Acceleration', [], false);
root.add(Node3);

% set treeModel
treeModel = DefaultTreeModel(root);

% create the tree
[tree, container]= uitree('v0');
tree.setModel(treeModel);
% make root the initially selected node
tree.setSelectedNode(Node11);

 function [I,map] = checkedIcon()
    I = uint8(...
        [1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0;
         2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,1;
         2,2,2,2,2,2,2,2,2,2,2,2,0,2,3,1;
         2,2,1,1,1,1,1,1,1,1,1,0,2,2,3,1;
         2,2,1,1,1,1,1,1,1,1,0,1,2,2,3,1;
         2,2,1,1,1,1,1,1,1,0,1,1,2,2,3,1;
         2,2,1,1,1,1,1,1,0,0,1,1,2,2,3,1;
         2,2,1,0,0,1,1,0,0,1,1,1,2,2,3,1;
         2,2,1,1,0,0,0,0,1,1,1,1,2,2,3,1;
         2,2,1,1,0,0,0,0,1,1,1,1,2,2,3,1;
         2,2,1,1,1,0,0,1,1,1,1,1,2,2,3,1;
         2,2,1,1,1,0,1,1,1,1,1,1,2,2,3,1;
         2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
         2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,1;
         2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,1;
         1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1]);
     map = [0.023529,0.4902,0;
            1,1,1;
            0,0,0;
            0.50196,0.50196,0.50196;
            0.50196,0.50196,0.50196;
            0,0,0;
            0,0,0;
            0,0,0];
  end

  function [I,map] = uncheckedIcon()
     I = uint8(...
       [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1;
        2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1;
        2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,1,1,1,1,1,1,1,1,1,1,2,2,3,1;
        2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,1;
        2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,1;
        1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1]);
     map = ...
      [0.023529,0.4902,0;
       1,1,1;
       0,0,0;
       0.50196,0.50196,0.50196;
       0.50196,0.50196,0.50196;
       0,0,0;
       0,0,0;
       0,0,0];
  end

  end

I would like to able to show an additional icon (on the right of the checked/unchecked icon and on the left of the nodes names). A solution that I have thought is replacing the name of the node with a jLabel component.

icon2 = javax.swing.ImageIcon('image.jpg');
jLabel = javax.swing.JLabel('Cartesian Coordinates');
jLabel.setIcon(icon2);
Node11.setName(jLabel);

However, the above code does not work. I also have tried to add a jLabel component to a treenode without success. I have even tried to modify the property ‘setIcon’ of a DefaultCheckBoxNode but this component has not that property. So, I asked myself if there is some solution for my problem.

Upvotes: 1

Views: 569

Answers (1)

Kevin K
Kevin K

Reputation: 9584

You could create an icon that is a combination of the two icons, drawn side by side, and set that icon on the tree node. I am not sure if this is possible to do using just the MATLAB API, but my understanding is that you can construct and work with Java objects directly in MATLAB, so it should be possible either way.

It appears the setIcon() method accepts a Java Image object, not an Icon object. Here is the general strategy for creating a composite image:

  1. Create a BufferedImage object with an appropriate width and height to hold both icons
  2. Get the Graphics2D object for the buffered image using BufferedImage.createGraphics()
  3. Draw each icon into the buffered image using Graphics2D.drawImage()

This question and this question may be useful to you.

Upvotes: 2

Related Questions