Skip to main content

JFrame with a GridLayout of JPanel

2 replies [Last post]
paripaolo
Offline
Joined: 2012-01-09
Points: 0

Hi,

I'm trying to build a JFrame with a GridLayout and a set of JPanels with a ImageIcon in each of them as background.
I don't understand why the image doesn't cover all the surface of the JPanel showing a grey line on the top. Note that with colors it works!
Here the code:

import javax.swing.*;
import java.awt.*;
import java.awt.Color;

public class GridLayoutExample{

public JPanel createContentPane (){

JPanel totalGUI = new JPanel();

// We create a JPanel with the GridLayout.
JPanel mainPanel = new JPanel(new GridLayout(2, 2, 0, 0));

/*JPanel red = createSquareJPanel(Color.red, 100);
JPanel blue = createSquareJPanel(Color.blue, 100);
JPanel green = createSquareJPanel(Color.green, 100);
JPanel orange = createSquareJPanel(Color.orange, 100);*/

JPanel red = createImageJPanel(100);
JPanel blue = createImageJPanel(100);
JPanel green = createImageJPanel(100);
JPanel orange = createImageJPanel(100);

mainPanel.add(red);
mainPanel.add(blue);
mainPanel.add(green);
mainPanel.add(orange);

totalGUI.add(mainPanel);
totalGUI.setOpaque(true);
return totalGUI;
}

// In this method, we create a square JPanel of a colour and set size
// specified by the arguments.

private JPanel createSquareJPanel(Color color, int size)
{
JPanel tempPanel = new JPanel();
tempPanel.setBackground(color);
tempPanel.setMinimumSize(new Dimension(size, size));
tempPanel.setMaximumSize(new Dimension(size, size));
tempPanel.setPreferredSize(new Dimension(size, size));
return tempPanel;
}

private JPanel createImageJPanel(int size)
{
ImageIcon icon = new ImageIcon("/media/sharedDisk/paolo/SacroCuoreConnection/Applications/Eclipse/WumpusMind/multimediaFiles/images/foots/foots1.jpeg");
JPanel tempPanel = new JPanel(true);
Image image = icon.getImage();
Image newImg = image.getScaledInstance(size, size, Image.SCALE_DEFAULT);
icon.setImage(newImg);
JLabel pic = new JLabel(icon);
tempPanel.setBackground(Color.red);
tempPanel.add(pic);
tempPanel.validate();
tempPanel.add(pic);
return tempPanel;
}

private static void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("[=] GridLayout [=]");
frame.setSize(900,900);

GridLayoutExample demo = new GridLayoutExample();
frame.setContentPane(demo.createContentPane());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kschaefe
Offline
Joined: 2006-06-08
Points: 0

This is not the correct fourm.  Please use the general Swing/AWT forum.

Karl

paripaolo
Offline
Joined: 2012-01-09
Points: 0

Done.

Thanks