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

RESTful modeling of button presses that invoke server side actions to a resource

web-applications rest

I'm embarking on a new web app we want do do it RESTfully. Now it is time to begin designing the interactions, and something sorta basic about REST has me stumped. I am trying to figure out the b...

How to implement polymorphic interface with generic custom collection

c# collections interface generics

ok - so what I want to accomplish is this: Build a poly interface making use of a generic collection that accepts one of three custom types (constrained through an interface).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.