Solved

Change label text dynamically

Posted on 2012-12-26
9
253 Views
Last Modified: 2013-01-17
Dear Expert,
I have a string array and a pannel with buttons. I want to change label text of button with array values. I do not want to make it happen through button.setText methode. I want to achieve it dynamically.

Please help and suggest the solution.

Regards
0
Comment
Question by:MILIND_JOG
  • 4
  • 4
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 38721054
I do not want to make it happen through button.setText methode. I want to achieve it dynamically.
Whether dynamic or not, you would use setText either way. Without knowing more of what you want to achieve it's not possible to advise though
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 38721073
Dear Expert,

Spot on. And as he says, that's the method you are going to end up using. The only other thing that occurs to me that you might mean is that text changes on a mouseover. In which case, you could use a MouseInputAdapter. Although you might also need MouseInfo from the awt too.
0
 

Author Comment

by:MILIND_JOG
ID: 38722773
Thanks for instant comments. I mentioned that I do not want to use Button.setText methode. I mean to say that insted using setText on every button, I want to make it happen in loop using Component of Panel so as to avoide setText method for every individual Button.

Regards
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38723993
Yes, you can use a loop, but again, setText would be used nonetheless. Typically if you have a large number of components where there is a pattern in one of the attributes (in your case, the text) then you can use an array of Component
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:MILIND_JOG
ID: 38726462
Component[] jp1 = jPanel1.getComponents();
        for (Component component : jp1){
            component.setEnabled(false);
        }

using above code, I wanted to set component text and label values. I am unable to achieve the same. I am unable to get such setText or setLabel methodes. I am able to get only setName which is of no use for me. If notthing can be done than no matter I have already done it on individual Button basis.

Regards
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 38726683
That's not a particularly good way of doing it, but it might be made to work thus

int index = 0;
        Component[] jp1 = jPanel1.getComponents();

        for (Component component : jp1) {
            if (component instanceof JButton) {
                ((JButton) component).setText(String.format("Button %d", ++index));
            }
        }

Open in new window

0
 

Author Comment

by:MILIND_JOG
ID: 38728816
Thanks CEHJ. I will make necessory changes and will come back to you again.

Regards.
0
 

Author Closing Comment

by:MILIND_JOG
ID: 38785891
Thanks CEHJ for your great support.

Regards
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38786912
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

932 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

11 Experts available now in Live!

Get 1:1 Help Now