java - JPanel not appearing when added to parent JPanel -


i'm dumbfounded here. have jpanel (defboardpanel) i'm adding parent jpanel (gamepanel) follows:

  public gamepanel(setupboard sb) {      this.setlayout(new borderlayout());   //  this.setbackground(color.yellow);     jpanel defboardpanel = new jpanel();     defboardpanel.setbackground(color.yellow);      (int r = 0; r < sb.boardsize; r++){         (int c = 0; c <  sb.boardsize; c++){             cell c = new cell(r, c);                    c.label.setopaque(true);             if (sb.getcell(r, c).status == sb.getcell(r,c).status.occupied){                 c.label.setbackground(color.black);                 system.out.println("label occupied");             }             else {                 c.label.setbackground(color.white);             }             defboardpanel.add(c.label);         }     }      defboardpanel.revalidate();     defboardpanel.setvisible(true);     this.add(defboardpanel, borderlayout.west);     this.revalidate();     this.setvisible(true); 

this panel added jframe (mainframe), shown below. when application launched, jframe displays different type of panel (setupboard), user sets game board. when click "accept", startgame() method of mainframe called, should show jpanels above.

public mainframe() {     this.setlayout(new borderlayout());     this.setsize(500, 500);      setupboard sb = new setupboard(10, this);     this.setcontentpane(sb);   }      void startgame(setupboard sb){         gamepanel gp = new gamepanel(sb);         this.setcontentpane(gp);         this.revalidate();  } 

my issue child panel (defboardpanel) not displaying. gamepanel displays (which i've verified using setbackground(color.yellow) method see commented out), not panel i've added onto it.

what stupid mistake overlooking here?

edit: startgame() being called within setupboard class:

void startgame(){     mf.startgame(this); } 

where mf reference mainframe created setupboard instance. fact gamepanel displays @ confirms being called correctly.

seems work ok if trim code don't have. issue coming not showing us. therefore, producing sscce benefit you. meanwhile, can take advantage (to find differences code) of following one, highly originated yours (i filled gaps could):

import java.awt.borderlayout; import java.awt.color; import java.awt.gridlayout;  import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.swingutilities;  public class gamepanel extends jpanel {     private static final int cols = 10;     private static final int rows = 10;      public gamepanel() {         this.setlayout(new borderlayout());         // this.setbackground(color.yellow);         jpanel defboardpanel = new jpanel(new gridlayout(rows, cols));         defboardpanel.setbackground(color.yellow);         (int r = 0; r < rows; r++) {             (int c = 0; c < cols; c++) {                 jlabel label = new jlabel((r + 1) + " " + (c + 1));                 label.setopaque(true);                 if (math.random() > 0.5) {                     label.setbackground(color.black);                     label.setforeground(color.white);                     system.out.println("label occupied");                 } else {                     label.setbackground(color.white);                 }                 defboardpanel.add(label);             }         }         this.add(defboardpanel, borderlayout.west);     }      public static class mainframe extends jframe {         public mainframe() {             this.setlayout(new borderlayout());             this.setsize(500, 500);         }          void startgame() {             gamepanel gp = new gamepanel();             this.setcontentpane(gp);             pack();             setvisible(true);         }     }      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {             @override             public void run() {                 mainframe mainframe = new mainframe();                 mainframe.startgame();             }         });     } } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -