Solved

Change label text dynamically

Posted on 2012-12-26
9
252 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

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!

 

Author Comment

by:MILIND_JOG
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks CEHJ. I will make necessory changes and will come back to you again.

Regards.
0
 

Author Closing Comment

by:MILIND_JOG
Comment Utility
Thanks CEHJ for your great support.

Regards
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 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

12 Experts available now in Live!

Get 1:1 Help Now