0
任何人都可以幫助我使用我的代碼! 我在ArrayLists中有一些數據,我將它顯示爲一個JTree。我希望能夠編輯我的數據並將其保存在我的容器中。現在,視覺部分完美無缺,但是我的ArrayList內部沒有任何變化。我怎樣才能達到我的目標?更改JTree中的節點
這是我的JTree看起來像:
public TreeNode makeTree()
{
ArrayList <Factory> factories = myparser.parseXml();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Factories");
for (int i=0; i<factories.size(); ++i)
{
Factory factory = factories.get(i);
DefaultMutableTreeNode factory1 = new DefaultMutableTreeNode(factory.getName());
root.add(factory1);
for (int j=0; j<factory.getDepartments().size(); ++j)
{
Department department = factory.getDepartments().get(j);
DefaultMutableTreeNode department1 = new DefaultMutableTreeNode(department.getName());
factory1.add(department1);
for (int k=0; k<department.getSections().size(); ++k)
{
Section section = department.getSections().get(k);
DefaultMutableTreeNode section1 = new DefaultMutableTreeNode(section.getName());
department1.add(section1);
for (int m=0; m<section.getProducts().size(); ++m)
{
Product product = section.getProducts().get(m);
DefaultMutableTreeNode product1 = new DefaultMutableTreeNode(product.getId());
section1.add(product1);
DefaultMutableTreeNode product1_amount = new DefaultMutableTreeNode(product.getAmount());
product1.add(product1_amount);
}
}
}
}
return root;
}
這裏是我的ActionListener:
JButton addChildButton = new JButton("Add Child");
addChildButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
DefaultMutableTreeNode selectedNode
= (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();
if (selectedNode == null) return;
DefaultMutableTreeNode newNode
= new DefaultMutableTreeNode("New");
model.insertNodeInto(newNode, selectedNode,
selectedNode.getChildCount());
// now display new node
TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
tree.scrollPathToVisible(path);
}
});
這裏是我對巫類arralist是基於:
import java.util.ArrayList;
public class Factory {
private String name;
private ArrayList <Department> departments;
public Factory(String name, ArrayList<Department> departments)
{this.name = name; this.departments = departments;}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
public void addDepartment(Department department)
{
departments.add(department);
}
public ArrayList<Department> getDepartments()
{
return this.departments;
}
public void setDepartments(ArrayList<Department> departments)
{
this.departments = departments;
}
}
import java.util.ArrayList;
public class Department {
private String name;
private ArrayList <Section> sections;
public Department(String name, ArrayList<Section> sections)
{this.name = name; this.sections = sections;}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
public void addSection(Section section)
{
sections.add(section);
}
public ArrayList<Section> getSections()
{
return this.sections;
}
public void setSections(ArrayList<Section> sections)
{
this.sections = sections;
}
}
import java.util.ArrayList;
public class Section {
private String name;
private ArrayList<Product> products;
public Section(String name, ArrayList<Product> products)
{this.name = name; this.products = products;}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
public void addProduct(Product product)
{
products.add(product);
}
public ArrayList<Product> getProducts()
{
return this.products;
}
public void setProducts(ArrayList<Product> products)
{
this.products = products;
}
}
public class Product {
private String id;
private int amount;
public Product (String id, int amount){this.id = id; this.amount = amount;}
public String getId()
{
return this.id;
}
public void setId(String id)
{
this.id = id;
}
public int getAmount()
{
return this.amount;
}
public void setAmount(int amount)
{
this.amount = amount;
}
}
我認爲你必須尋找適當的通知程序,沒有別的沒有SSCCE/MCVE,簡短,可運行,可編譯爲局部變量模型的硬編碼值,或通過點擊至jtree標籤進行搜索 – mKorbel