I’m extending a JPanel to display a game board, and adding a JEditorPane at the bottom to hold some status text. Unfortunately, the game board renders just fine, but the JEditorPane is just a blank gray area until I highlight the text in it, when it will render whatever text is highlighted (but not the rest). If I’m understanding Swing right, it should work, because super.paintComponent(g) should render the other children (i.e., the JEditorPane). Tell me, o great stackoverflow, what bonehead mistake am I making?
public GameMap extends JPanel {
public GameMap() {
JEditorPane statusLines = new JEditorPane("text/plain","Stuff");
this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
this.add(new Box.Filler(/*enough room to draw my game board*/));
this.add(statusLines);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
for ( all rows ){
for (all columns){
//paint one tile
}
}
}
}
I don’t see anything immediately boneheaded about your code in general, but I would say that your component hierarchy seems a bit boneheaded.
Is there a reason why you aren’t separating your objects out better? In order to keep your code maintainable and testable, I’d encourage you to extract
GameBoardlogic into a different class. This would give you the ability to do simplify yourGameMapby removing thepaintComponent(...)And then your
GameBoardmight look like