2

I am writing a small card game and so far i have the following :

  • I have a custom JPanel Card component where i override the getPreferredSize() method

  • I also have a custom HandView component which essentially just adds a bunch of card components to it. I also override the getPreferredSize() and use a flowlayout.

My problem is when too many Card components are added to the handView they are not shown. I do not want the card components and the hand component to resize . What i would like is to make the card components start overlapping each other when too many are added. Is that possible with a flowlayout?

user2302702
  • 95
  • 1
  • 7

1 Answers1

4

Is that possible with a flowlayout?

You can specify a horizontal gap when creating the FlowLayout. If you use a negative value then you will get overlapping.

However, you may want to consider the Overlap Layout which was written specifically for this type of layout. It provides different option for how the overlapping should work.

What i would like is to make the card components start overlapping each other when too many are added

In both cases you would need to dynamically reset the gap as cards are added/removed and then revalidate the panel.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Overlap layout seems interesting but i can't get it to work. For some reason when i add my cards it does not display anything. (i have called the revalidate - repaint methods). I guess i will use the negative gap approach – user2302702 Oct 02 '15 at 16:52
  • @user2302702, `when i add my cards it does not display anything.` Hmm, I just noticed a problem when you use `frame.setSize(...)` the components don't display, but if you use `frame.pack()` they do display. Not sure what the problem is, I'll look into it when I get time. – camickr Oct 02 '15 at 17:35
  • @user2302702, found the bug and uploaded a new version. The components should appear on the overlap layout whether you use setSize() or pack() on the frame. – camickr Oct 02 '15 at 18:32