Solved

Creating labeled GUI components

Posted on 2004-04-08
21
246 Views
Last Modified: 2013-11-23
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?
0
Comment
Question by:dirku
  • 9
  • 5
  • 2
  • +1
21 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 25 total points
ID: 10782859
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10782880
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10782902
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
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 25 total points
ID: 10782943
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 10783050
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10783356
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10783428
>> 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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10783468
:-)
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:dirku
ID: 10784420
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
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10786662
> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 10787171
>> 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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10789346
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
 

Author Comment

by:dirku
ID: 10958718
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10958742
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10958797
Hi dirku,
I agree with mayank,
Can you please accept.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10958825
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10959998
As we already said: our recommendation is: split between mayankeagle and zzynx.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now