tags:

views:

39

answers:

1

Hi. Im developing a system that stores courses that participants can apply to. I'm presenting the enrollments in a JTree in the courseadministratorGUI.

My problem is that, for every enrollment it's adding a new courseNode.

Been trying for many hours, and hope I can now get some advice that will point me in the correct direction. Thank you.

    private void updateJTree() {
    for (Category cat : catcontrol.getAllCategoriesList()) {

        category = new DefaultMutableTreeNode(cat);


        for (Course c : ccontrol.getAllCourses()) {

            if (cat.getIdCategory() == c.getIdCategory()) {
                for (Enrollment e : econtrol.getAllEnrollments()) {
                    if (e.getIdCourse() == c.getIdCourse()) {
                        if (cat.getIdCategory() == c.getIdCategory() && e.getCourse().equals(c)) {
                            root.add(category);
                        }

                        if (c.getIdCourse() == e.getIdCourse()) {

                            course = new DefaultMutableTreeNode(c);
                            category.add(course);
                            enrollment = new DefaultMutableTreeNode(e.getParticipant().getFirstNames());
                            course.add(enrollment);

                        }
                    }
                }
            }
        }
    }

    jTree1.setModel(new DefaultTreeModel(root));
    jTree1.addTreeSelectionListener(this);

    jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
}
+1  A: 

I am not sure I understand the question, but... why "for every enrollment it's adding a new courseNode"? Because you're telling it to. Right here:

course = new DefaultMutableTreeNode(c);
    category.add(course);
enrollment = new DefaultMutableTreeNode(e.getParticipant().getFirstNames());
    course.add(enrollment);

Add course, then add an enrollment. Always in pairs. Now I don't know the structure of your data, but I dare say that is not what you want. Can you describe in more detail what Enrollment is (one person? list of people? something else?), and what getParticipant() returns, and what do you want to appear in the tree?

One more comment - you do realise that these two if statements check the same thing, right? You can get rid of the inner one, since it's not doing anything. Or, more likely, rewrite to do something useful (however, as I said, I am not too sure what it is you want to do).

if (e.getIdCourse() == c.getIdCourse()) {
    // ...
    if (c.getIdCourse() == e.getIdCourse()) {
Amadan