Results 1 to 1 of 1
  1. #1
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    63
    Rep Power
    0

    Default Calling methods of a component from a different component

    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(); !!!
        }
    }
    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.

    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

  1. Replies: 0
    Last Post: 01-12-2012, 07:39 PM
  2. Replies: 15
    Last Post: 09-21-2011, 01:25 AM
  3. What's the name of the component that...?
    By balla in forum AWT / Swing
    Replies: 1
    Last Post: 07-06-2011, 03:03 AM
  4. Calling url from java component
    By cool in forum AWT / Swing
    Replies: 3
    Last Post: 12-07-2010, 10:58 AM
  5. Component to use ?
    By pbaudru in forum AWT / Swing
    Replies: 2
    Last Post: 02-17-2010, 09:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •