Solved

Creating labeled GUI components

Posted on 2004-04-08
21
254 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 30

Assisted Solution

by:Mayank S
Mayank S 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:Mayank S
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:Mayank S
ID: 10783468
:-)
0
 

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:Mayank S
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:Mayank S
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 63
difference between sorce folder and folder in eclipise 3 28
Java 8 to Java 6 8 19
What browser will run Java? 7 41
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

792 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