Solved

Change label text dynamically

Posted on 2012-12-26
9
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

728 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