How to add a JScrollPanel to previously added label components?

42 Views Asked by At

I'm having a little problem when trying to add JScrollPanel to previously added components with their bounds set specificaly in the for loop. The problem begins when I wanna add more than 15 components in my frame because I've explicitly set frame height and width so they won't be visible. Here is a code snippet:

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
        setContentPane(contentPane);
        setResizable(false);
        contentPane.setLayout(null);
        for(int i=0,j=0;i<questions.length;i++) {

            JLabel quest = new JLabel((i+1)+") "+questions[i][0]);
            quest.setBounds(185, 10+j, 200, 200);
            contentPane.add(quest);

            JLabel ans = new JLabel("Ans) "+answers[i][0]);
            ans.setBounds(190, 30+j, 200, 200);
            contentPane.add(ans);
            j=j+40;

        }

        Exit = new JButton("Exit");
        Exit.setBounds(270, 530, 89, 23);
        contentPane.add(Exit);
        Exit.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {

                System.exit(0);
            }
        });

The code from above consists of for loop where I dynamically add components to their preferred x, y location using setBounds and simple exit button. How can I add JLabel components (quest, ans) from loop to JScrollPanel but to keep their location as specified in for loop ?

Thanks guys for all the previous help :)

0

There are 0 best solutions below