Author: Zafir Anjum
Add angled lines between nodes (as in Windows explorer) or add horizontal lines separating the nodes. Specific to Metal look and feel.
By default, under the Metal (Java) look and feel, no lines are drawn to connect or group the nodes. Drawing of the lines are drawn by client property of the JComponent - "JTree.lineStyle".
The first image shows the default with no lines ("None"), the second image shows the "Angled" lineStyle and the third shows the "Horizontal" lineStyle.
Here's the code snippet that changes the property.
tree.putClientProperty( "JTree.lineStyle", "Angled" );
tree.repaint();
And here's a complete sample.
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Tree
{
static JTree tree ;
public static void main(String[] args)
{
JFrame frame = new JFrame("Table");
frame.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
Window win = e.getWindow();
win.setVisible(false);
win.dispose();
System.exit(0);
}
} );
frame.getContentPane().setLayout( new BorderLayout() );
tree = new JTree();
JScrollPane sp = new JScrollPane( tree );
frame.getContentPane().add( sp, "Center" );
JButton btn = new JButton( "Test");
btn.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e )
{
test();
}
} );
frame.getContentPane().add( btn, "South" );
frame.pack();
frame.show();
}
static boolean angled = true;
static void test()
{
if( angled )
tree.putClientProperty( "JTree.lineStyle", "Angled" );
else
tree.putClientProperty( "JTree.lineStyle", "Horizontal" );
angled = !angled;
tree.repaint();
}
}
Posted On: 18-Feb-1999