[SWING] Apply Decorator pattern to JProgressBar

H

Hole

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

Rzeźnik

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

John B. Matthews

Hole said:
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>
 
H

Hole

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.


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

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

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

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top