Frage

I have been trying to construct a checkbox node tree .

My specification is that i need the parent node to also be a checkbox and not a folder like icon .

I have realized that the default check box node tree will be a folder icon for a parent node and check box for a leaf node .

Any suggestions as to how i can achieve it ?

War es hilfreich?

Lösung

Hi i think you need like followng image where node also contains the check box

you can achieve it as follow:

 String[] strs = {"swing",     // 0
     "platf",     // 1
     "basic",     // 2
     "metal",     // 3
     "JTree"};    // 4

CheckNode[] nodes = new CheckNode[strs.length];
for (int i=0;i<strs.length;i++) {
  nodes[i] = new CheckNode(strs[i]); 
}
nodes[0].add(nodes[1]);
nodes[1].add(nodes[2]);
nodes[1].add(nodes[3]);
nodes[0].add(nodes[4]);
nodes[3].setSelected(true);
JTree tree = new JTree( nodes[0] );
tree.setCellRenderer(new CheckRenderer());
tree.getSelectionModel().setSelectionMode(
  TreeSelectionModel.SINGLE_TREE_SELECTION
);

refer full example

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top