![]() | ![]() | ![]() | ![]() | ![]() |
The TreeListSelector is a tree-to-list selector that enables you to move single or multiple items between the tree and list and submit the items that are selected on the target list. This sample shows how to create a Swing TreeListSelector.
Here are the steps for creating a simple TreeListSelector:
The resulting code would look something like this:
DefaultTreeModel sourceModel = new DefaultTreeModel(); ... add nodes to your TreeModel ... TreeListSelector treeListSelector = new TreeListSelector(); treeListSelector.setModel(sourceModel);
The code shown on the Full Code tab will create a TreeModel and display a default TreeListSelector with that model. It contains the customizations shown below, but they are commented out. You can remove the comment delimiters that surround the customization code in order to see that customization in your Java application.
A default TreeListSelector looks like the following after an item is selected:

This sample shows the following customizations:
By default, the root node of the source tree is visible. To hide the root node, add the following line of code after you create the TreeListSelector:
treeListSelector.setSourceRootVisible(false);
You will see the root node disappear in the source tree:

By default, only children of the parent nodes can be moved to the target list. Your application might need to move both parent and child nodes. To enable both parent and child nodes to be selected, add the following line of code after you create the TreeListSelector:
treeListSelector.setParentNodesMoveable(true);
You will see both parent and child nodes show up in the target list after you select them:

By default, only children of a parent node can be moved to the target list. Your application may want to change the default behavior to move only the parent nodes. To move only parent nodes, add the following lines of code after you create the TreeListSelector:
treeListSelector.setParentNodesMoveable(true); treeListSelector.setParentOrChildrenMoveable(true);
You will see that only selected parent nodes will show up in the target list:

These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Tip: For help with building a SAS Java Project, see SAS Note 32218.
package samples;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import com.sas.swing.visuals.dualselector.TreeListSelector;
public class TreeListSelectorApp {
protected TreeListSelector treeListSelector;
public static void main(String[] args) {
// set swing look and feel with system look and feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
TreeListSelectorApp simpleApp = new TreeListSelectorApp();
simpleApp.createFrame(args).setVisible(true);
}
// create a frame and add the table view to it
public JFrame createFrame(String[] args) {
JFrame frame = new JFrame(this.getClass().getName());
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout(20, 20));
// create a dual tree selector
treeListSelector = new TreeListSelector();
// Create the tree node structure for the source tree
DefaultMutableTreeNode rootNode1 = new DefaultMutableTreeNode(
"Root Node");
DefaultMutableTreeNode subRoot11 = new DefaultMutableTreeNode(
"SubRoot 11");
DefaultMutableTreeNode leaf111 = new DefaultMutableTreeNode("Leaf 111");
DefaultMutableTreeNode leaf112 = new DefaultMutableTreeNode("Leaf 112");
rootNode1.add(subRoot11);
subRoot11.add(leaf111);
subRoot11.add(leaf112);
DefaultMutableTreeNode subRoot12 = new DefaultMutableTreeNode(
"SubRoot 12");
DefaultMutableTreeNode leaf121 = new DefaultMutableTreeNode("Leaf 121");
DefaultMutableTreeNode leaf122 = new DefaultMutableTreeNode("Leaf 122");
rootNode1.add(subRoot12);
subRoot12.add(leaf121);
subRoot12.add(leaf122);
DefaultMutableTreeNode subRoot21 = new DefaultMutableTreeNode(
"SubRoot 21");
DefaultMutableTreeNode leaf211 = new DefaultMutableTreeNode("Leaf 211");
DefaultMutableTreeNode leaf212 = new DefaultMutableTreeNode("Leaf 212");
subRoot11.add(subRoot21);
subRoot21.add(leaf211);
subRoot21.add(leaf212);
// Create and set the new source tree model
DefaultTreeModel treeModel1 = new DefaultTreeModel(rootNode1);
treeListSelector.setModel(treeModel1);
// CUSTOMIZATION: Setting root node visibility
// By default, the root node is visible in the source tree
// Here's how to hide it
// Uncomment the next line to see this customization in your java app
// treeListSelector.setSourceRootVisible(false);
// CUSTOMIZATION: Moving parent nodes
// By default, the parent nodes cannot be moved to the target list
// Enable them to be moved along with their children
// Uncomment the next line to see this customization in your java app
// treeListSelector.setParentNodesMoveable(true);
// CUSTOMIZATION: Moving ONLY parent nodes
// By default, only the children of a parent node can be moved to the
// target list
// Enable parents to move independently of children
// Uncomment the next two lines to see this customization in your java
// app
// treeListSelector.setParentNodesMoveable(true);
// treeListSelector.setParentOrChildrenMoveable(true);
// add the selector to the center of the content pane
contentPane.add(treeListSelector, BorderLayout.CENTER);
// add some dummy labels to the layout
// this will add some white space around the selector
// this can be removed if you don't want the white space
contentPane.add(new JLabel(), BorderLayout.NORTH);
contentPane.add(new JLabel(), BorderLayout.SOUTH);
contentPane.add(new JLabel(), BorderLayout.EAST);
contentPane.add(new JLabel(), BorderLayout.WEST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
return frame;
}
}
|
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.

| Type: | Sample |
| Date Modified: | 2008-08-07 12:25:29 |
| Date Created: | 2006-02-09 15:35:37 |
| Product Family | Product | Host | Product Release | SAS Release | ||
| Starting | Ending | Starting | Ending | |||
| SAS System | SAS AppDev Studio | Microsoft Windows 2000 Datacenter Server | 3.2 | 9.1 TS1M3 SP4 | ||
| Microsoft Windows 2000 Advanced Server | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows 2000 Server | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows 2000 Professional | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows Server 2003 Datacenter Edition | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows Server 2003 Enterprise Edition | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows NT Workstation | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft® Windows® for x64 | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows Server 2003 Standard Edition | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows XP Professional | 3.2 | 9.1 TS1M3 SP4 | ||||
| Windows Vista | 3.2 | 9.1 TS1M3 SP4 | ||||




