Change JTree.lineStyle client property in Java

Description

The following code shows how to change JTree.lineStyle client property.

The JTree.lineStyle property has the following valid settings:

  • None - not drawing lines to connect nodes
  • Angled - the default setting for Ocean, for drawing lines in the Tree.hash color to connect the nodes
  • Horizontal - for drawing horizontal lines between first-level nodes in the Tree.line color

The JTree.lineStyle client property is used only by the Metal look and feel.

This client property is specific to tree components, and it is not set for all trees.

Example


//w ww.j  av a  2 s.c  o  m

import java.awt.BorderLayout;
import java.util.Vector;

import javax.swing.ImageIcon;
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 Main {
  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",
        "java2s.com" });
    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);
    tree.putClientProperty("JTree.lineStyle", "None");
    
    frame.add(new JScrollPane(tree), BorderLayout.CENTER);

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

The code above generates the following result.

Change JTree.lineStyle client property in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer