CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Oct 2011
    Posts
    8

    Newb with (probably) a fundamental misunderstanding

    Hi all, I'm new to Java, and to programming in general, but in an attempt to learn, I am trying to create some programs, but have run into an issue...

    So, let me just dive right in to my problem...

    I have three classes:

    TimerPanel which extends JPanel, and contains methods to start and stop the timer
    InputPanel which extends JPanel, and accepts user input
    And my main which contains the instances of, and displays both of these panels

    I have created instances of TimerPanel and InputPanel in a JFrame in my main.

    My problem is that I wish to call the start and stop timer methods on the specific instance of TimerPanel which resides in my main FROM my InputPanel.

    I have googled for a couple hours for a solution to this problem, but I cannot figure it out. So far, I have gathered that I must reference the specific instance of my TimerPanel object within my InputPanel, but I cannot figure out how to do this...perhaps because of a fundamental flaw in my design or understanding? Please educate me. Thanks!

  2. #2
    Join Date
    Jan 2011
    Posts
    24

    Re: Newb with (probably) a fundamental misunderstanding

    do you want to call this method on user action or you just want to call them directly, if you want to call them on user action than put a button and implement ActionListener , otherwise just call them what's the problem on that, perhaps posting your code could help to better suggestion.

  3. #3
    Join Date
    Oct 2011
    Posts
    8

    Re: Newb with (probably) a fundamental misunderstanding

    Basically, when I press the radio button, I want to call the wClockStop() method of the TimerPanel class, but I want to call it on the specific instance of TimerPanel which is created in main.

    TimerPanel:

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    
    class TimerPanel extends JPanel{
    
    	int wHr = 0;
    	int wMin = 0;
    	int wSec = 0;
    
    	int bHr = 0;
    	int bMin = 0;
    	int bSec = 0;
    
    	NumberFormat formatter = new DecimalFormat("00");
    	JTextField bClock = new JTextField();
    	JTextField wClock = new JTextField();
    	JTextField wTimerField = new JTextField(5);
    	JTextField bTimerField = new JTextField(5);
    
    	Timer wClockTimer = new Timer(1000, new WClockListener());
    	Timer bClockTimer = new Timer(1000, new BClockListener());
    
    	public TimerPanel(){
    
    		setLayout (new GridLayout(2, 1));
    		setBackground(Color.GRAY);
    
    		JPanel container = new JPanel();
    
    		wClock = new JTextField();
    		bClock = new JTextField();
    
    		bClock = bCreateClock();
    		wClock = wCreateClock();
    
    		add (bClock);
    		add (wClock);
    
    	}
    
    
    	public JTextField wCreateClock(){
    
    			wTimerField.setEditable(false);
    			wTimerField.setFont(new Font("sansserif", Font.PLAIN, 48));
    			wTimerField.setColumns(5);
    			wTimerField.setForeground(Color.black);
    			wTimerField.setBackground(Color.white);
    
    			wTimerField.setText("00:00:00");
    
    			return wTimerField;
    
    		}
    
    		class WClockListener implements ActionListener {
    			public void actionPerformed(ActionEvent event) {
    
    				wSec++;
    				if (wSec == 60)
    				{
    					wSec = 0;
    					wMin++;
    				}
    				if (wMin == 60)
    				{
    					wMin = 0;
    					wHr++;
    				}
    
    				wTimerField.setText("" + formatter.format(wHr) + ":" + formatter.format(wMin) + ":" + formatter.format(wSec));
    
    			}
    		}
    
    	public JTextField bCreateClock(){
    
    			bTimerField.setEditable(false);
    			bTimerField.setFont(new Font("sansserif", Font.PLAIN, 48));
    			bTimerField.setColumns(5);
    			bTimerField.setForeground(Color.white);
    			bTimerField.setBackground(Color.black);
    
    			bTimerField.setText("00:00:00");
    
    			return bTimerField;
    
    		}
    
    		class BClockListener implements ActionListener {
    			public void actionPerformed(ActionEvent event) {
    
    				bSec++;
    				if (bSec == 60)
    				{
    					bSec = 0;
    					bMin++;
    				}
    				if (bMin == 60)
    				{
    					bMin = 0;
    					bHr++;
    				}
    
    				bTimerField.setText("" + formatter.format(bHr) + ":" + formatter.format(bMin) + ":" + formatter.format(bSec));
    
    			}
    		}
    
    
    		public void bClockStop(){
    			bClockTimer.stop();
    			wClockTimer.start();
    		}
    
    		public void wClockStop(){
    			wClockTimer.stop();
    			bClockTimer.start();
    		}
    	}
    InputPanel:

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class InputPanel extends JPanel{
    
        public InputPanel(){
    
    	JRadioButton radio = new JRadioButton();
    	SelectListener selectListener = new SelectListener();
    	radio.addActionListener (selectListener);
    
    	add (radio);
    
        }
    
    
        private class SelectListener implements ActionListener
        {
           public void actionPerformed (ActionEvent event)
           {
    		//Here is where I want to call the wClockStop() method of the TimerPanel class,
    		//but I want to call it on the specific instance of TimerPanel which is created in main.
           }
     	}
    }
    main:

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class main
    {
       //-----------------------------------------------------------------
       //  Creates and prints a random set of playing cards.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
    
          JFrame frame = new JFrame ("Example");
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
    	  JPanel panel = new JPanel();
    	  TimerPanel timer = new TimerPanel();
    	  InputPanel input = new InputPanel();
    
    	  panel.add (timer);
    	  panel.add (input);
    
    	  panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    
          frame.setResizable(false);
          frame.setPreferredSize(new Dimension (500, 250));
          frame.getContentPane().add(panel);
          frame.pack();
          frame.setVisible(true);
       }
    }

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Newb with (probably) a fundamental misunderstanding

    There are several ways of doing this, the simplest and crudest way is to do the following:

    When you create an instance of your InputPanel pass it a reference to the TimerPanel you have already created. To do this you will need to change your InputPanel's constructor to take a parameter of type TimerPanel and store the reference in an instance variable so you can use it later to start and stop the timer. ie

    Code:
    private TimerPanel timer;
    
    public InputPanel (TimerPanel p) {
       timer = p;
    }
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Oct 2011
    Posts
    8

    Re: Newb with (probably) a fundamental misunderstanding

    Thanks for the help. I'm sure your solution is exactly what I'm looking for, but I still don't know how to implement it. I tried to post exact code, but it says that a moderator needs to approve the post. I'm going to try to attach the code in a zip file. I'd really appreciate it if you could give me a more in-depth explanation after seeing the code.
    Attached Files Attached Files

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Newb with (probably) a fundamental misunderstanding

    but it says that a moderator needs to approve the post.
    That's because you are a new poster, although it looks like you have been approved now.
    I'm going to try to attach the code in a zip file. I'd really appreciate it if you could give me a more in-depth explanation after seeing the code.
    Sorry but I generally don't download from posted links, can you try posting in line again. I'm not sure how much time I can spend on the forum for the next week but I'm sure someone else will help if I can't.

    Briefly you want to be creating your instances like this:
    Code:
    TimerPanel timer = new TimerPanel();
    InputPanel input = new InputPanel(timer);
    Modify your InputPanel's constructor as I suggested in the earlier post and thats it, you now have a reference to your instance of the TimerPanel in your instance of the InputPanel.

    Within the InputPanel I am assuming you have you controls eg JButtons that when clicked will start and stop the timer. For each JButton add an ActionListener that performs the action ie


    Code:
    	JButton startButton = new JButton("Start");
    	startButton.addActionListener(new ActionListener()
    		{
    		@Override
    		public void actionPerformed(ActionEvent e)
    			{
    			timer.start();
    			}
    		});
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Oct 2011
    Posts
    8

    Re: Newb with (probably) a fundamental misunderstanding

    Ok, here goes...

    TimerPanel:

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    
    class TimerPanel extends JPanel{
    
    	int wHr = 0;
    	int wMin = 0;
    	int wSec = 0;
    
    	int bHr = 0;
    	int bMin = 0;
    	int bSec = 0;
    
    	NumberFormat formatter = new DecimalFormat("00");
    	JTextField bClock = new JTextField();
    	JTextField wClock = new JTextField();
    	JTextField wTimerField = new JTextField(5);
    	JTextField bTimerField = new JTextField(5);
    
    	Timer wClockTimer = new Timer(1000, new WClockListener());
    	Timer bClockTimer = new Timer(1000, new BClockListener());
    
    	public TimerPanel(){
    
    		setLayout (new GridLayout(2, 1));
    		setBackground(Color.GRAY);
    
    		JPanel container = new JPanel();
    
    		wClock = new JTextField();
    		bClock = new JTextField();
    
    		bClock = bCreateClock();
    		wClock = wCreateClock();
    
    		add (bClock);
    		add (wClock);
    
    	}
    
    
    	public JTextField wCreateClock(){
    
    			wTimerField.setEditable(false);
    			wTimerField.setFont(new Font("sansserif", Font.PLAIN, 48));
    			wTimerField.setColumns(5);
    			wTimerField.setForeground(Color.black);
    			wTimerField.setBackground(Color.white);
    
    			wTimerField.setText("00:00:00");
    
    			return wTimerField;
    
    		}
    
    		class WClockListener implements ActionListener {
    			public void actionPerformed(ActionEvent event) {
    
    				wSec++;
    				if (wSec == 60)
    				{
    					wSec = 0;
    					wMin++;
    				}
    				if (wMin == 60)
    				{
    					wMin = 0;
    					wHr++;
    				}
    
    				wTimerField.setText("" + formatter.format(wHr) + ":" + formatter.format(wMin) + ":" + formatter.format(wSec));
    
    			}
    		}
    
    	public JTextField bCreateClock(){
    
    			bTimerField.setEditable(false);
    			bTimerField.setFont(new Font("sansserif", Font.PLAIN, 48));
    			bTimerField.setColumns(5);
    			bTimerField.setForeground(Color.white);
    			bTimerField.setBackground(Color.black);
    
    			bTimerField.setText("00:00:00");
    
    			return bTimerField;
    
    		}
    
    		class BClockListener implements ActionListener {
    			public void actionPerformed(ActionEvent event) {
    
    				bSec++;
    				if (bSec == 60)
    				{
    					bSec = 0;
    					bMin++;
    				}
    				if (bMin == 60)
    				{
    					bMin = 0;
    					bHr++;
    				}
    
    				bTimerField.setText("" + formatter.format(bHr) + ":" + formatter.format(bMin) + ":" + formatter.format(bSec));
    
    			}
    		}
    
    
    		public void bClockStop(){
    			bClockTimer.stop();
    			wClockTimer.start();
    		}
    
    		public void wClockStop(){
    			wClockTimer.stop();
    			bClockTimer.start();
    		}
    	}
    InputPanel:

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class InputPanel extends JPanel{
    
    	public InputPanel(){
    		radioButton();
    		showPanel();
    	}
    
        public JPanel radioButton(){
    
    	JPanel panel = new JPanel();
    	JRadioButton radio = new JRadioButton();
    	SelectListener selectListener = new SelectListener();
    
    	radio.addActionListener (selectListener);
    	panel.add (radio);
    
    	return panel;
    
        }
    
        private class SelectListener implements ActionListener
        {
           public void actionPerformed (ActionEvent event)
           {
    		//Here is where I want to call the wClockStop() method of the TimerPanel class,
    		//but I want to call it on the specific instance of TimerPanel which is created in main.
           }
     	}
    
         private void showPanel(){
     	   JPanel panels = radioButton();
    
     	     this.add(panels);
    
        }
    }
    main:

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class main
    {
       //-----------------------------------------------------------------
       //  Creates and prints a random set of playing cards.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
    
          JFrame frame = new JFrame ("Example");
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
    	  JPanel panel = new JPanel();
    	  TimerPanel timer = new TimerPanel();
    	  InputPanel input = new InputPanel();
    
    	  panel.add (timer);
    	  panel.add (input);
    
    	  panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    
          frame.setResizable(false);
          frame.setPreferredSize(new Dimension (500, 250));
          frame.getContentPane().add(panel);
          frame.pack();
          frame.setVisible(true);
       }
    }

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Newb with (probably) a fundamental misunderstanding

    Do as I suggested in my earlier posts and it will work
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  9. #9
    Join Date
    Oct 2011
    Posts
    8

    Re: Newb with (probably) a fundamental misunderstanding

    Quote Originally Posted by keang View Post
    Do as I suggested in my earlier posts and it will work
    Got it. Thank you!

  10. #10
    Join Date
    Oct 2011
    Posts
    8

    Re: Newb with (probably) a fundamental misunderstanding

    Ok, now I am running into a similar problem... I'd also like to pass a reference of my instance of InputPanel to my instance of TimerPanel...

    The problem as you might imagine is that when I try to pass the reference to TimerPanel, InputPanel hasn't actually been created yet..

    Code:
    	  TimerPanel timer = new TimerPanel(input);
    	  InputPanel input = new InputPanel(control);
    
    	  panel.add (timer);
    	  panel.add (input);
    Kind of a chicken and egg situation...

  11. #11
    Join Date
    Oct 2011
    Posts
    8

    Re: Newb with (probably) a fundamental misunderstanding

    Anyone? I'm completely stumped here. I'd really appreciate any input at all.

  12. #12
    Join Date
    Oct 2011
    Posts
    8

    Re: Newb with (probably) a fundamental misunderstanding

    I played around for a bit and I think I may have found a solution... does this make any sense?

    Code:
    TimerPanel timer = null;
    InputPanel input = null;
    
    timer = new TimerPanel(input);
    input = new InputPanel(control);
    
    	  panel.add (timer);
    	  panel.add (input);
    EDIT: nevermind, I don't think this will work.

  13. #13
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Newb with (probably) a fundamental misunderstanding

    You're right, it wont work. But what you can do is go back to using a no parameter constructor and add a method to your Timer class to set the input panel reference ie setInputPanel(..).

    However when you get circular situations like this (resulting in very high coupling between classes) you should look carfeully at your design as it's often a sign that your design is weak. A good solution, which reduces the couping between the classes, is to have your classes fire events that any other interested class can listen for.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured