• Post Reply Bookmark Topic Watch Topic
  • New Topic

CardLayout- how to deal with it?

 
Stefan Elfvinge
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I have a class extending JPanel(ScoreBoard) acting as a scoreboard for a game, ScoreBoard
contains 5 Jpanels, each containing a JTable.
ScoreBoard uses a CardLayout. Adding an instance of ScoreBoard to my JFrame(GameArea) doesn't work.
It compiles, runs, showing all others components(using Flow- or BorederLayout). But ScoreBoard remains invisible!!!??? If I change ScoreBoard's
LayoutManager to something other than CardLayout, it works!!?? What to do...
 
kallam reddy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
you put one empty panel in jFrame(game) and set cardlayout to that, then add Jpanel(scoreboard) to that panel.then it works fine .
 
Stefan Elfvinge
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kallam reddy:
hi
you put one empty panel in jFrame(game) and set cardlayout to that, then add Jpanel(scoreboard) to that panel.then it works fine .

Thanks for instant reply!
Do u mean that my mistake is that I first build my
ScoreBoard(JPanel), then add it to the JFrame?
From your advise I take it I should add an additional JPanel, giving it a CardLayout, and THEN, add my ScoreBoard instance to that. Why is this "extra" panel necessary?
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan, could u post some of the code? to show us the panel and how u add it.
from what i know, i dont think u need the extra panel. the idea should work.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you doing the panel.show() for the cardlayout?
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, i think u dont have to do the show().
if im not mistaken, the card layout will show the FIRST component added to it unless u use next() show() or any of the other functions.
 
Stefan Elfvinge
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for input, here's some code:
private GameController controller
private boolean[] markedValues;
private int summa;
private int bonus;
public ScoreBoard(GameController controller)
{
super(new CardLayout());
setBorder(new EtchedBorder());
this.controller = controller;
markedValues = new boolean[19];
for(int i = 0; i<markedValues.length; i++)
markedValues[i] = false;
}

// To be called from “GameController”
public void addScoreBoardText(DicePlayer player)
{
String name = player.getName();
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(new TitledBorder(new EtchedBorder(),
name + " scoreboard"));
JTable scoreBoardText = new JTable(19, 2);
setupScoreBoardText(scoreBoardText);
panel.setBackground(Color.lightGray);
panel.add(scoreBoardText, BorderLayout.CENTER);
// L�gg texten i panelens container
CardLayout cL = (CardLayout)this.getLayout();
cL.addLayoutComponent(panel, name);
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!