GridLayout Removing Padding between JPanels

Go To StackoverFlow.com

3

I'm working on a Computer Science project, and I haven't been able to find any solution to my problem. I'm creating a 2D array of JPanels containing images through a GridLayout. I want to remove the padding/margins between all the panels so that it flows seemlessly into a single image. However, the setHGap and setVGap methods don't seem to help me. I'd appreciate any response. Thank you.

import java.awt.GridLayout;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MapArray {

    static JPanel[][] tiles = new JPanel[11][11];

    public MapArray() {
    }

    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = Map.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setSize(325, 300);
                GridLayout layout = new GridLayout(10, 10);
                frame.setLayout(layout);

                for (int i = 0; i < 10; i++) {
                    for (int j = 0; j < 10; j++) {
                        tiles[i][j] = new JPanel();
                        tiles[i][j].add(new JLabel(
                            createImageIcon("tile-1.png")));
                        frame.add(tiles[i][j]);
                    }
                }

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                frame.validate();
                frame.repaint();
            }
        });
    }
}
2012-04-04 01:07
by chicagochillin
Remember each of your panels has padding als - Romain Hippeau 2012-04-04 01:21
Use pack(), not setSize() - trashgod 2012-04-04 01:25
How would I go about altering that padding - chicagochillin 2012-04-04 01:25
Also pack() didn't change anything - chicagochillin 2012-04-04 01:27


7

A few notes:

  • Give each panel a GridLayout.

  • Use the array length for loop limits.

  • Re-factor constants when possible.

Code:

import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MapArray {

    private static final int SIZE = 4;
    private static final JPanel[][] tiles = new JPanel[SIZE][SIZE];

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setLayout(new GridLayout(SIZE, SIZE));
                for (int i = 0; i < tiles.length; i++) {
                    for (int j = 0; j < tiles[0].length; j++) {
                        tiles[i][j] = new JPanel(new GridLayout());
                        tiles[i][j].add(new JLabel(
                            new ImageIcon("image.gif")));
                        frame.add(tiles[i][j]);
                    }
                }
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
2012-04-04 01:27
by trashgod
See also <code>ImageLabelPanel</code> - trashgod 2012-04-04 01:29
Thank you so much. This worked perfectly - chicagochillin 2012-04-04 01:58