Forums Register Login

Find jLabel reference to update (from array of jLabels)

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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!!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Why did you make "ctr" static? Because it's static, all instances use the same variable.
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
You're welcome!
+Pie Number of slices to send: Send
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
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2710 times.
Similar Threads
arrange the spacing between components
i want to call main class constructor from inner class or smt...
help on reading textfield on 1 class from another class and using it to create a file
gridbagconstraints problem
GridBagConstraints Problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:38:28.