Swing Custom JPanel Component Help

P

pek

Currently I have a CardGroupPanel that extends JScrollPane (because I
want it to be scrollable).
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.

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.

CardGroupPanel also has an option for Card Orientation (Horizontal or
Vertical). Depending on the orientation, the cards are drawn
accordingly.

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.

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.
 
S

SadRed

Currently I have a CardGroupPanel that extends JScrollPane (because I
want it to be scrollable).
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.

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.

CardGroupPanel also has an option for Card Orientation (Horizontal or
Vertical). Depending on the orientation, the cards are drawn
accordingly.

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.

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.

I think you could use GridLayout, GridBagLayout or nested BoxLayouts.

'Container' is not a good name for your custom GUI class because it is
the name of a basic API class of java.awt package.
 
D

Daniel Pitts

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 said:
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.
 
P

pek

CardGroupPanel should extends JPanel, and be put into a JScrollPane,
rather than extending JScrollPane

I extend JScrollPane because the scrollability of the object is not
optional. It always exist. So, rather than setting for every
CardGroupPanel a new JScrollPane, I find it much easier to extend
JScrollPane. Although, I did have some problems with the background.
When extending a JPanel, you can use setOpaque(false) and the JPanel
is transparent. When extending the JScrollPane, even if you set both
(or any combination) JScrollPane's setOpaque and the JPanel's
container setOpaque it still isn't transparent. I'd hate adding to the
constructor a Color which would be used to set it's background color.
I find it bad practice. But, this would be my solution if nothing else
doesn't work. Also, I tried overriding JScrollPane's
setBackground(Color bg) and after calling super.setBackground(bg) I
try to call container.setBackground(bg) but container hasn't been yet
initialized and a NullPointerException is been thrown. I haven't yet
found I way so please advise. ;)
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.

I find it quicker to use JLabel since it has a setIcon. That is
currently what I use. But recently I changed it and I am overloading
the paint(Graphics g) method because I draw some more things on top of
the card. I haven't look to use JComponent yet. I think that since I
am using paint, there is no reason to be a JLabel any more. At first,
I thought that MouseListener is the reason why I still have JLabel. I
just saw JComponent at the API. It supports MouseListener. So probably
I'll change it.
Your method would be better to take a Collection<Card> instead of
LinkedList<Card>

You mean, the field should be a Collection, but when I initialize it I
would use LinkedList<Card> ? Like : Collection<Card> cards = new
LinkedList said:
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)>

Never thought of using the components orientation. ;) I thought it
would be easier to create a new. But OK, I find it reasonable. ;)
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.

I'll try most of the things and send feedback here. Thank you very
much for your time.
 
D

Daniel Pitts

I extend JScrollPane because the scrollability of the object is not
optional. It always exist. So, rather than setting for every
CardGroupPanel a new JScrollPane, I find it much easier to extend
JScrollPane. Although, I did have some problems with the background.
When extending a JPanel, you can use setOpaque(false) and the JPanel
is transparent. When extending the JScrollPane, even if you set both
(or any combination) JScrollPane's setOpaque and the JPanel's
container setOpaque it still isn't transparent. I'd hate adding to the
constructor a Color which would be used to set it's background color.
I find it bad practice. But, this would be my solution if nothing else
doesn't work. Also, I tried overriding JScrollPane's
setBackground(Color bg) and after calling super.setBackground(bg) I
try to call container.setBackground(bg) but container hasn't been yet
initialized and a NullPointerException is been thrown. I haven't yet
found I way so please advise. ;)

If you don't set the CardGroupPAnel as the viewport view for your
scroll pane, you aren't going to get any scroll bars.

The proper way to do what you are trying to do:

CardGroupPanel panel = new CardGroupPanel();
JScrollPane scrollPane = new JScrollPane(panel);
panel.setBackground(Color.green);
scrollPane.setBackground(Color.green);


Make sure CardGroupPanel extends JPanel and implements Scrollable.
That should give you the control you need over width/height/scroll
area.

Read the sun tutorial for more information <http://java.sun.com/docs/
books/tutorial/uiswing/components/scrollpane.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top