JTree'dan tugunlarni qo'shish va o'chirish

Juda oddiy JTree bor. Men shoshilgandek, agar kerak bo'lmasa TreeModel dan foydalanmaslikni afzal ko'raman. Muammoni aniqlash uchun SSCCE yozdim:

Ba'zan tugunni qo'shaman. Boshqa paytlarda men ularni olib tashlayman. Add ni bosganimda, tugun to'g'ri qo'shilgan. Remove tugmasini bosgandan so'ng, tugunni olib tashlash kerak, lekin u ishlamaydi. Bundan tashqari, agar bir nechta tugunni qo'shishga harakat qilsam, daraxt qo'shgan birinchi tugma bilan qoladi.

JTree uchun yangilash usulini yozdim, u erda ildiz tugunidan osilgan barcha tugunlarni o'chiraman va so'ngra yaratmoqchi bo'lgan tugun va pastki tugunlarga qarayman.

Men daraxtga ishlov berish uchun TreeModel dan foydalanmasdan tashqari bu erda noto'g'ri ish qilyapmanmi?

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
19
"Tezda" bo'lish sizni sekinlashtirishi mumkin; modelni ishlatib, @camickrning fikricha, to'g'ri yondashuv. +1, btw, to'liq misol uchun.
qo'shib qo'ydi muallif trashgod, manba
shoshilinch yoki yo'q bo'lsa, siz uning qismlari ostida tugunlarni o'zgartirsangiz, siz modelni xabardor qilish uchun ega bo'lasiz. Hech qanday yo'l yo'q. @camickr sizni :-) sharhlarim bilan qanday qilib bezatilganligini ko'rsatadi
qo'shib qo'ydi muallif kleopatra, manba

1 javoblar

Nima uchun siz barcha tugunlarni o'chirib tashlayotganingizni bilmayman.

Yangilanish har doim model orqali amalga oshirilishi kerak. Sizda bir nechta variant bor:

Modelni to'g'ridan-to'g'ri yangilash:

DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
model.insertNodeInto(new DefaultMutableTreeNode("another_child"), root, root.getChildCount());

Daraxt tugunlarini yangilang va so'ngra modeli xabardor qiling:

DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
root.add(new DefaultMutableTreeNode("another_child"));
model.reload(root);

Xuddi shu narsa tugunlarni olib tashlash uchun ham qo'llaniladi.

DefaultTreeModel modelni to'g'ridan-to'g'ri yangilaydigan removeNodeFromParent (...) ga ega.

Yoki DefaultMutableTreeNode sinfidagi olib tashlash (...) usulidan foydalanishingiz mumkin. Bu holatda siz qayta yuklashni amalga oshirishingiz kerak ().

46
qo'shib qo'ydi
Bolaning qiymati qanday o'chiriladi?
qo'shib qo'ydi muallif Nitesh Verma, manba
Ko'p rahmat, men shu yo'lni chindan ham murakkablashtirardim
qo'shib qo'ydi muallif Roman Rdgz, manba
Rahmat. JTree bilan ko'proq o'ynashim kerak :)
qo'shib qo'ydi muallif camickr, manba
+1 - qayta yuklanish uchun ;-) Modeldagi tugunlar mavjud, lekin u erda tinglovchilarni xabardor qilish uchun noqonuniy nomlar haqida ma'lumot yo'q. Yangilash - oxirgi o'lchov, f.i. daraxtning bir necha chuqurliklarida murakkab o'zgarishlar kiritilgandan so'ng
qo'shib qo'ydi muallif kleopatra, manba
tushuntirish uchun; nodeChanged() usuli tugunlarni olib tashlashda buggy xatti-harakatiga sabab bo'ladi, lekin reload() ishlaydi. Buning sababi nodeChanged() daraxtlarni daraltmadığından, reload() amalga oshiradi.
qo'shib qo'ydi muallif glend, manba