Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

How to update my label in the gui

Thread view: 
Petterson Mikael - 08 Dec 2005 17:49 GMT
Hi,

I have written a class that keeps track of the number of threads
running. My problem is that I don't know how to update the label:
"Number of current threads is: "
Any hints?

cheers,

//mikael

Part of gui code:
=================
//Add panel with number of threads
        GridBagConstraints threadsTextConstraints = new GridBagConstraints();
        threadsTextConstraints.gridx = 0;
        threadsTextConstraints.gridy = 6;
        threadsTextConstraints.gridwidth = 2;
        threadsTextConstraints.gridheight = 1;
        threadsTextConstraints.fill = GridBagConstraints.HORIZONTAL;
        threadsLabel = new JLabel("Number of Threads is
"+tc.getNumberOfThreads());
        threadsLabel.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
        gridbag.setConstraints(threadsLabel, threadsTextConstraints);
        this.add(threadsLabel);

Thread class:
=============
public class ThreadCounter implements Runnable {
    private Exercise2Model model;
    private int numberOfThreads = 0;
    public ThreadCounter(Exercise2Model model){
        this.model = model;   
    }
   
    public void run() {
        while (true) {
            try {
                Thread.sleep(10);
                int nrOfThreads = model.getNumberOfThreads();
                this.setNumberOfThreads(nrOfThreads);
            } catch (InterruptedException ie) {
                System.err.println("Thread has been interrupted");
            }
           
        }
    }
   
    public String getNumberOfThreads(){
    return    Integer.toString(numberOfThreads);   
    }
   
    public void setNumberOfThreads(int nrOfThreads){
        if (numberOfThreads == nrOfThreads){
            //There is no change. Don't do anything.
        }else{
        //There has been a change
        numberOfThreads=nrOfThreads;
        System.out.println(numberOfThreads);
        }
    }
}
Knute Johnson - 08 Dec 2005 17:57 GMT
> Hi,
>
[quoted text clipped - 59 lines]
>     }
> }

JLabel.setText()

Signature

Knute Johnson
email s/nospam/knute/

Roedy Green - 08 Dec 2005 23:51 GMT
On Thu, 08 Dec 2005 18:49:51 +0100, Petterson Mikael
<mikael.petterson@era.ericsson.se> wrote, quoted or indirectly quoted
someone who said :

>//There has been a change
>        numberOfThreads=nrOfThreads;
>        System.out.println(numberOfThreads);

you do a setText.  I don't even think you need to use invokeLater
since that method is thread safe, at least it is for a TextComponent.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Knute Johnson - 09 Dec 2005 02:53 GMT
> On Thu, 08 Dec 2005 18:49:51 +0100, Petterson Mikael
> <mikael.petterson@era.ericsson.se> wrote, quoted or indirectly quoted
[quoted text clipped - 6 lines]
>  you do a setText.  I don't even think you need to use invokeLater
> since that method is thread safe, at least it is for a TextComponent.

JLabel doesn't extend JTextComponent and the docs do not say that it is
thread safe.  I think he would need to update it on the EDT.  I think
that may have been the cause of some problems I was have a while back
with the JLabel not resizing correctly.

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.