Java by API/javax.swing/JTree
Версия от 17:43, 31 мая 2010; (обсуждение)
Содержание
- 1 JTree: addTreeSelectionListener(TreeSelectionListener tsl)
- 2 JTree: addTreeWillExpandListener(TreeWillExpandListener tel)
- 3 JTree: convertValueToText(Object v,boolean s,boolean e,boolean l,int r,boolean h)
- 4 JTree.DynamicUtilTreeNode: createChildren(DefaultMutableTreeNode parent, Object children)
- 5 JTree: getCellRenderer()
- 6 JTree: getLeadSelectionRow()
- 7 JTree: getMaxSelectionRow()
- 8 JTree: getMinSelectionRow()
- 9 JTree: getNextMatch(String prefix, int startingRow, Bias bias)
- 10 JTree: getPathForLocation(int x, int y)
- 11 JTree: getRowHeight()
- 12 JTree: getSelectionRows()
- 13 JTree: putClientProperty("JTree.lineStyle", "None")
- 14 JTree: setAutoscrolls(boolean b)
- 15 JTree: setCellEditor(TreeCellEditor editor)
- 16 JTree: setExpandedState(TreePath path, boolean state)
- 17 JTree: setRowHeight(int rowHeight)
- 18 JTree: setTransferHandler(TransferHandler newHandler) (Drag and drop files from file explore)
- 19 new JTree()
- 20 new JTree(TreeNode root)
- 21 new JTree(Vector vector)
- 22 Tree.closedIcon
- 23 Tree.collapsedIcon
- 24 Tree.expandedIcon
- 25 Tree.leafIcon
- 26 Tree.openIcon
JTree: addTreeSelectionListener(TreeSelectionListener tsl)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
public class MainClass extends JPanel {
public MainClass(File dir) {
setLayout(new BorderLayout());
JTree tree = new JTree(addNodes(null, dir));
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
System.out.println("You selected " + node);
}
});
JScrollPane scrollpane = new JScrollPane();
scrollpane.getViewport().add(tree);
add(BorderLayout.CENTER, scrollpane);
}
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
String curPath = dir.getPath();
DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
if (curTop != null) {
curTop.add(curDir);
}
ArrayList ol = new ArrayList();
String[] tmp = dir.list();
for (int i = 0; i < tmp.length; i++)
ol.add(tmp[i]);
File f;
ArrayList files = new ArrayList();
for (int i = 0; i < ol.size(); i++) {
String thisObject = (String) ol.get(i);
String newPath;
if (curPath.equals("."))
newPath = thisObject;
else
newPath = curPath + File.separator + thisObject;
if ((f = new File(newPath)).isDirectory())
addNodes(curDir, f);
else
files.add(thisObject);
}
for (int fnum = 0; fnum < files.size(); fnum++)
curDir.add(new DefaultMutableTreeNode(files.get(fnum)));
return curDir;
}
public Dimension getMinimumSize() {
return new Dimension(200, 400);
}
public Dimension getPreferredSize() {
return new Dimension(200, 400);
}
public static void main(String[] av) {
JFrame frame = new JFrame("MainClass");
frame.setForeground(Color.black);
frame.setBackground(Color.lightGray);
Container cp = frame.getContentPane();
cp.add(new MainClass(new File(".")));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
JTree: addTreeWillExpandListener(TreeWillExpandListener tel)
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.ExpandVetoException;
import javax.swing.tree.TreePath;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
root.add(new DefaultMutableTreeNode("A"));
root.add(new DefaultMutableTreeNode("B"));
root.add(new DefaultMutableTreeNode("C"));
JTree tree = new JTree(root);
TreeWillExpandListener treeWillExpandListener = new TreeWillExpandListener() {
public void treeWillCollapse(TreeExpansionEvent treeExpansionEvent)
throws ExpandVetoException {
TreePath path = treeExpansionEvent.getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
String data = node.getUserObject().toString();
System.out.println("WillCollapse: " + data);
}
public void treeWillExpand(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException {
TreePath path = treeExpansionEvent.getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
String data = node.getUserObject().toString();
System.out.println("WillExpand: " + data);
}
};
tree.addTreeWillExpandListener(treeWillExpandListener);
JScrollPane scrollPane = new JScrollPane(tree);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTree: convertValueToText(Object v,boolean s,boolean e,boolean l,int r,boolean h)
import java.awt.BorderLayout;
import java.awt.ruponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;
public class MainClass {
public static void main(final String args[]) {
JFrame frame = new JFrame("Tree Tips");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTree tree = new JTree(new String[]{"a","b","c"});
ToolTipManager.sharedInstance().registerComponent(tree);
TreeCellRenderer renderer = new ToolTipTreeCellRenderer();
tree.setCellRenderer(renderer);
JScrollPane scrollPane = new JScrollPane(tree);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
class ToolTipTreeCellRenderer implements TreeCellRenderer {
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
public ToolTipTreeCellRenderer() {
}
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
boolean expanded, boolean leaf, int row, boolean hasFocus) {
Object tip = null;
if (value != null) {
if (value instanceof DefaultMutableTreeNode) {
tip = ((DefaultMutableTreeNode) value).getUserObject();
} else {
tip = tree.convertValueToText(value, selected, expanded, leaf, row, hasFocus);
}
renderer.setToolTipText((String)tip);
}
renderer.setText((String)tip);
return renderer;
}
}
JTree.DynamicUtilTreeNode: createChildren(DefaultMutableTreeNode parent, Object children)
import java.awt.BorderLayout;
import java.util.Hashtable;
import java.util.Properties;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class MainClass {
public static void main(final String args[]) {
JFrame frame = new JFrame("DynamicUtilTreeNode Hashtable");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
Hashtable<Object, Object> hashtable = new Hashtable<Object, Object>();
hashtable.put("A", args);
hashtable.put("B", new String[] { "a", "b", "c" });
Hashtable<Object, Object> innerHashtable = new Hashtable<Object, Object>();
Properties props = System.getProperties();
innerHashtable.put(props, props);
hashtable.put("C", innerHashtable);
JTree.DynamicUtilTreeNode.createChildren(root, hashtable);
JTree tree = new JTree(root);
JScrollPane scrollPane = new JScrollPane(tree);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTree: getCellRenderer()
import java.awt.BorderLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellEditor;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;
public class MainClass {
public static void main(final String args[]) {
JFrame frame = new JFrame("Editable Tree");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTree tree = new JTree();
tree.setEditable(true);
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
String elements[] = { "A", "B", "C", "D" };
JComboBox comboBox = new JComboBox(elements);
comboBox.setEditable(true);
TreeCellEditor comboEditor = new DefaultCellEditor(comboBox);
TreeCellEditor editor = new DefaultTreeCellEditor(tree, renderer, comboEditor);
tree.setCellEditor(editor);
JScrollPane scrollPane = new JScrollPane(tree);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTree: getLeadSelectionRow()
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
public class Main {
public static void main(String args[]) {
String title = "JTree Sample";
JFrame frame = new JFrame(title);
JTree tree = new JTree();
TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
JTree treeSource = (JTree) treeSelectionEvent.getSource();
System.out.println("Min: " + treeSource.getMinSelectionRow());
System.out.println("Max: " + treeSource.getMaxSelectionRow());
System.out.println("Lead: " + treeSource.getLeadSelectionRow());
System.out.println("Row: " + treeSource.getSelectionRows()[0]);
}
};
tree.addTreeSelectionListener(treeSelectionListener);
JScrollPane scrollPane = new JScrollPane(tree);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTree: getMaxSelectionRow()
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
public class Main {
public static void main(String args[]) {
String title = "JTree Sample";
JFrame frame = new JFrame(title);
JTree tree = new JTree();
TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
JTree treeSource = (JTree) treeSelectionEvent.getSource();
System.out.println("Min: " + treeSource.getMinSelectionRow());
System.out.println("Max: " + treeSource.getMaxSelectionRow());
System.out.println("Lead: " + treeSource.getLeadSelectionRow());
System.out.println("Row: " + treeSource.getSelectionRows()[0]);
}
};
tree.addTreeSelectionListener(treeSelectionListener);
JScrollPane scrollPane = new JScrollPane(tree);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTree: getMinSelectionRow()
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
public class Main {
public static void main(String args[]) {
String title = "JTree Sample";
JFrame frame = new JFrame(title);
JTree tree = new JTree();
TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
JTree treeSource = (JTree) treeSelectionEvent.getSource();
System.out.println("Min: " + treeSource.getMinSelectionRow());
System.out.println("Max: " + treeSource.getMaxSelectionRow());
System.out.println("Lead: " + treeSource.getLeadSelectionRow());
System.out.println("Row: " + treeSource.getSelectionRows()[0]);
}
};
tree.addTreeSelectionListener(treeSelectionListener);
JScrollPane scrollPane = new JScrollPane(tree);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTree: getNextMatch(String prefix, int startingRow, Bias bias)
import javax.swing.JTree;
import javax.swing.text.Position;
import javax.swing.tree.TreePath;
public class Main {
public static void main(String[] argv) throws Exception {
JTree tree = new JTree();
int startRow = tree.getRowCount() - 1;
String prefix = "b";
TreePath path = tree.getNextMatch(prefix, startRow, Position.Bias.Backward);
System.out.println(path);
}
}
JTree: getPathForLocation(int x, int y)
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
public class MainClass extends JPanel {
public MainClass() {
final JTree tree;
final JTextField jtf;
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options");
DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
top.add(a);
DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
a.add(a1);
DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");
a.add(a2);
DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
top.add(b);
DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
b.add(b1);
DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
b.add(b2);
DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3");
b.add(b3);
tree = new JTree(top);
JScrollPane jsp = new JScrollPane(tree);
add(jsp, BorderLayout.CENTER);
jtf = new JTextField("", 20);
add(jtf, BorderLayout.SOUTH);
tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
TreePath tp = tree.getPathForLocation(me.getX(), me.getY());
if (tp != null)
jtf.setText(tp.toString());
else
jtf.setText("");
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
JTree: getRowHeight()
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Changed Renderer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTree tree = new JTree();
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
renderer.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 32));
int rowHeight = tree.getRowHeight();
if (rowHeight <= 0) {
tree.setRowHeight(rowHeight - 1);
}
Color backgroundSelection = renderer.getBackgroundSelectionColor();
renderer.setBackgroundSelectionColor(Color.blue);
renderer.setBackgroundNonSelectionColor(backgroundSelection);
// Swap text colors
Color textSelection = renderer.getTextSelectionColor();
renderer.setTextSelectionColor(Color.green);
renderer.setTextNonSelectionColor(textSelection);
JScrollPane scrollPane = new JScrollPane(tree);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTree: getSelectionRows()
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
public class Main {
public static void main(String args[]) {
String title = "JTree Sample";
JFrame frame = new JFrame(title);
JTree tree = new JTree();
TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
JTree treeSource = (JTree) treeSelectionEvent.getSource();
System.out.println("Min: " + treeSource.getMinSelectionRow());
System.out.println("Max: " + treeSource.getMaxSelectionRow());
System.out.println("Lead: " + treeSource.getLeadSelectionRow());
System.out.println("Row: " + treeSource.getSelectionRows()[0]);
}
};
tree.addTreeSelectionListener(treeSelectionListener);
JScrollPane scrollPane = new JScrollPane(tree);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTree: putClientProperty("JTree.lineStyle", "None")
import java.awt.BorderLayout;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Tree Lines");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Box box = Box.createHorizontalBox();
JTree tree1 = new JTree();
JScrollPane scrollPane1 = new JScrollPane(tree1);
tree1.setAutoscrolls(true);
JTree tree2 = new JTree();
tree2.putClientProperty("JTree.lineStyle", "None");
JScrollPane scrollPane2 = new JScrollPane(tree2);
box.add(scrollPane1, BorderLayout.WEST);
box.add(scrollPane2, BorderLayout.EAST);
frame.add(box, BorderLayout.CENTER);
frame.setSize(300, 240);
frame.setVisible(true);
}
}
JTree: setAutoscrolls(boolean b)
import java.awt.BorderLayout;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Tree Lines");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Box box = Box.createHorizontalBox();
JTree tree1 = new JTree();
JScrollPane scrollPane1 = new JScrollPane(tree1);
tree1.setAutoscrolls(true);
JTree tree2 = new JTree();
tree2.putClientProperty("JTree.lineStyle", "None");
JScrollPane scrollPane2 = new JScrollPane(tree2);
box.add(scrollPane1, BorderLayout.WEST);
box.add(scrollPane2, BorderLayout.EAST);
frame.add(box, BorderLayout.CENTER);
frame.setSize(300, 240);
frame.setVisible(true);
}
}
JTree: setCellEditor(TreeCellEditor editor)
import java.awt.BorderLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellEditor;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;
public class MainClass {
public static void main(final String args[]) {
JFrame frame = new JFrame("Editable Tree");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTree tree = new JTree();
tree.setEditable(true);
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
String elements[] = { "A", "B", "C", "D" };
JComboBox comboBox = new JComboBox(elements);
comboBox.setEditable(true);
TreeCellEditor comboEditor = new DefaultCellEditor(comboBox);
TreeCellEditor editor = new DefaultTreeCellEditor(tree, renderer, comboEditor);
tree.setCellEditor(editor);
JScrollPane scrollPane = new JScrollPane(tree);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTree: setExpandedState(TreePath path, boolean state)
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreePath;
public class Main {
public static void main(String[] argv) {
JTree tree = new JTree() {
protected void setExpandedState(TreePath path, boolean state) {
if (state) {
super.setExpandedState(path, state);
}
}
};
JFrame frame = new JFrame("Ignore all collapse requests");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(tree));
frame.setSize(380, 320);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
JTree: setRowHeight(int rowHeight)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Changed Renderer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTree tree = new JTree();
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
renderer.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 32));
int rowHeight = tree.getRowHeight();
if (rowHeight <= 0) {
tree.setRowHeight(rowHeight - 1);
}
Color backgroundSelection = renderer.getBackgroundSelectionColor();
renderer.setBackgroundSelectionColor(Color.blue);
renderer.setBackgroundNonSelectionColor(backgroundSelection);
// Swap text colors
Color textSelection = renderer.getTextSelectionColor();
renderer.setTextSelectionColor(Color.green);
renderer.setTextNonSelectionColor(textSelection);
JScrollPane scrollPane = new JScrollPane(tree);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
JTree: setTransferHandler(TransferHandler newHandler) (Drag and drop files from file explore)
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
public class FSTest extends JFrame {
public FSTest() {
super("FSTree Component Test");
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
FSTree fst = new FSTree(new DefaultMutableTreeNode("Starter"));
getContentPane().add(new JScrollPane(fst));
setVisible(true);
}
public static void main(String args[]) {
new FSTest();
}
}
class FSTree extends JTree {
public FSTree() {
super();
init();
}
public FSTree(TreeModel newModel) {
super(newModel);
init();
}
public FSTree(TreeNode root) {
super(root);
init();
}
public FSTree(TreeNode root, boolean asks) {
super(root, asks);
init();
}
private void init() {
setDragEnabled(false);
setTransferHandler(new FSTransfer());
}
public class FSTransfer extends TransferHandler {
public boolean importData(JComponent comp, Transferable t) {
if (!(comp instanceof FSTree)) {
return false;
}
if (!t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
return false;
}
FSTree tree = (FSTree) comp;
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
try {
List data = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
Iterator i = data.iterator();
while (i.hasNext()) {
File f = (File) i.next();
root.add(new DefaultMutableTreeNode(f.getName()));
}
model.reload();
return true;
} catch (UnsupportedFlavorException ufe) {
System.err.println("Ack! we should not be here.\nBad Flavor.");
} catch (IOException ioe) {
System.out.println("Something failed during import:\n" + ioe);
}
return false;
}
// We only support file lists on FSTrees...
public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
if (comp instanceof FSTree) {
for (int i = 0; i < transferFlavors.length; i++) {
if (!transferFlavors[i].equals(DataFlavor.javaFileListFlavor)) {
return false;
}
}
return true;
}
return false;
}
}
}
new JTree()
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
public class MainClass {
public static void main(String args[]) {
JFrame f = new JFrame("JTree Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTree tree = new JTree();
JScrollPane scrollPane = new JScrollPane(tree);
f.add(scrollPane, BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
}
}
new JTree(TreeNode root)
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
public class MainClass extends JPanel {
public MainClass() {
final JTree tree;
final JTextField jtf;
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options");
DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
top.add(a);
DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
a.add(a1);
DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");
a.add(a2);
DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
top.add(b);
DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
b.add(b1);
DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
b.add(b2);
DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3");
b.add(b3);
tree = new JTree(top);
JScrollPane jsp = new JScrollPane(tree);
add(jsp, BorderLayout.CENTER);
jtf = new JTextField("", 20);
add(jtf, BorderLayout.SOUTH);
tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
TreePath tp = tree.getPathForLocation(me.getX(), me.getY());
if (tp != null)
jtf.setText(tp.toString());
else
jtf.setText("");
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
new JTree(Vector vector)
import java.awt.BorderLayout;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Swing Package Hierarchy");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Vector rootVector = new TreeVector("A", new String[] { "a" });
JTree tree = new JTree(rootVector);
tree.setRootVisible(true);
TreeModel model = tree.getModel();
model.valueForPathChanged(new TreePath(model.getRoot()), "javax.swing");
((DefaultTreeModel) model).reload();
JScrollPane scrollPane = new JScrollPane(tree);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
class TreeVector extends Vector {
String name;
TreeVector(String name) {
this.name = name;
}
TreeVector(String name, Object elements[]) {
this.name = name;
for (int i = 0, n = elements.length; i < n; i++) {
add(elements[i]);
}
}
public String toString() {
return name;
}
}
Tree.closedIcon
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class MainClass extends JFrame {
JTree tree;
DefaultTreeModel treeModel;
public MainClass() {
super("Tree Test Example");
setSize(200, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
UIManager.put("Tree.leafIcon", new ImageIcon("leafIcon.gif"));
UIManager.put("Tree.openIcon", new ImageIcon("openIcon.gif"));
UIManager.put("Tree.closedIcon", new ImageIcon("closedIcon.gif"));
UIManager.put("Tree.expandedIcon", new ImageIcon("expandedIcon.gif"));
UIManager.put("Tree.collapsedIcon", new ImageIcon("collapsedIcon"));
}
public void init() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode subroot = new DefaultMutableTreeNode("SubRoot");
DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf 1");
DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode("Leaf 2");
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
treeModel.insertNodeInto(subroot, root, 0);
subroot.add(leaf1);
root.add(leaf2);
tree.putClientProperty("JTree.lineStyle", "Angled");
getContentPane().add(tree, BorderLayout.CENTER);
}
public static void main(String args[]) {
MainClass tt = new MainClass();
tt.init();
tt.setVisible(true);
}
}
Tree.collapsedIcon
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class MainClass extends JFrame {
JTree tree;
DefaultTreeModel treeModel;
public MainClass() {
super("Tree Test Example");
setSize(200, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
UIManager.put("Tree.leafIcon", new ImageIcon("leafIcon.gif"));
UIManager.put("Tree.openIcon", new ImageIcon("openIcon.gif"));
UIManager.put("Tree.closedIcon", new ImageIcon("closedIcon.gif"));
UIManager.put("Tree.expandedIcon", new ImageIcon("expandedIcon.gif"));
UIManager.put("Tree.collapsedIcon", new ImageIcon("collapsedIcon"));
}
public void init() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode subroot = new DefaultMutableTreeNode("SubRoot");
DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf 1");
DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode("Leaf 2");
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
treeModel.insertNodeInto(subroot, root, 0);
subroot.add(leaf1);
root.add(leaf2);
tree.putClientProperty("JTree.lineStyle", "Angled");
getContentPane().add(tree, BorderLayout.CENTER);
}
public static void main(String args[]) {
MainClass tt = new MainClass();
tt.init();
tt.setVisible(true);
}
}
Tree.expandedIcon
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class MainClass extends JFrame {
JTree tree;
DefaultTreeModel treeModel;
public MainClass() {
super("Tree Test Example");
setSize(200, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
UIManager.put("Tree.leafIcon", new ImageIcon("leafIcon.gif"));
UIManager.put("Tree.openIcon", new ImageIcon("openIcon.gif"));
UIManager.put("Tree.closedIcon", new ImageIcon("closedIcon.gif"));
UIManager.put("Tree.expandedIcon", new ImageIcon("expandedIcon.gif"));
UIManager.put("Tree.collapsedIcon", new ImageIcon("collapsedIcon"));
}
public void init() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode subroot = new DefaultMutableTreeNode("SubRoot");
DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf 1");
DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode("Leaf 2");
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
treeModel.insertNodeInto(subroot, root, 0);
subroot.add(leaf1);
root.add(leaf2);
tree.putClientProperty("JTree.lineStyle", "Angled");
getContentPane().add(tree, BorderLayout.CENTER);
}
public static void main(String args[]) {
MainClass tt = new MainClass();
tt.init();
tt.setVisible(true);
}
}
Tree.leafIcon
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class MainClass extends JFrame {
JTree tree;
DefaultTreeModel treeModel;
public MainClass() {
super("Tree Test Example");
setSize(200, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
UIManager.put("Tree.leafIcon", new ImageIcon("leafIcon.gif"));
UIManager.put("Tree.openIcon", new ImageIcon("openIcon.gif"));
UIManager.put("Tree.closedIcon", new ImageIcon("closedIcon.gif"));
UIManager.put("Tree.expandedIcon", new ImageIcon("expandedIcon.gif"));
UIManager.put("Tree.collapsedIcon", new ImageIcon("collapsedIcon"));
}
public void init() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode subroot = new DefaultMutableTreeNode("SubRoot");
DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf 1");
DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode("Leaf 2");
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
treeModel.insertNodeInto(subroot, root, 0);
subroot.add(leaf1);
root.add(leaf2);
tree.putClientProperty("JTree.lineStyle", "Angled");
getContentPane().add(tree, BorderLayout.CENTER);
}
public static void main(String args[]) {
MainClass tt = new MainClass();
tt.init();
tt.setVisible(true);
}
}
Tree.openIcon
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class MainClass extends JFrame {
JTree tree;
DefaultTreeModel treeModel;
public MainClass() {
super("Tree Test Example");
setSize(200, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
UIManager.put("Tree.leafIcon", new ImageIcon("leafIcon.gif"));
UIManager.put("Tree.openIcon", new ImageIcon("openIcon.gif"));
UIManager.put("Tree.closedIcon", new ImageIcon("closedIcon.gif"));
UIManager.put("Tree.expandedIcon", new ImageIcon("expandedIcon.gif"));
UIManager.put("Tree.collapsedIcon", new ImageIcon("collapsedIcon"));
}
public void init() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode subroot = new DefaultMutableTreeNode("SubRoot");
DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf 1");
DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode("Leaf 2");
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
treeModel.insertNodeInto(subroot, root, 0);
subroot.add(leaf1);
root.add(leaf2);
tree.putClientProperty("JTree.lineStyle", "Angled");
getContentPane().add(tree, BorderLayout.CENTER);
}
public static void main(String args[]) {
MainClass tt = new MainClass();
tt.init();
tt.setVisible(true);
}
}