Re: Swing Custom JPanel Component Help
On Jul 14, 7:23 pm, pek <kimwl...@gmail.com> wrote:
Currently I have a CardGroupPanel that extends JScrollPane (because I
want it to be scrollable).
CardGroupPanel should extends JPanel, and be put into a JScrollPane,
rather than extending JScrollPane
This CardGroupPanel has a JPanel named Container. The Container has a
number of CardLabels that are components that extend JLabel and draw a
Playing Card. CardGroupLabel also has a LinkedList<Card> cards.
Depending on how you draw the card, you should either use a standard
JLabel, and use setIcon to set the image, or you should extends
JComponent instead of JLabel.
CardGroupPanel has a method addCards(LinkedList<Card> someCards) which
adds someCards to cards and creates for each Card a CardLabel
(basically a wrapper for each Card) which then is added to Container.
Your method would be better to take a Collection<Card> instead of
LinkedList<Card>
CardGroupPanel also has an option for Card Orientation (Horizontal or
Vertical). Depending on the orientation, the cards are drawn
accordingly.
Most Components have orientation built in. See <http://java.sun.com/
j2se/1.4.2/docs/api/java/awt/
Component.html#setComponentOrientation(java.awt.ComponentOrientation)>
Suppose that I create a CardGroupPanel with Horizontal orientation.
The CardGroupPanel has width 200 (depends from the size of the
JFrame). The CardLabels width is about 20. This means that
CardGroupPanel can draw fully, 10 CardLabels. After that, the vertical
scroll bar will appear.
The problem is that I want, when Horizontal orientation is selected,
the 11th card will be drawn UNDER the first card creating a second row
of cards. Thus, HORIZONTAL SCOLL BAR will be activated. Same with
Vertical orientation, I would like to create a second column of cards
and activate the horizontal scroll bar.
If you follow the very first suggestion I gave you, that should solve
your problem, or at least get you moving in the right direction.
The problem is that by default, the size of Container is not set.
Calling container.getSize(), container.getMinimumSize() and
getPreferredSize() will get 0x0 and getMaximumSize() will get
something like 31000x20000. That means that I cannot use the
containers size to calculate exactly WHEN to create a second row or
column. I don't want to explicitly set the size because if the JFrame
is resized, it should automatically calculate the rows and columns of
the new size it has.
Any ideas?
I hope everything is clear. Thank you.
Sorry for my English.