Viacheslav Zhabonos
Viacheslav Zhabonos

Reputation: 190

Building GUI with ArrayList<JPanel>

I am developing application in Java for testing (for purposes of study). Spent half of the day until figured connecting to the Firebird DB. Now I have a problem when building GUI. Here is my code:

    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.util.*;

public class ExamSettingsWindow extends JFrame {
    JDialog examSettingsWindow = new JDialog(MainWindow.mainWindow, "Редагування екзамену", true);
    JPanel mainPanel = new JPanel();

    ArrayList<Question> questionsList = new ArrayList<Question>(Question.getQuestions());
    ArrayList<JPanel> qaPanels = new ArrayList<JPanel>();



    ExamSettingsWindow() {
        for(Question q : questionsList)
        {  // Some weird shit happenning in this loop, can't explain this, but i'm sure it will work someday...
            JLabel qID = new JLabel(String.valueOf(q.getID()));
            qID.setBorder(new EmptyBorder(2,2,2,2));
            JLabel qText = new JLabel(q.getText());
            qText.setBorder(new EmptyBorder(2,2,2,2));
            JPanel qPanel = new JPanel();
            qPanel.setLayout(new BoxLayout(qPanel, BoxLayout.X_AXIS));
            qPanel.setBorder(new EmptyBorder(5,5,5,5));
            qPanel.setMinimumSize(new Dimension(500,30));
            qPanel.add(qID);
            qPanel.add(qText);

            JPanel aPanels = new JPanel();
            aPanels.setLayout(new BoxLayout(aPanels, BoxLayout.Y_AXIS));
            aPanels.setBorder(new EmptyBorder(5,5,5,5));
            aPanels.setMinimumSize(new Dimension(500,30));

            for (Question.Answer a : q.answersList) {
                JLabel aID = new JLabel(a.getID());
                aID.setBorder(new EmptyBorder(2,2,2,2));
                JLabel aText = new JLabel(a.getText());
                aText.setBorder(new EmptyBorder(2,2,2,2));
                JPanel aPanel = new JPanel();
                aPanel.setMinimumSize(new Dimension(500,30));
                aPanel.setLayout(new BoxLayout(aPanel, BoxLayout.X_AXIS));
                aPanel.add(aID);
                aPanel.add(aText);

                aPanels.add(aPanel);
            }

            JPanel qaPanel = new JPanel();
            qaPanel.setMinimumSize(new Dimension(500,200));
            qaPanel.setLayout(new BoxLayout(qaPanel, BoxLayout.Y_AXIS));
            qaPanel.setBorder(new TitledBorder(new LineBorder(Color.black, 2),
                    "Питання "+String.valueOf(q.getID())));

            qaPanels.add(qaPanel);
        }

        examSettingsWindow.setMinimumSize(new Dimension(500, 500));
        examSettingsWindow.setMaximumSize(new Dimension(500, 500));
        examSettingsWindow.setResizable(false);
        examSettingsWindow.setDefaultCloseOperation(HIDE_ON_CLOSE);
        examSettingsWindow.setLocationRelativeTo(null);

        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setMinimumSize(new Dimension(500,500));
        for (JPanel p : qaPanels) {
            mainPanel.add(p);
        }
        JScrollPane sc = new JScrollPane(mainPanel);
        sc.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        examSettingsWindow.add(mainPanel);
    }

    public void initExamSettingsFrame(boolean vision) {
        examSettingsWindow.setVisible(vision);
    }
}

Here is picture of what are displayed after running th program: >>click<<

I think problem might be in ArrayList which contains JPanel with equal names, but i'm not sure and my head is blowing up when i'm searching for reasons and solution. Please help... Give me the right direction...

P.S. My code isn't perfect, don't blame me for this. I just trying to study Java.

Upvotes: 1

Views: 82

Answers (1)

Beniton Fernando
Beniton Fernando

Reputation: 1533

You may have add the question panel and answer panel to qaPanel

qaPanel.add(qPanel);
qaPanel.add(aPanels);

Then UI displayed the questions and answers.

One more change i did was changing the layout of mainPanel from BoxLayout to GridLayout. which gave me below result.

mainPanel.setLayout(new GridLayout(qaPanels.size(),1));//Optional change

enter image description here

Upvotes: 3

Related Questions