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.

  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). (That code would need at least a `main(String[])` method to be an MCVE/SSCCE.) 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 3) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Jun 22 '16 at 01:52
  • Thanks, I will follow your recomendations in future. – Viacheslav Zhabonos Jun 22 '16 at 09:20

1 Answers1

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

Beniton Fernando
  • 1,533
  • 1
  • 14
  • 21