Java by API/javax.swing.event/TreeSelectionEvent — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 14:33, 31 мая 2010
TreeSelectionEvent: getNewLeadSelectionPath()
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;
public class MainClass extends JPanel {
public static void main(String[] args) {
// create a hierarchy of nodes
MutableTreeNode root = new DefaultMutableTreeNode("A");
MutableTreeNode bNode = new DefaultMutableTreeNode("B");
MutableTreeNode cNode = new DefaultMutableTreeNode("C");
root.insert(bNode, 0);
root.insert(cNode, 1);
bNode.insert(new DefaultMutableTreeNode("1"), 0);
bNode.insert(new DefaultMutableTreeNode("2"), 1);
cNode.insert(new DefaultMutableTreeNode("1"), 0);
cNode.insert(new DefaultMutableTreeNode("2"), 1);
final DefaultTreeModel model = new DefaultTreeModel(root);
final JTree tree = new JTree(model);
final JTextField nameField = new JTextField("Z");
final JButton button = new JButton("Add");
button.setEnabled(false);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath tp = tree.getSelectionPath();
MutableTreeNode insertNode = (MutableTreeNode) tp.getLastPathComponent();
int insertIndex = 0;
if (insertNode.getParent() != null) {
MutableTreeNode parent = (MutableTreeNode) insertNode.getParent();
insertIndex = parent.getIndex(insertNode) + 1;
insertNode = parent;
}
MutableTreeNode node = new DefaultMutableTreeNode(nameField.getText());
model.insertNodeInto(node, insertNode, insertIndex);
}
});
JPanel addPanel = new JPanel(new GridLayout(2, 1));
addPanel.add(nameField);
addPanel.add(button);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
TreePath tp = e.getNewLeadSelectionPath();
button.setEnabled(tp != null);
}
});
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.getContentPane().add(new JScrollPane(tree));
frame.getContentPane().add(addPanel, BorderLayout.SOUTH);
frame.setVisible(true);
}
}
TreeSelectionEvent: isAddedPath(int index)
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
public class Main {
public static void main(String[] argv) throws Exception {
JTree tree = new JTree();
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent evt) {
TreePath[] paths = evt.getPaths();
for (int i = 0; i < paths.length; i++) {
if (evt.isAddedPath(i)) {
System.out.println("This node has been selected");
} else {
System.out.println("This node has been deselected");
}
}
}
});
}
}