Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Change label text dynamically

Posted on 2012-12-26
9
Medium Priority
?
260 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

610 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