Re: Painting a Stack with Swing



On 29 Sep 2005 02:54:02 -0700, "froorider" <t.hasler@xxxxxxxx> wrote
or quoted :

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





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



Relevant Pages

  • Advanced DataBindings and Stack Overlow
    ... basically, it kind of works when i bind, let's say, the 'visible' property ... control the application will crash with the message 'stack overflow'. ... bound panels will cause the crash after about 50 'next's.. ...
    (microsoft.public.dotnet.framework.windowsforms.databinding)
  • Painting a Stack with Swing
    ... I want to make my own swing component. ... The stack contains some panels. ... "empty" stack view with an initial size should be painted. ...
    (comp.lang.java.gui)
  • Re: Are First Alert panels any good ?
    ... First Alert panels are ... simply re-branded Ademco panels. ... How do they stack up with Ademco or GE panels. ...
    (alt.security.alarms)
  • Re: GUI best practices question
    ... but the number of containers (panels, frames...) and layout managers. ...
    (comp.lang.java.help)
  • Re: what are panels for?
    ... Panels can encapsulate layouts and components. ... It is generally referred to as a nested layout. ... to encapsulate the comonents in a parent container in the ...
    (comp.lang.java.gui)