Java论坛网»Java技术»急...急...急... 删除树节点后如何调整光标位置
急...急...急... 删除树节点后如何调整光标位置
问?:
只能删除第三层上的节点, 删除节点后调整光标位置。
最后一个子节点:将光标调整到父节点上
后有节点:将光标调整到后一节点上
后无节点:将光标调整到前一节点上
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
JTree tree = new JTree();
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
TreeSelectionModel selectionModel = tree.getSelectionModel();
JButton removeButton = new JButton("Remove selected node");
JButton addButton = new JButton("Add node");
public Test() {
Container contentPane = getContentPane();
selectionModel
.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(tree, BorderLayout.CENTER);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
TreePath path = e.getNewLeadSelectionPath();
boolean nodesAreSelected = (path != null);
addButton.setEnabled(nodesAreSelected);
removeButton.setEnabled(nodesAreSelected);
}
});
model.addTreeModelListener(new TreeModelListener() {
public void treeNodesInserted(TreeModelEvent e) {
showInsertionOrRemoval(e, " added to ");
}
public void treeNodesRemoved(TreeModelEvent e) {
showInsertionOrRemoval(e, " removed from ");
}
private void showInsertionOrRemoval(TreeModelEvent e, String s) {
Object[] parentPath = e.getPath();
int[] indexes = e.getChildIndices();
Object[] children = e.getChildren();
Object parent = parentPath[parentPath.length - 1];
JOptionPane.showMessageDialog(Test.this, "Node \""
+ children[0].toString() + "\"" + s + parent.toString()
+ " at index " + indexes[0], "Node Added or Removed",
JOptionPane.INFORMATION_MESSAGE);
}
public void treeNodesChanged(TreeModelEvent e) {
}
public void treeStructureChanged(TreeModelEvent e) {
}
});
}
class ControlPanel extends JPanel {
public ControlPanel() {
addButton.setEnabled(false);
removeButton.setEnabled(false);
add(addButton);
add(removeButton);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path = selectionModel.getSelectionPath();
MutableTreeNode parent, node = (MutableTreeNode) path
.getLastPathComponent();
if (path.getPathCount() > 1)
parent = (MutableTreeNode) node.getParent();
else
parent = (MutableTreeNode) node;
int index = parent.getIndex(node) + 1;
String s = JOptionPane.showInputDialog(Test.this,
"Enter a name for the new node:", "New Tree Node",
JOptionPane.QUESTION_MESSAGE);
MutableTreeNode newNode = new DefaultMutableTreeNode(s);
model.insertNodeInto(newNode, parent, index);
}
});
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path = selectionModel.getSelectionPath();
if (path.getPathCount() == 1) {
JOptionPane.showMessageDialog(ControlPanel.this,
"Can't remove root node!");
return;
}else if (path.getPathCount() == 2) {
JOptionPane.showMessageDialog(ControlPanel.this,
"Can't remove node!");
return;
}
MutableTreeNode node = (MutableTreeNode) path
.getLastPathComponent();
model.removeNodeFromParent(node);
}
});
}
}
public static void main(String args[]) {
GraphicJavaApplication.launch(new Test(), "Tree Model Example", 300,
300, 450, 300);
}
}
class GraphicJavaApplication extends WindowAdapter {
public static void launch(final JFrame f, String title, final int x,
final int y, final int w, int h) {
f.setTitle(title);
f.setBounds(x, y, w, h);
f.setVisible(true);
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
}
最后一个子节点:将光标调整到父节点上
后有节点:将光标调整到后一节点上
后无节点:将光标调整到前一节点上
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
JTree tree = new JTree();
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
TreeSelectionModel selectionModel = tree.getSelectionModel();
JButton removeButton = new JButton("Remove selected node");
JButton addButton = new JButton("Add node");
public Test() {
Container contentPane = getContentPane();
selectionModel
.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(tree, BorderLayout.CENTER);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
TreePath path = e.getNewLeadSelectionPath();
boolean nodesAreSelected = (path != null);
addButton.setEnabled(nodesAreSelected);
removeButton.setEnabled(nodesAreSelected);
}
});
model.addTreeModelListener(new TreeModelListener() {
public void treeNodesInserted(TreeModelEvent e) {
showInsertionOrRemoval(e, " added to ");
}
public void treeNodesRemoved(TreeModelEvent e) {
showInsertionOrRemoval(e, " removed from ");
}
private void showInsertionOrRemoval(TreeModelEvent e, String s) {
Object[] parentPath = e.getPath();
int[] indexes = e.getChildIndices();
Object[] children = e.getChildren();
Object parent = parentPath[parentPath.length - 1];
JOptionPane.showMessageDialog(Test.this, "Node \""
+ children[0].toString() + "\"" + s + parent.toString()
+ " at index " + indexes[0], "Node Added or Removed",
JOptionPane.INFORMATION_MESSAGE);
}
public void treeNodesChanged(TreeModelEvent e) {
}
public void treeStructureChanged(TreeModelEvent e) {
}
});
}
class ControlPanel extends JPanel {
public ControlPanel() {
addButton.setEnabled(false);
removeButton.setEnabled(false);
add(addButton);
add(removeButton);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path = selectionModel.getSelectionPath();
MutableTreeNode parent, node = (MutableTreeNode) path
.getLastPathComponent();
if (path.getPathCount() > 1)
parent = (MutableTreeNode) node.getParent();
else
parent = (MutableTreeNode) node;
int index = parent.getIndex(node) + 1;
String s = JOptionPane.showInputDialog(Test.this,
"Enter a name for the new node:", "New Tree Node",
JOptionPane.QUESTION_MESSAGE);
MutableTreeNode newNode = new DefaultMutableTreeNode(s);
model.insertNodeInto(newNode, parent, index);
}
});
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path = selectionModel.getSelectionPath();
if (path.getPathCount() == 1) {
JOptionPane.showMessageDialog(ControlPanel.this,
"Can't remove root node!");
return;
}else if (path.getPathCount() == 2) {
JOptionPane.showMessageDialog(ControlPanel.this,
"Can't remove node!");
return;
}
MutableTreeNode node = (MutableTreeNode) path
.getLastPathComponent();
model.removeNodeFromParent(node);
}
});
}
}
public static void main(String args[]) {
GraphicJavaApplication.launch(new Test(), "Tree Model Example", 300,
300, 450, 300);
}
}
class GraphicJavaApplication extends WindowAdapter {
public static void launch(final JFrame f, String title, final int x,
final int y, final int w, int h) {
f.setTitle(title);
f.setBounds(x, y, w, h);
f.setVisible(true);
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
}
答!: 1:
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path = selectionModel.getSelectionPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) node.getParent();
TreePath newPath = null;
TreeNode nextNode = parentNode.getChildAfter(node);
TreeNode prevNode = parentNode.getChildBefore(node);
if (nextNode != null) {
newPath = path.getParentPath().pathByAddingChild(nextNode);
}
else if (prevNode != null) {
newPath = path.getParentPath().pathByAddingChild(prevNode);
}
else {
newPath = path.getParentPath();
}
if (path.getPathCount() == 1) {
JOptionPane.showMessageDialog(ControlPanel.this,
"Can't remove root node!");
return;
} else if (path.getPathCount() == 2) {
JOptionPane.showMessageDialog(ControlPanel.this,
"Can't remove node!");
return;
}
model.removeNodeFromParent(node);
tree.setSelectionPath(newPath);
}
});
public void actionPerformed(ActionEvent e) {
TreePath path = selectionModel.getSelectionPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) node.getParent();
TreePath newPath = null;
TreeNode nextNode = parentNode.getChildAfter(node);
TreeNode prevNode = parentNode.getChildBefore(node);
if (nextNode != null) {
newPath = path.getParentPath().pathByAddingChild(nextNode);
}
else if (prevNode != null) {
newPath = path.getParentPath().pathByAddingChild(prevNode);
}
else {
newPath = path.getParentPath();
}
if (path.getPathCount() == 1) {
JOptionPane.showMessageDialog(ControlPanel.this,
"Can't remove root node!");
return;
} else if (path.getPathCount() == 2) {
JOptionPane.showMessageDialog(ControlPanel.this,
"Can't remove node!");
return;
}
model.removeNodeFromParent(node);
tree.setSelectionPath(newPath);
}
});
相关JAVA教程:
DAO初学者问题
超链接如何加密?各位学长请指教
有没有人在netbeans platform上用过jfreechart,我在run module 的时候总是报NoClassDefFoundError,不知是
请问用hibernate新增一个记录后如何返回这条记录的自增ID?
利用Font设置文本字体的问题
有合肥的兄弟么,知道合肥凯捷么,这个公司怎么样?
在面板(JPanel)中创建能滚动列表框的问题,大家帮我看看
struts:基础问题!!!!各位大侠帮帮忙啊~!!
jms在jndi中的名字(sun one)
谁能解释一下webservice中那些乱七八糟的命名空间都有什么用?
关于下拉框选值的问题
求救关于ibatis的问题