How to bring the Image in Java GUI To a new line?

I am trying to make a simple gui window with images. Here's my code:

import java.awt.*;

import javax.swing.*;
public class Image extends JFrame
{
    private ImageIcon [] image = new ImageIcon[10];
    private JLabel [] label = new JLabel[10];

    Image()
    {
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();


        for(int i=0; i<10; i++)
        {
            for(int j = 0; j<10;j++)
            {
            image[i] = new ImageIcon(getClass().getResource((i+1) +".jpg"));
            label[i] = new JLabel (image[i]);
            c.fill = GridBagConstraints.REMAINDER;
            c.gridx = j;
            c.gridy = i;
            add(label[i]);
            }
        }
    }

    public static void main(String[] args)
    {
        Image i = new Image();
        i.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        i.setVisible(true);
        i.pack();
        i.setTitle("My Title");
    }

}

What I am trying to do is after the counter exits the inner loop, it should go on a new line in the window.

But i get the whole thing in the same line. Any suggestions?

Answers


You need to pass the constraints as a parameter when adding the labels:

add(label[i], c);

instead of

add(label[i]);

Not directly related to the problem, but you should create the GUI in the event dispatch thread; also, call setVisible(true) on the frame only after it is otherwise ready.


Need Your Help

Working with enums in C/C++

c++ c enums

I am working on an adaption of the code shown here, however, instead of using individual values I want to create an enum of possible values.

Silverstripe 3.1.2 - PaginatedList only for Dataobjects related to page

php silverstripe

I want to create a paginated news section with different categories.