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 / GUI / September 2005

Tip: Looking for answers? Try searching our database.

Painting a Stack with Swing

Thread view: 
froorider - 29 Sep 2005 10:54 GMT
Hy,

I want to make my own swing component. The component should paint a
dynamic stack. The stack contains some panels. These panels should be
painted horizontally or vertically. At init time of the component, an
"empty" stack view  with an initial size should be painted.

I used a Panel with a BorderLaout. The stack view is in the center of
it. On the left side, there is a JLabel showing some information. The
problem is, that the "empty" stack is not painted at all. I tired to
fill the stack with empty images but this doesn't worked.

Does someone now how to deal with that or knows a Swing lib with a
predefined layout manager, component or something like that? The
component I want to develop is for a Visualization.

Thanks.

The component should look like this:

------------------------------------
|               |              |   |
|               | ------------ |   |
|               | | PALLET 3 | |   |
|               | ------------ |   |
|               | ------------ |   |
|  Place 510    | | PALLET 2 | |   |
|               | ------------ |   |
|               | ------------ |   |
|               | | PALLET 1 | |   |
|               | ------------ |   |
|               |              |   |
------------------------------------
Roedy Green - 29 Sep 2005 14:29 GMT
>I want to make my own swing component. The component should paint a
>dynamic stack. The stack contains some panels. These panels should be
>painted horizontally or vertically. At init time of the component, an
>"empty" stack view  with an initial size should be painted.

The problem is the default size for your panels would be 0,0 since
they contain nothing.

There are three basic approaches to tackling your problem.

1. You fool around with different layout managers, creating dummy
components, doing setMinimumSize set PreferredSize and setMaxmimuSize,
playing with GridBagLayout weights etc. until you trick Swing into
producing the effects you want. I would give FlowLayout a try for the
right-hand stack section with the variable number of items.  Remember
to validate/repaint after every pop/push. See
http://mindprod.com/jgloss/layout.html

2. If no layout will give you the effects you want, write you own. See
http://mindprod.com/jgloss/layoutmananger.html.  It is much easier
than you think.

3. If the "panels" are not complex panels in their own right but
merely coloured bars, or labelled bars, overwrite paintComponent on
your main panel, examine the total size of the panel, allocate it to
your stack items, and fillRect some rectangles and drawString on the
labels. The logic is very similar to writing your own LayoutManager. A
LayoutManager would not do any drawing, just the placement.

The advantage of the "StackLayoutManager" is it becomes a much more
reusable component. Code that uses it is much easier to maintain with
application logic, placement logic, and drawing logic nicely
separated.


Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

froorider - 30 Sep 2005 07:44 GMT
Thank you for your hints. I've already begun to write a custom
LayoutManager. I called it StackLayout. When I'm finished with it, I
will post the code somewhere.

Greetz

Tom
Roedy Green - 30 Sep 2005 08:21 GMT
>Thank you for your hints. I've already begun to write a custom
>LayoutManager. I called it StackLayout. When I'm finished with it, I
>will post the code somewhere.

Good.  A Layout is a little harder to get you head around, but the end
product is much cleaner.  See
http://mindprod.com/jgloss/starlayout.html

It is the source code for the world most mindlessly simple layout
manager.  It is an absolute layout manager that arranges components in
star shaped patterns, e.g. compass points, VCR buttons,  clocks.

Looking at the source should help you grab the concepts of what you
need to do in your layout manager, without overwhelming you with
details.

The nice thing is, when you are done, the StackLayout will behave
itself under all conditions without futzing about trying to trick it
into behaving.  Further, you will also have a much deeper
understanding of layouts in general, which is nice to have under your
belt.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



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.