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.
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!!
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);
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?
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
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.
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 ]
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?
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?
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
Post by:autobot
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