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?
dirkuAsked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
class MyLabeledProgressBar extends JPanel {

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

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


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

}
0
 
zzynxSoftware engineerCommented:
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);
}
0
 
zzynxSoftware engineerCommented:
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
>> What do I have to do that this new component paints itself (with the label) like JProgressBar does?

The label would remain as it is, and the JProgressBar's value would be updated. What other kind of repainting do you want to happen with the label?

Is it that you want to display the "percentage complete" in the label? If so, then when you set the value for the JProgressBar in your setValue () method (which you will provide, if you're implementing all methods that the JProgressBar has), you can also set the text for the label there:

public void setValue ( int value )
{
  label.setText ( Integer.toString ( value ) ) ;
  progressBar.setValue ( value ) ;

} // end of setValue ()
0
 
zzynxSoftware engineerCommented:
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)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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).
0
 
zzynxSoftware engineerCommented:
>> 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..."
;)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
:-)
0
 
dirkuAuthor Commented:
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.
0
 
GrandSchtroumpfCommented:
> 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.
0
 
zzynxSoftware engineerCommented:
>> 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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
dirkuAuthor Commented:
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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
zzynxSoftware engineerCommented:
Hi dirku,
I agree with mayank,
Can you please accept.
0
 
zzynxSoftware engineerCommented:
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...
0
 
zzynxSoftware engineerCommented:
As we already said: our recommendation is: split between mayankeagle and zzynx.
0
All Courses

From novice to tech pro — start learning today.