Solved

Change label text dynamically

Posted on 2012-12-26
9
255 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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
Windows 10 IE Certificate Issue 10 51
MySqlDump not dumping triggers 1 43
ejb message driven bean mdb creation steps 2 18
collection output issue 9 36
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
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:
This video teaches viewers about errors in exception handling.

839 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