[SWING] Apply Decorator pattern to JProgressBar

Discussion in 'Java' started by Hole, Oct 14, 2009.

  1. Hole

    Hole Guest

    Good Afternoon,

    I have a nice JDialog window showing multiple progress bars.
    Now, I would like to add decorated progress bars (it should be a panel
    with a JLabel and a JProgressBar) to the JDialog's content pane so I
    guessed I need to create a new component by Decorator pattern applied
    to JProgressBar.


    I read the decorator pattern documentation but I'm quite confused.
    Does the decorator class extend the component that is being decorated
    or the component that decorate?

    Thanks!!!
     
    Hole, Oct 14, 2009
    #1
    1. Advertising

  2. Hole

    Rzeźnik Guest

    Re: Apply Decorator pattern to JProgressBar

    On 14 Paź, 14:37, Hole <> wrote:
    > Good Afternoon,
    >
    > I have a nice JDialog window showing multiple progress bars.
    > Now, I would like to add decorated progress bars (it should be a panel
    > with a JLabel and a JProgressBar) to the JDialog's content pane so I
    > guessed I need to create a new component by Decorator pattern applied
    > to JProgressBar.
    >
    > I read the decorator  pattern documentation but I'm quite confused.
    > Does the decorator class extend the component that is being decorated
    > or the component that decorate?


    Hi
    No, decorator typically does not extend decorated class (though it
    usually extends some shared base class).
    What you probably want to do is to make a composite component with
    JLabel and a progress bar as its parts and delegate painting to them
    (you might want LayoutManager instance as well). The other way is to
    extend JProgressBar and redefine its painting method as needed (or UI
    painting method). Nice example of composite controls is here:
    https://flamingo.dev.java.net - browse CVS for JSlider, it is very
    simple control so you should not have trouble in understanding what is
    going on there.
     
    Rzeźnik, Oct 14, 2009
    #2
    1. Advertising

  3. In article
    <>,
    Hole <> wrote:

    > I have a nice JDialog window showing multiple progress bars. Now, I
    > would like to add decorated progress bars (it should be a panel with
    > a JLabel and a JProgressBar) to the JDialog's content pane so I
    > guessed I need to create a new component by Decorator pattern applied
    > to JProgressBar.


    The requirement that "it should be a panel with a JLabel and a
    JProgressBar" suggests composition, as outlined here:

    class LabelBar extends JPanel {
    private JLabel label;
    private JProgressBar bar;
    public LabelBar(...) { ... }
    ...
    }

    Using suitable parameters, add LabelBar instances to a JPanel that
    becomes your JDialog's contentPane.

    > I read the decorator pattern documentation but I'm quite confused.
    > Does the decorator class extend the component that is being decorated
    > or the component that decorate?


    Decoration seems more suited to altering the behavior of an existing
    component:

    <http://www.patterndepot.com/put/8/JavaPatterns.htm>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Oct 14, 2009
    #3
  4. Hole

    Hole Guest

    Re: Apply Decorator pattern to JProgressBar

    On Oct 14, 7:06 pm, "John B. Matthews" <> wrote:
    > In article
    > <>,
    >
    >  Hole <> wrote:
    > > I have a nice JDialog window showing multiple progress bars. Now, I
    > > would like to add decorated progress bars (it should be a panel with
    > > a JLabel and a JProgressBar) to the JDialog's content pane so I
    > > guessed I need to create a new component by Decorator pattern applied
    > > to JProgressBar.

    >
    > The requirement that "it should be a panel with a JLabel and a
    > JProgressBar" suggests composition, as outlined here:
    >
    > class LabelBar extends JPanel {
    >     private JLabel label;
    >     private JProgressBar bar;
    >     public LabelBar(...) { ... }
    >     ...
    >
    > }
    >
    > Using suitable parameters, add LabelBar instances to a JPanel that
    > becomes your JDialog's contentPane.
    >
    > > I read the decorator  pattern documentation but I'm quite confused.
    > > Does the decorator class extend the component that is being decorated
    > > or the component that decorate?

    >
    > Decoration seems more suited to altering the behavior of an existing
    > component:
    >
    > <http://www.patterndepot.com/put/8/JavaPatterns.htm>
    >
    > --
    > John B. Matthews
    > trashgod at gmail dot com
    > <http://sites.google.com/site/drjohnbmatthews>


    Thanks to all of you.
    I wanted to extend JProgressBar to prevent too many modifies to code.
    However, I simply extended JPanel (as John suggests) and did some
    refactoring sessions.

    Cheers!!
     
    Hole, Oct 15, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cengiz

    Update of JProgressBar

    Cengiz, Aug 6, 2003, in forum: Java
    Replies:
    2
    Views:
    900
    Cengiz
    Aug 7, 2003
  2. Inertia_sublimation
    Replies:
    5
    Views:
    2,124
    Inertia_sublimation
    Jan 3, 2004
  3. Stefan Siegl
    Replies:
    1
    Views:
    986
    Marrow
    Jul 18, 2003
  4. glomde
    Replies:
    5
    Views:
    545
    glomde
    Mar 29, 2007
  5. alex23
    Replies:
    7
    Views:
    231
    Jan Kaliszewski
    Aug 22, 2009
Loading...

Share This Page