Using JTree client property: Produce horizontal lines between level-one nodes : Client Property « Swing « Java Tutorial






Using JTree client property: Produce horizontal lines between level-one nodes
import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;

class TreeNodeVector<E> extends Vector<E> {
  String name;

  TreeNodeVector(String name) {
    this.name = name;
  }

  TreeNodeVector(String name, E elements[]) {
    this.name = name;
    for (int i = 0, n = elements.length; i < n; i++) {
      add(elements[i]);
    }
  }

  public String toString() {
    return "[" + name + "]";
  }
}

public class ChangingTreeLineStyleColorHorizontal {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("JTreeSample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Vector<String> v1 = new TreeNodeVector<String>("Two", new String[] { "Mercury", "Venus",
        "Mars" });
    Vector<Object> v2 = new TreeNodeVector<Object>("Three");
    v2.add(System.getProperties());
    v2.add(v1);
    Object rootNodes[] = {v1, v2 };
    Vector<Object> rootVector = new TreeNodeVector<Object>("Root", rootNodes);
    JTree tree = new JTree(rootVector);

    UIManager.put("Tree.line", Color.GREEN);
    tree.putClientProperty("JTree.lineStyle", "Horizontal");

    
    frame.add(new JScrollPane(tree), BorderLayout.CENTER);

    frame.setSize(300, 300);
    frame.setVisible(true);
    
  }
}








14.123.Client Property
14.123.1.Client Property
14.123.2.Swing's Client Properties Summary Table
14.123.3.Using JTree client property: Produce horizontal lines between level-one nodesUsing JTree client property: Produce horizontal lines between level-one nodes