Link to home
Start Free TrialLog in
Avatar of dirku
dirkuFlag for Germany

asked on

Creating labeled GUI components

How can I create a labeled GUI component which behaves like common swing components?
I want to extend JProgressBar to be a labeled component. Hence, I want to extend JProgressBar. This LabeledJProgressBar is exactly the same like JProgressBar but possess a JLabel. Now the question ist: What do I have to do that this new component paints itself (with the label) like JProgressBar does?
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry I wasn't completely finished:

class MyLabeledProgressBar extends JPanel {

    JLabel theLabel = new JLabel();
    JProgressBar progressBar = new JProgressBar();

    public MyLabeledProgressBar(String labelText) {
       super();
       setLayout( new FlowLayout() );
       theLabel.setText(labelText);
       add(theLabel);
       add(progressBar);
    }


    public void setMaximum(int max) {
       progressBar.setMaximum(max);
    }
    // and so on...

}

The idea is that your class is a Panel (containing a JLabel and a JProgressBar)
Then of course you have to provide all JProgressBar functions that you want to use (e.g. see setMaximum()) on that panel
and pass them to the JProgressBar in it.
The same for the JLabel functions
e.g.

public void setText(String newLabelText) {
    theLabel.setText(newLabelText);
}
The use is then rather straightforward:

MyLabeledProgressBar pb = new MyLabeledProgressBar("Lalbel");
pb.setMinimum(...);
pb.setMaximum(...);
pb.whateverYouNeedToInitialize();

now add pb in whatever panel you want to use it.

Remark: of course you can forsee extra constructors

public MyLabeledProgressBar(String labelText, int min, int max) {
       super();
       setLayout( new FlowLayout() );
       theLabel.setText(labelText);
       add(theLabel);
       progressBar.setMinimum(min);
       progressBar.setMaximum(max);
       add(progressBar);
}

You got the idea I guess
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
setValue() is indeed one of the functions you have to provide in your MyLabelProgressBar class.

Completely up to you to decide about

        - the layout (e.g. where to place the label: left, right, up, down from the progress bar)
          (you could even make that a parameter of the constructor)

        - the behaviour (of the progress bar according to the label or vice versa)
          (like adapting the label if the value is changed)
Yeah, but in case the label is meant for displaying the percentage-completed, then I would suggest that you don't implement all the JLabel methods.

For example, there would be no use to implement the setText () method, because ideally, its not the text that would drive the value of the JProgressBar, but the JProgressBar's value would determine the value to be displayed in the JLabel (and the value of the JProgressBar would perhaps depend upon the status of some other processing).
>> Yeah, but in case the label is meant for displaying the percentage-completed, then I would suggest that ...

Hence my "Completely up to you to decide about..."
;)
Avatar of dirku

ASKER

So, there is no 'easy' way to implement such a component?
I mean something like extending JProgressBar, adding a JLabel to it and overwrite the layout method but to have all the functionality of the JProgressBar through inheritance.
Avatar of GrandSchtroumpf
GrandSchtroumpf

> I mean something like extending JProgressBar

The question is:
Do you add a JLabel to a JProgressBar or do add a JProgressBar to a JLabel?
Will your resulting component be a JProgressBar or JLabel?
The answer is neither!  You just create a new component"
So, you might as well extend JPanel.
>> So, there is no 'easy' way to implement such a component?
I find it quite easy. I agree you have to provide some JProgressBar & JLabel functions (only the ones you gonna use)
but that isn't hard, is it? That's just typing work.
Well, you would generally extend a class when you want to enhance it. Here, you want to merge the two together, so it would better to have a new class of your own which contains these two members.

Even if you want to extend a JProgressBar to do this, well - you would end up writing your own class and it might prove to be tougher to decide how to deal with the JLabel in that case.
Avatar of dirku

ASKER

I'm sorry for taking this long time to comment your suggestions.
I don't want to have this overhead of reimplementing all the methods of JLabel/JProgressBar. I rather thought of something like adapting the paint method of the component that a String will be rendered for instance on the left hand side of the JProgressBar.
Unfortunately I will not find the time now to check this possibility but I have created an own type of Border and I just extended an EtchedLayout and adapted the paintBorder method. I think this will work for the JProgressBar component to.

However, thank you for your suggestions.
Well, but that goes beyond the scope of your initial question. The suggestions posted to your question were correct as per your question's scope and so, you should close it.

As of now, my recommendation would be - split between mayankeagle and zzynx.
Hi dirku,
I agree with mayank,
Can you please accept.
Btw:
>>I don't want to have this overhead of reimplementing all the methods of JLabel/JProgressBar.
Only the funcs you really need/gonna use.

>>I rather thought of something like adapting the paint method of the component that a String
>>will be rendered for instance on the left hand side of the JProgressBar.
You could discuss about what creates more overhead...
As we already said: our recommendation is: split between mayankeagle and zzynx.