JAVA中如何删除树中所选的节点?
发布网友
发布时间:2022-05-02 05:24
我来回答
共3个回答
热心网友
时间:2022-06-28 14:58
//获得商品分类树形结构
private JTree getTree(){
if(tree ==null)
{
tree = new JTree(getTreeModel());
tree.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
// tree = new JTable(getModel());
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
// 节点选择事件
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
treeSelection();
}
});
tree.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
if(e.getButton()==MouseEvent.BUTTON3)
{
int selRow = tree.getRowForLocation(e.getX(), e.getY());
tree.setSelectionRow(selRow);
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
if(selRow != -1) {
if(e.getClickCount() == 1) {
// myRightClick(selRow, selPath);
selectedNode = (DefaultMutableTreeNode) selPath.getLastPathComponent();
showPopMenu(selRow,selPath,e.getX(),e.getY());
}
}
}
}
});
}
return tree;
}
/**
* 获得当前选择的商品对象
* @return
*
*/
private Goods getSelectedGoods(){
int row = table.getSelectedRow();
if(row!=-1)
{
String code = (String)getModel().getValueAt(row,0);
Goods g= gHandle.getGoodsByCode(code);
return g;
}
return null;
}
/**
* 树形结构上显示右键菜单
* @param selRow
* @param selPath
*
*/
private void showPopMenu(int selRow, TreePath path,int x,int y) {
// tree.add(getPopMenu());
if (path == null)
return;
//点的是根结点
if(selectedNode.isRoot())
{
JPopupMenu pop = getPopMenu(1,path,selRow);
// System.out.println(selRow);
this.getContentPane().add(pop);
pop.show(tree,x,y);
}
//叶结点
if(selectedNode.isLeaf())
{
JPopupMenu pop = getPopMenu(0,path,selRow);
this.getContentPane().add(pop);
pop.show(tree,x,y);
}
}
/**
* 创建右键菜单
* @param action
* @return
*
*/
private JPopupMenu getPopMenu(int action,final TreePath path,final int selRow){
if(pop==null)
{
pop = new JPopupMenu();
//新建类别事件处理
addItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String value = JOptionPane.showInputDialog("请输入新类别的名称:");
if(value==null||value.trim().length()==0)
return;
GoodsCategoryHandle gcHandle = new GoodsCategoryHandle();
if(gcHandle.isExsited(value))
{
JOptionPane.showMessageDialog(null, "该类别名称已经存在!", "提示", JOptionPane.ERROR_MESSAGE);
return ;
}
DefaultMutableTreeNode newNode =new DefaultMutableTreeNode(value);
treeModel.insertNodeInto(newNode,(MutableTreeNode)treeModel.getRoot(),selRow-1);
GoodsCategory gc = new GoodsCategory();
gc.setCategoryName(value);
//更新到数据库中
gcHandle.addCategory(gc);
}
});
//重命名类别名称事件处理
renameItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String value = JOptionPane.showInputDialog(null,"请输入新类别的名称:",selectedNode.toString());
if(value==null||value.trim().length()==0)
return;
GoodsCategoryHandle gcHandle = new GoodsCategoryHandle();
if(gcHandle.isExsited(value))
{
JOptionPane.showMessageDialog(null, "该类别名称已经存在!", "提示", JOptionPane.ERROR_MESSAGE);
return ;
}
GoodsCategory gc =gcHandle.getCategory(selectedNode.toString());
gc.setCategoryName(value);
//更新到数据库中
gcHandle.modifyCategory(gc);
selectedNode.setUserObject(value);
tree.setModel(getTreeModel());
}
});
//移除结点事件处理
delItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int i =JOptionPane.showConfirmDialog(null,
"慎重!删除类别后,该类别下的所有商品将全部被删除,确认后继续", "提示", JOptionPane.YES_NO_OPTION);
if(i==JOptionPane.YES_OPTION)
{
GoodsCategoryHandle gcHandle = new GoodsCategoryHandle();
GoodsHandle gHandle = new GoodsHandle();
int id = (gcHandle.getCategory(selectedNode.toString().trim())).getCategoryId();
ArrayList al = gHandle.getGoodsByCategory(id);
boolean b_bak = false;
//对删除商品的备分
try {
FileWriter out = new FileWriter("/"+CommonUtil.getTime()+"商品删除bak.db",true);
out.write("商品名称"+" "+"商品分类"+" "+
"商品别名"+" "+"商品单位"+" "+
"商品厂商"+" "+"商品库存上限"+" "+
"商品库存下限"+" "+
"商品库存下限"+" "+
"商品参考售价"+" "+
"商品实际库存");
out.write('\n');
for(int j=0;j<al.size();j++)
{
Goods g = (Goods)al.get(j);
out.write(g.toString());
out.write('\n');
}
out.close();
b_bak=true;
} catch (IOException e1) {
e1.printStackTrace();
b_bak = false;
}
gcHandle.setCommit(false);
boolean g_del=gHandle.deleteByCategoryId(id);
boolean gc_del=gcHandle.deleteById(id);
//只有类别删除成功,该类别下的商品删除成功,并且备分成功后才提交删除
if(g_del&&gc_del&&b_bak)
gcHandle.commit();
else
{
// System.out.println("fail");
gcHandle.rollBack();
JOptionPane.showMessageDialog(null, "删除失败,可能是因为该类别下的商品存在销售记录,不能被删除!", "提示", JOptionPane.ERROR_MESSAGE);
}
tree.setModel(getTreeModel());
tree.setSelectionRow(0);
}
else
return;
}
});
pop.add(addItem);
pop.add(delItem);
pop.add(renameItem);
}
if(action ==1)
{
delItem.setEnabled(false);
renameItem.setEnabled(false);
}
if(action ==0)
{
delItem.setEnabled(true);
renameItem.setEnabled(true);
}
return pop;
}
/**
* 用户在商品分类树上的事件处理
*
*
*/
private void treeSelection(){
TreePath path = tree.getSelectionPath();
if (path == null)
return;
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
.getLastPathComponent();
selectedCategory = cHandle.getCategory(node.toString());
// 若选中的是根节点
if (node.isRoot())
showAllGoods();
if(node.isLeaf()){
model.setRowCount(0);
ArrayList al = gHandle.getGoodsByCategoryName(node
.toString());
for (int i = 0; i < al.size(); i++) {
Goods g = (Goods) al.get(i);
model.addRow(getRow(g));
}
}
}
public GoodsCategory getCategory(){
return this.selectedCategory;
}
/**
* 生产树形列表的数据模型
*
* @return
*
*/
private TreeModel getTreeModel() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode(
PropertiesUtil.getString("categoryTree.all"));
ArrayList categoryList = cHandle.getAllCategory();
// 添加子节点
for (int i = 0; i < categoryList.size(); i++) {
GoodsCategory gc = (GoodsCategory) categoryList.get(i);
String name = gc.getCategoryName();
DefaultMutableTreeNode node = new DefaultMutableTreeNode(name);
root.add(node);
}
// 生产treeModel
treeModel = new DefaultTreeModel(root);
return treeModel;
}
热心网友
时间:2022-06-28 14:59
javax.swing.tree.DefaultTreeModel.removeNodeFromParent(MutableTreeNode node)
用于从树模型中删除节点。
欲删除鼠标所选的节点
DefaultMutableTreeNode selectedNode =
(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (selectedNode != null && selectedNode.getParent() != null) {
model.removeNodeFromParent(selectedNode);
}
建议还是找本书看看吧,JTree用了MVC模式,还是从基础看起比较好,推荐CoreJava这本书的第二卷,里面有详细介绍。
热心网友
时间:2022-06-28 14:59
在这里写不太方便,请发消息告知我邮箱,我可以做个示例给你传过去,OK?