Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Swing Custom JPanel Component Help

Thread view: 
pek - 15 Jul 2007 03:23 GMT
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.
SadRed - 15 Jul 2007 05:02 GMT
> Currently I have a CardGroupPanel that extends JScrollPane (because I
> want it to be scrollable).
[quoted text clipped - 35 lines]
> 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.
Daniel Pitts - 15 Jul 2007 05:29 GMT
> 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
[quoted text clipped - 7 lines]
> 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
[quoted text clipped - 9 lines]
> I hope everything is clear. Thank you.
> Sorry for my English.
Mark Space - 15 Jul 2007 07:06 GMT
>> 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>

or maybe just an array?

addCards( Card [] someCards ) { // ...
pek - 15 Jul 2007 16:22 GMT
> 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

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. ;)

> > This CardGroupPanel has a JPanel named Container. The Container has a
> > number of CardLabels that are components that extend JLabel and draw a
[quoted text clipped - 3 lines]
> 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.

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

You mean, the field should be a Collection, but when I initialize it I
would use LinkedList<Card> ? Like : Collection<Card> cards = new
LinkedList<Card>(); OK, I'll do it.

> > CardGroupPanel also has an option for Card Orientation (Horizontal or
> > Vertical). Depending on the orientation, the cards are drawn
[quoted text clipped - 3 lines]
> 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. ;)

> > Suppose that I create a CardGroupPanel with Horizontal orientation.
> > The CardGroupPanel has width 200 (depends from the size of the
[quoted text clipped - 10 lines]
> 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.

> > The problem is that by default, the size of Container is not set.
> > Calling container.getSize(), container.getMinimumSize() and
[quoted text clipped - 9 lines]
> > I hope everything is clear. Thank you.
> > Sorry for my English.
Daniel Pitts - 15 Jul 2007 19:39 GMT
> > 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).
[quoted text clipped - 17 lines]
> 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>
Roedy Green - 15 Jul 2007 19:37 GMT
>This CardGroupPanel has a JPanel named Container.

Not a wise choice of name. Container is the name of a the class for a
Swing getContentPane.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.