1

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.

2

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

Yep, i missed adding components to the panel... Thanks a lot :)
@ViacheslavZhabonos Great. Hope you would accept my answer. ;-)
I just pressed "accept" button... Do you see some changes? Is it a correct button?
P.S. But i'm still curious. Is my method to build GUI correct? Should i keep it?
I think it fine. I may not able to tell that exactly without knowing what you are trying to accomplish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.