-1

Is there a way for me to add buttons to the right side of my JLayeredPane? The layered pane contains a JPanel which represents a chess board and on top of this board I have JLabels representing chess pieces. I want basically another panel attached to the right side of the board which contains player information and buttons allowing for rematches/etc. What would be the best way to go about adding this panel?

Here is a snippet of my code. The part which sets up the board panel inside the layeredpane:

private void setupBoard() {
    paneX = new JLayeredPane();
    getContentPane().add(paneX);
    paneX.setPreferredSize(new Dimension(500,500));

    boardX = new JPanel(); 
    paneX.add(boardX, JLayeredPane.DEFAULT_LAYER);
    boardX.setLayout(new GridLayout(8,8));
    boardX.setBounds(0,0,500,500);
    chessBoard.setPreferredSize(new Dimension(500,500));
}

Then, I go on to add the jlabels to each component on the panel. I want to add another big panel attached to the right side of the board like I mentioned earlier.

femtoRgon
  • 32,893
  • 7
  • 60
  • 87
SKLAK
  • 3,825
  • 9
  • 33
  • 57

1 Answers1

2

Can't quite see why you would use a JLayeredPane for this, but that's just me.

Set the Layout for the main container to BorderLayout. Add the boardX to the BorderLayout.CENTER position of the main container.

Add the player information panel to the BorderLayout.EAST position of the main container.

Setting the bounds of the boardX is not really going to have any effect, as the parent container will want to use the panels preferred/minimum/maximum size (based on whatever layout manager you might use) to determine the best size to make it, which based on your code, would probably be 500x500 anyway...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • what would you suggest instead of jlayeredpane? – SKLAK Sep 17 '13 at 00:25
  • Explain to me why you're using a `JLayeredPane`? What are you adding to it? – MadProgrammer Sep 17 '13 at 00:28
  • I'm new to java swing so I was guessing the layeredpane would allow me to layer the board and then the labels on the board. But now that I think about it the labels are on the board, not the layeredpane. What would be the best alternative to layeredpane? – SKLAK Sep 17 '13 at 00:39
  • It would, until you added a layout manager to it ;). Personally, I would make each "cell" on the board something like a `JPanel`, setting it's layout to `BorderLayout` and then add/remove components directory to/from these cells. That way the layering is taken care for you. This would require you to, obviously, know which panel belonged to which cell though... ;) – MadProgrammer Sep 17 '13 at 00:47