views:

235

answers:

1

How should I use a linked list with my GUI program? The program is supposed to have blocks (a block being a JPanel) with textareas and dropdown menus. The number of blocks depends on the number of times the user presses a button. It's supposed to be possible to put blocks within blocks. I want to store the information created, using a linked list and then saving to a file. I'm thinking, for the sub blocks, there would be linked lists within the linked list. Should I use Java.util.LinkedList? How do I add the information? A Node class? Should that be in a separate file? I started to try sitting up a linked list, but it's getting me confused.

I'm still unsure about how a person is supposed to make GUI's. I've only seen really simple GUI's. Can anyone tell me how I should arrange things? At the moment, I have three files, one for the main GUI window, one for the question blocks to be inserted within that, and one for some tools to use within those two files to make the code easier to understand. But, there seems to be something wrong with the question block file, because I've been making the background white for each JPanel, and there's a gray outline around the question blocks when I run the program.

I probably shouldn't paste a ton of code here... These are my files: http://asj127.webs.com/BuildAssessmentWindow.java http://asj127.webs.com/QuestionBlock.java http://asj127.webs.com/JPanelTools.java

A: 

I think you should separate the data model from the gui, and yes, add a list (LinkedList or ArrayList) to the ListNode1 class:

public class ListNode1 implements java.io.Serializable {
    private String num;
    private String questionText;
    private String answerText;
    private List<ListNode1> subNodes = new LinkedList<ListNode>();
...
    public List<ListNode1> getSubNodes() {
        return subNodes;
    }

    public void addSubNode(ListNode1 subNode) {
        subNodes.add(subNode);
    }

    public void removeSubNode(ListNode1 subNode) {
        subNodes.remove(subNode);
    }
}

Note that I have made the class serializable so that you can easily save it to or read it from a file.

This structure is in fact an n-ary tree (or forest).

At the gui level, using a list would be redundant because a Container such as JPanel already contains the list of all the components it contains, So you can simply use a JPanel to hold the QuestionBlock's of all the sub-nodes.

I would link a QuestionBlock to a ListNode1 instead of a list:

public class QuestionBlock extends JPanel {
    private JPanel container;
    private JComboBox numberDropDown, answerDropDown;
    private JTextArea question;
    private ListNode1 node;

    public QuestionBlock(ListNode1 node)
    {
        super(new FlowLayout());
        //setLocation(new Point(0, 0));
        setVisible(true);
        //setSize(800, 610);
        //setBackground(new Color(0, 0, 0));
        this.node=node;
        createGUI();
    }

    public void createGUI()
    {
        container = new JPanel();
        container.setLayout(new BorderLayout());

        numberDropDown = new JComboBox();
        numberDropDown.addItem("1.");
        numberDropDown.addItem("a.");
        container.add(panelTools.addThisComponent(numberDropDown), BorderLayout.LINE_START);

        questionsBlock = panelTools.addNewJPanel();
        questionsBlock.setLayout(new BorderLayout());

        question = new JTextArea(4, 30);
        question.setText(node.getQuestionText);
        question.setBackground(new Color(250, 250, 250)); //light gray color for noticeability
        questionsBlock.add(panelTools.addThisComponent(question), BorderLayout.PAGE_START);

        answerDropDown = new JComboBox();
        answerDropDown.addItem("--Select--");
        answerDropDown.addItem("SubQuestion");
        questionsBlock.add(panelTools.addThisComponent(answerDropDown), BorderLayout.PAGE_END);

        container.add(questionsBlock, BorderLayout.CENTER);
        add(container);
        for (ListNode1 subNode: node.getSubNodes()) {
           add(new QuestionBlock());
        }
    }
}

You would also need a method to save the answers:

public void saveAnswers() {
    node.setAnswerText((String)answerDropDown.getSelectedItem());
    for (Component comp: getComponents()) {
        if (comp instanceof QuestionBlock) {
            ((QuestionBlock)comp).saveAnswers();
        }
    }
}
Maurice Perry
I'm confused by this part: private List<ListNode1> subNodes = new LinkedList<ListNode>();"At the gui level, using a list would be redundant because a Container such as JPanel already contains the list of all the components it contains, So you can simply use a JPanel to hold the QuestionBlock's of all the sub-nodes." So... (thinks about it for awhile) what does this mean?
Asj
A QuestionBlock is a JPanel that contains a JPanel for the JTextArea and the JComboBox, followed by the QuestionBlock's of all the sub-nodes. getComponents() gives you all that (see method saveAnswers).
Maurice Perry