Question

I'm trying to display cards in my GUI and I want to overlap cards to save on space. However, I can't seem to get it to work. I'm currently using a MigLayout, though it feels like it's more of a JLabel issue than something else.

To describe it at a high level, consider playing a game of cards in real life. When you hold cards in your hand normally, you only need to see part of the card, so much of the card is overlapped by another card, and so forth until the the final card is fully displayed since nothing is on top of it. I want to overlap in a way that only part of the cards underneath is shown, and then the top card is fully shown.

The current problem is that when I add a JLabel to a JPanel, it puts the newest label behind what is already there. I want it to be in front of what is already there. I can't seem to find any method that allows me to set which label is in front.

I want to do something like:

Put card1 in cell 0 0 Put card2 in cell 0 0 with a transposed y position Any area where both cards overlapped has card2 on top

Simply adding it puts it behind, which suggests a first-come, first-serve priority on the display of a component. I'd rather have a last-come, first-serve priority. And like I've said, I have had no luck trying to find a method that allows any of this.

If any of this is unclear, please let me know. Thanks in advance.

Was it helpful?

Solution

It looks like JPanel inherits from Container, which has a method setComponentZOrder() which sounds like it does just what you need.

setCompoentZOrder() documentation

OTHER TIPS

Check out Overlap Layout which explains a bit more about how ZOrder works and may even provide you with a layout manager to help in your card game.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top