Results 1 to 1 of 1
- 10-26-2012, 08:15 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 50
- Rep Power
- 0
Calling methods of a component from a different component
First off, I haven't really gone through the lesson on timers yet, so I understand that my timer might not work properly the way I designed it. It's just there to give you a rough idea of what it's supposed to be doing.Java Code:public class Main { public static void main (String[] args) { SwingUtilities.invokeLater (new Runnable() { public void run() { createAndShowGUI(); } }); } private static void createAndShowGUI() { JFrame frame = new JFrame (""); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.setJMenuBar (new JMenuBar()); frame.setContentPane (new MainPanel()); frame.pack(); frame.setLocationRelativeTo (null); frame.setVisible (true); } } class MainPanel extends JPanel { public TimerLabel timerLabel = new TimerLabel(); public SolutionsPanel solutionsPanel = new SolutionsPanel(); public MainPanel() { super(); add (timerLabel); add (solutionsPanel); restart(); } public void restart() { timerLabel.reset(); solutionsPanel.reset(); } } class TimerLabel extends JLabel implements ActionListener { public Timer timer = new Timer (1000, this); TimerLabel() { super(); } public void actionPerformed (ActionEvent e) { int timeLeft = Integer.valueOf (this.getText()); if (timeLeft > 0) { timeLeft--; this.setText (Integer.toString (timeLeft)); } else { //call MainPanel.solutionsPanel.showSolutions(); !!! } } public void reset() { timer.stop(); this.setText (Integer.toString (100)); timer.start(); } } class SolutionsPanel extends JScrollPane { JTextArea textArea; SolutionsPanel() { super(); textArea = new JTextArea (4, 15); add (textArea); } public void reset() { textArea.setText ("Solutions"); hideSolutions(); } public void hideSolutions() { textArea.setVisible (false); } public void showSolutions() { textArea.setVisible (true); //call timerLabel.timer.stop(); !!! } }
I have a panel with two components: timerLabel and solutionsPanel, as seen above.
Basically what I want to do is, when the timer reaches zero, I want to call showSolutions() on solutionsPanel, and then showSolutions should in turn call timer.stop() on timerLabel.timer. Is there an easy way to do this? What would happen if I made timerLabel and solutionsPanel static?Last edited by Mate de Vita; 10-27-2012 at 01:56 PM.
Similar Threads
-
Tutorial:Review of Http Methods and Servlet API for Web Component Developer Exam
By Java Exam in forum OCPJWCDReplies: 0Last Post: 01-12-2012, 07:39 PM -
using string representation of component to access its methods
By d3n1s in forum Advanced JavaReplies: 15Last Post: 09-21-2011, 01:25 AM -
What's the name of the component that...?
By balla in forum AWT / SwingReplies: 1Last Post: 07-06-2011, 03:03 AM -
Calling url from java component
By cool in forum AWT / SwingReplies: 3Last Post: 12-07-2010, 10:58 AM -
Component to use ?
By pbaudru in forum AWT / SwingReplies: 2Last Post: 02-17-2010, 09:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks