0

public static JPanel[][] creationGrille(JPanel content) throws IOException{

    ImageIcon monImage = new ImageIcon("src\\images\\tree.png");
    JLabel imageLab = new JLabel();
    imageLab.setIcon(monImage);
     //JPanel affichage Grille
    JPanel contentGrille = new JPanel();
    //On définit le layout à utiliser pour la grille
    contentGrille.setLayout(new GridLayout(14, 14));//14 lignes, 14 colonnes
    content.add(contentGrille,BorderLayout.CENTER);
    JPanel cell[][]= new JPanel[14][14];
    for(int i=0; i<cell.length; i++){
        for(int j=0; j<cell.length; j++){
            cell[i][j]= new JPanel();
            cell[i][j].setSize(new Dimension(50, 50));
            if (i == 5 || i ==6 || i==7 || i==8 || j==5 || j==6 || j==7 || j==8) {
                cell[i][j].setBackground(Color.gray);
                 if((i<5 && j==6) || (i>8 && j==6)){
                    cell[i][j].setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.white));  // top, left,bottom,right, color
                 }else if((i==6 && j<5) || (i==6 && j>8)){
                    cell[i][j].setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.white));
                 }
            }else{
                cell[i][j].add(imageLab);
                //cell[i][j].setBackground(Color.white);
            }


            contentGrille.add(cell[i][j]);
        }
    }
    return cell;
} 

i take each cell and i used my imageIcon to dipaly it in the cell ? ?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    "my code is not working" Expected behaviour vs. actual? The _error message_? Please put some effort into clarifying your question to make it easier for us to answer... – bcsb1001 Feb 27 '16 at 20:27
  • there is no error message.... my panel contentGrille is 14*14 cells and i want to display the picture tree.png in some cells not in all the contentGrille – MOUNAIME ILYAS Feb 27 '16 at 20:37
  • A component can only belong to a single parent/container,so you will need to create a new JLabel for each panel you want to add the image to – MadProgrammer Feb 27 '16 at 21:12
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Feb 28 '16 at 04:26

1 Answers1

0

A component can only belong to a single parent/container,so you will need to create a new JLabel for each panel you want to add the image to

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • i declared : JLabel imageLab[][] = new JLabel[14][14]; and after i added this to my else condition : imageLab[i][j] = new JLabel(); imageLab[i][j].setIcon(monImage); cell[i][j].add(imageLab[i][j]); but is not working !! – MOUNAIME ILYAS Feb 27 '16 at 21:29
  • `cell[i][j].add(new JLabel(monImage));` – MadProgrammer Feb 27 '16 at 21:30