Let us demonstrate a simple creation of a Tree application, TreeExample.java:
The full code listing for TreeExample.java is as follows:
// Imports
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TreeExample
extends JFrame
{
// Instance attributes used in this example
private JPanel topPanel;
private JTree tree;
private JScrollPane scrollPane;
// Constructor of main frame
public TreeExample()
{
// Set the frame characteristics
setTitle( "Simple Tree Application" );
setSize( 300, 100 );
setBackground( Color.gray );
// Create a panel to hold all other components
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create a new tree control
tree = new JTree();
// Add the listbox to a scrolling pane
scrollPane = new JScrollPane();
scrollPane.getViewport().add( tree );
topPanel.add( scrollPane, BorderLayout.CENTER );
}
// Main entry point for this example
public static void main( String args[] )
{
// Create an instance of the test application
TreeExample mainFrame = new TreeExample();
mainFrame.setVisible( true );
}
}