aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Find jLabel reference to update (from array of jLabels) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Find jLabel reference to update (from array of jLabels)" Watch "Find jLabel reference to update (from array of jLabels)" New topic
Author

Find jLabel reference to update (from array of jLabels)

Eamon Williams
Greenhorn

Joined: Feb 21, 2006
Posts: 18
Hi,

I have one form from which I am dynamically creating arrays of jPanels, jButtons & JLabels (number of which unknown until runtime). Each panel has one button and one jlabel on it - all added to the main panel. I have an actionlistener on each jButton so that when pressed it updates the jlabel on its (same) panel. The jlabel that it is updating is actually a timer.

My problem is I don't know how to find or reference which jlabel to update from the button that has been pressed. This is because I am unsure how the components are named when they are added to the array; for example, jButton1 and jLabel1 etc.

Heres the code for the adding of arrays:



Heres the code for the actionlistener:


Any hints or tips would be great.

Thanks.
Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
If my understanding is correct, then you require one JButton and JLAbel in each of the JPanel. JLabel is the Timer. If that is the case then why don't you try Extending the JPanel class and adding two members a JButton and the JLAbel. Have the actionListener in this class itself so that it knows which Button /Label to update.
You can then have an array of this object.
If you throw more light on the requirement then maybe i can help!!


-Sam Codean<br />SCJP 1.4 (98%)<br />SCJD 5.0 (87.5%)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
It is generally a better idea to have listener objects which are responsible for exactly one thing (i.e. one button press).

For example

startButtons[i].addActionListener(new StartHandler(ringNumLabel[i], i));

Your code would become much simpler - no need to use actioncommands or to parse button names etc.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Eamon Williams
Greenhorn

Joined: Feb 21, 2006
Posts: 18
Thanks for the reply.

I'm not entirely sure how to implement your solution (i'm new to programming). The requirement is to have an unknown number of panels on the interface (all consisting of a button and label), stored in an array. On pressing the 'Start' button the jlabel will be updated with a timer which will begin counting.

My button array is defined as:
JButton startButtons[] = new JButton[aNumber];
startButtons[i] = new JButton("Start");
(where i will increment aNumber of times)

When the actionlistener on this button is called, how do I establish which jlabel to update with the timer? i.e. how do I find and reference the component name of the label (i.e jLabelx) from the array 'timerLabel[]'? So that I can then go on to display the timer; timerLabelx.setText("Count: " + ctr);

Thanks for the help.
Eamon Williams
Greenhorn

Joined: Feb 21, 2006
Posts: 18
Hi Ilja,

Sorry, I had posted the previous reply before I read your post.

Using

startButtons[i].addActionListener(new StartHandler(ringNumLabel[i], i));

as you say, why are you passing ringNumLabel[i] and i? Do I handle the event in StartHandler function? how would I then reference ringNumLabel[i] in the StartHandler code to add the timer?

Thanks again
Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
Hi Eamon,
Let me summarize the requirement. You need to have a set of JPanels with one JButton and JLabel in each. When you click on that button the corresponding JLabel shows a Counter something like 00:00:00 and increments every second.

If that is the case then two questions
1. How many of these Panels are to be created? Where do you get the number at Runtime? Through some property or user enters in some textfield?
2. When does the timer stop?

Best way i still feel is to have a class extending JPanel and having members JButton, JLabel, ActionListener, Timer.
It simplifies many things. Try giving it a shot
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Eamon Williams:
Using

startButtons[i].addActionListener(new StartHandler(ringNumLabel[i], i));

as you say, why are you passing ringNumLabel[i] and i? Do I handle the event in StartHandler function? how would I then reference ringNumLabel[i] in the StartHandler code to add the timer?


The StartHandler class would implement the ActionListener interface, and therefore contain the actionPerformed method, handling the event.

The ringNumLabel[i] is passed to the constructor, so that the handler can remember it in a field, and access it in the actionPerformed method.
Eamon Williams
Greenhorn

Joined: Feb 21, 2006
Posts: 18
Hi,

Please bear with me, think i'm getting there now. I've passed ringNumLabel[i] to the StartHandler class and created the timer. How does it update the jlabel on the form in the main class from the StartHandler class? (on panel[i])



Thanks once again
[ March 05, 2006: Message edited by: Eamon Williams ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I'm not sure I understand the question. Did the code work and you are asking how exactly it is working? Or is there still some problem you need help with?
Eamon Williams
Greenhorn

Joined: Feb 21, 2006
Posts: 18
Hi,

In the end I implemented the solution that uses a seperate class to extend jpanel. So all of my code to add the timerlabel and button is in this class. This all works and displays as expected except for the timer, as each 'start' button is pressed the counter begins counting from the number the last timer was at. i.e. the first timer started begins at 0, the second timer started will begin counting from whatever number the first timer is on at that point (instead of 0).

Is it because each 'ctr' variable is named the same? Do I need an array of these for each panel class i'm adding to the main panel?

Heres my code for this class:


Cheers
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Why did you make "ctr" static? Because it's static, all instances use the same variable.
Eamon Williams
Greenhorn

Joined: Feb 21, 2006
Posts: 18
Ahh, I did not know that! Sounds right, i'll tweak it later on.

I've never used this forum before and it's been a great help for me - Cheers!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You're welcome!
Eamon Williams
Greenhorn

Joined: Feb 21, 2006
Posts: 18
Hi,

Another query on this piece of work if anybody can help...?

Background is; I have a form which creates an array of jPanels - each jPanel is created in 'SetUpRings2' as directly above in this post, which extends jPanel and has actionlisteners on the buttons placed in this class. The array of panels is added to a main panel.



My question is, how can I capture an event from any one of the panels in the panelArray from the main form? I need to update a database and update some components on the GUI on the main form when the user presses the button on the panel. What code do I need on the main form to capture this?

Thanks once again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Find jLabel reference to update (from array of jLabels)