java tree类子项的添加和删除_java – 从JTree中添加和删除节点

我有一个非常基本的JTree。当我很忙时,如果不需要,我不喜欢使用TreeModel。我写了一个SSCCE来揭露这个问题:

有时我添加一个节点。其他时候我删除它们。当我按下Add,一个节点被正确添加。当我删除,应该删除节点,但它不。另外,如果我尝试添加多个节点,树只保留我添加的第一个节点。

我为JTree写了一个更新方法,其中我首先擦除从根节点挂起的所有节点,然后查看我必须创建哪些节点和子节点。

我在这里做错了什么,除了不使用TreeModel来操作树?

import java.awt.Dimension;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ArrayList;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTree;

import javax.swing.tree.DefaultMutableTreeNode;

public class TreeTest {

private JFrame myFrame;

private JTree myTree;

private JButton addButton, removeButton;

private int numberOfNodes;

private DefaultMutableTreeNode rootNode;

private ArrayList graphicIDS;

private ArrayList graphicInfo;

public static void main (String [ ] args){

new TreeTest();

}

public TreeTest() {

graphicIDS = new ArrayList();

numberOfNodes = 0;

graphicInfo = new ArrayList();

graphicInfo.add("Info A");

graphicInfo.add("Info B");

graphicInfo.add("Info C");

graphicInfo.add("Info D");

JPanel panel = new JPanel(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();

myFrame = new JFrame("JTree test");

myFrame.setResizable(false);

myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

c.fill = GridBagConstraints.BOTH;

c.anchor = GridBagConstraints.NORTH;

c.gridx = 0;

c.gridy = 0;

c.gridwidth = 2;

c.insets = new Insets(5,5,5,5);

rootNode = new DefaultMutableTreeNode("Root node");

myTree = new JTree(rootNode);

myTree.setPreferredSize(new Dimension(200, 500));

panel.add(myTree, c);

c.gridwidth = 1;

c.gridy++;

removeButton = new JButton("Remove");

removeButton.setEnabled(false);

removeButton.addActionListener(new ActionListener (){

@Override

public void actionPerformed(ActionEvent e) {

System.out.println("Removed curve "+(graphicIDS.size()));

graphicIDS.remove(graphicIDS.size()-1);

numberOfNodes--;

updateMeasurementsTree();

}

});

panel.add(removeButton, c);

c.gridx++;

addButton = new JButton("Add");

addButton.addActionListener(new ActionListener (){

@Override

public void actionPerformed(ActionEvent e) {

graphicIDS.add("Curve "+(numberOfNodes+1));

System.out.println("Added curve "+(numberOfNodes+1));

numberOfNodes++;

updateMeasurementsTree();

}

});

panel.add(addButton, c);

myFrame.getContentPane().add(panel);

myFrame.pack();

myFrame.setVisible(true);

}

public void updateMeasurementsTree(){

rootNode.removeAllChildren();

for(int i=0; i

String idString = graphicIDS.get(i);

DefaultMutableTreeNode idNode = new DefaultMutableTreeNode("Graphic "+idString);

rootNode.add(idNode);

int randomValue = (int) Math.floor(Math.random()*graphicInfo.size());

String infoString = graphicInfo.get(randomValue);

DefaultMutableTreeNode infoNode = new DefaultMutableTreeNode("Info "+infoString);

idNode.add(infoNode);

}

if(numberOfNodes==0) removeButton.setEnabled(false);

else{

removeButton.setEnabled(true);

expandAll();

}

}

public void expandAll() {

int row = 0;

while (row < myTree.getRowCount()) {

myTree.expandRow(row);

row++;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值