Hi All,

As you've probably read a million times, I am a newbie to java programming and need some help with uni assignment. I need to know how to numerical update a Textarea by clicking on a button. This would be a lot simplier if I could do it one class but my teacher wants both a Button Panel and Response Panel, which she then wants put in to a Center Panel.

I have started the code but am completey lost. Any help would be greatly appreciated.

Below are the three panel codes with a Frame code to finally put it in. I have written the driver code to run it with no problem so I wont post that. Any thing that is commented out is code that I'm not totally sure about.

public class MySurvey extends JFrame
{
	private CenterPanel centerPanel;
	private QuestionPanel questionPanel;
	private ButtonPanel buttonPanel;
	private ResponsePanel responsePanel;

	public MySurvey (String header)
	{

	JFrame MySurvey = JFrame();


	set.Title("Student Survey");

	JPanel CenterPanel = new JPanel();
	JPanel QuestionPanel = new JPanel();
	JPanel ButtonPanel = new JPanel();
	JPanel Response = new JPanel();

	/*getContentPane().add(CenterPanel, BorderLayout.CENTER);
	getContentPane().add(QuestionPanel, BorderLayout.SOUTH);
	getContentPane().add(ButtonPanel, BorderLayout.NORTH);
	getContentPane().add(ResponsePanel, BorderLayout.WEST);
	*/
	MySurvey.add( CenterPanel, QuestionPanel, ButtonPanel, ResponsePanel ); // add JPanels to frame


	}


}//end class

Below is the center panel that joins the two panels.

public class CenterPanel extends MySurvey
{
	public CenterPanel()
	{
	//to group ButtonPanel and ResponsePanel
	CenterPanel.setLayout(new BorderLayout());
	CenterPanel.add(QuestionPanel, BorderLayout.WEST);
	CenterPanel.add(ResponsePanel, BorderLayout.EAST);
	}
}

Below are the two Panels that go into the CenterPanel

public class ResponsePanel extends MySurvey
{
	JTextField txt1 = new JTextField(5);
	JTextField txt2 = new JTextField(5);
	JTextField txt3 = new JTextField(5);
	JTextField txt4 = new JTextField(5);
	JTextField txt5 = new JTextField(5);

	public void init()
		{

		ButtonActionListener bal = new ButtonActionListener(this);
		txt1.addActionListener(bal);
		txt2.addActionListener(bal);
		txt3.addActionListener(bal);
		txt4.addActionListener(bal);
		txt5.addActionListener(bal);

		Container cp = getContentPane();
		cp.setLayout(new FlowLayout());
		cp.add(txt1);
		cp.add(txt2);
		cp.add(txt3);
		cp.add(txt4);
		cp.add(txt5);

	}
}

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ButtonPanel extends MySurvey
{
	Button b1 = new Button ("BUTTON 1");
	Button b2 = new Button ("BUTTON 2");
	Button b3 = new Button ("BUTTON 3");
	Button b4 = new Button ("BUTTON 4");
	Button b5 = new Button ("BUTTON 5");


	public void init()
	{

	add(b1);
	add(b2);
	add(b3);
	add(b4);
	add(b5);


	/*ButtonActionListener bal = new ButtonActionListener(this);
	b1.addActionListener(bal);
	b2.addActionListener(bal);
	b3.addActionListener(bal);
	b4.addActionListener(bal);
	b4.addActionListener(bal);
	Container cp = getContentPane();
	cp.setLayout(new FlowLayout());
	cp.add(b1);
	cp.add(b2);
	cp.add(b3);
	cp.add(b4);
	cp.add(b5);
	*/
	}

	//public void setButtonText (String name)
	//{ txt1.setText(name); }
	//for setting text are answers for buttons clicked



}

Cheers all

VernonDozier commented: Used code tags on first post. +14

Recommended Answers

All 27 Replies

You can do all of this in one class if you really want to, but there is no need to do so - you can just as easily do it the way you have things set up. The way to make a button do something when it is clicked is to have your class implement ActionListener. Then in the actionPerformed method, you would do textFieldName.setText("text or variable name in here");

See this for more information and examples. If you don't know about ActionListener, or Listeners in general (which it appears you don't by looking at your classes) read the link they provide to ActionListener.

MySurvey extends JFrame. You have CenterPanel and ResponsePanel extending MySurvey. Therefore they are extending JFrame. If they are panels that you are adding to a JFrame, these classes should extend JPanel, not MySurvey.

Good observation Vernon. Man, I'm slacking lately. It's those exams coming up.

:(

Thanks for help so far. I do know a little bit about ActionListeners but not a whole lot. I've added the ActionListeners in the ButtonPanel by the following....

ButtonActionListener bal = new ButtonActionListener(this);
	b1.addActionListener(bal);
	b2.addActionListener(bal);
	b3.addActionListener(bal);
	b4.addActionListener(bal);
	b4.addActionListener(bal);

Then I've tried to perform the action with them with the following code.

public void actionPerformed(ActionEvent e)
{
   if (e.getSource( ) == b1)
      txt1.setText("yay");
   else if (e.getSource( ) == b2)
      txt2.setText("yay");
   else if (e.getSource( ) == b3)
      txt3.setText("yay");
   else if (e.getSource( ) == b4)
	  txt4.setText("yay");
   else if (e.getSource( ) == b5)
      txt5.setText("yay");
}

But because txt.1, txt2 etc... are in a different class I'm not sure how that works. Will I need to add actionlisteners to them as well or will the button class take care of updating these and how? I'm sorry If i'm not making sense... I'm only learning, Not too quickly either.....

I think you should probably post all the revised code, including any main function, since you clearly made some changes between your first and second posts and we'd have to guess what they are. Also, what precisely is the problem? Does it compile? Does it run? Does it display correctly initially? Does anything happen when buttons are pressed? Etc. Post all of your code and describe the problem please.

Any direction or help would be greatly appreciated... Here is all my current codes...

DRIVER CODE

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Driver
{

	public static void main(String [] args)
	{
		MySurvey surveyFrame = new MySurvey("Student Survey");
		surveyFrame.setSize (500,150);
		surveyFrame.setVisible (true);
		surveyFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
	}//end of main

}//end of driver class

FRAME CODE

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class MySurvey extends JFrame
{
	private JPanel CenterPanel = new JPanel();
	private JPanel QuestionPanel = new JPanel();
	private JPanel ButtonPanel = new JPanel();//Not sure to create these in center panel
	private JPanel ResponsePanel = new JPanel();//Not sure to create these in center panel

	public MySurvey (String header)
	{



	JFrame MySurvey = new JFrame();

	/*getContentPane().add(CenterPanel, BorderLayout.CENTER);
	getContentPane().add(QuestionPanel, BorderLayout.SOUTH);
	getContentPane().add(ButtonPanel, BorderLayout.NORTH);
	getContentPane().add(ResponsePanel, BorderLayout.WEST);
	*/
	MySurvey.add( CenterPanel, QuestionPanel); // add JPanels to frame


	}


}//end class

CENTRE PANEL

public class CenterPanel extends JPanel
{
	public CenterPanel()
	{
	//to group ButtonPanel and ResponsePanel
	CenterPanel.setLayout(new BorderLayout());
	CenterPanel.add(QuestionPanel, BorderLayout.WEST);
	CenterPanel.add(ResponsePanel, BorderLayout.EAST);
	}
}

QUESTION PANEL

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;


public class QuestionPanel extends JPanel

{
	public QuestionPanel()
	{
	//harder way of doing it
	//label1 = new JLabel("Please click on your response to \"There should be ASSIGNMENTS in RCM2311\"");
	//QuestionPanel.add(label1);

	//easier way of doing it
	QuestionPanel.add(new JLabel("Please click on your response to \"There should be ASSIGNMENTS in RCM2311\""));

	}
}

BUTTON PANEL

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ButtonPanel extends JPanel implements ActionListener
{
	Button b1 = new Button ("BUTTON 1");
	Button b2 = new Button ("BUTTON 2");
	Button b3 = new Button ("BUTTON 3");
	Button b4 = new Button ("BUTTON 4");
	Button b5 = new Button ("BUTTON 5");

	public void init()
	{

	ButtonActionListener bal = new ButtonActionListener(this);
	b1.addActionListener(bal);
	b2.addActionListener(bal);
	b3.addActionListener(bal);
	b4.addActionListener(bal);
	b4.addActionListener(bal);

	getContentPane().add(ButtonPanel);
	ButtonPanel.setLayout( new FlowLayout());
	ButtonPanel.add(b1);
	ButtonPanel.add(b2);
	ButtonPanel.add(b3);
	ButtonPanel.add(b4);
	ButtonPanel.add(b5);

	/*Container cp = getContentPane();
	cp.setLayout(new FlowLayout());
	cp.add(b1);
	cp.add(b2);
	cp.add(b3);
	cp.add(b4);
	cp.add(b5);
	*/

	}

	public void actionPerformed(ActionEvent e)
	{
   if (e.getSource( ) == b1)
      txt1.setText("yay");
   else if (e.getSource( ) == b2)
      txt2.setText("yay");
   else if (e.getSource( ) == b3)
      txt3.setText("yay");
   else if (e.getSource( ) == b4)
	  txt4.setText("yay");
   else if (e.getSource( ) == b5)
      txt5.setText("yay");

      /*txt1.setText("text or variable name in here");

	    Object o = e.getSource();
	    if(o == b1)
	      b1++;
	    else if(o == b2)
	       b2++;
	    else if(o == b3)
	       b3++;
	    else if(o == b4)
	       b4++;
	    else if(o == b5)
	       b5++;
		*/
	}

	//public void setButtonText (String name)
	//{ txt1.setText(for(int i=1; i<1000; i++));
	//}

	//public void setButtonText (String name)
	//{ txt1.setText(name); }
	//for setting text are answers for buttons clicked



}

RESPONSE PANEL

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;



public class ResponsePanel extends JPanel implements ActionListener
{

		TextField txt1 = new JTextField();
		TextField txt2 = new JTextField();
		TextField txt3 = new JTextField();
		TextField txt4 = new JTextField();
		TextField txt5 = new JTextField();


	public void init()
		{

		ButtonActionListener bal = new ButtonActionListener(this);
		txt1.addActionListener(bal);
		txt2.addActionListener(bal);
		txt3.addActionListener(bal);
		txt4.addActionListener(bal);
		txt5.addActionListener(bal);

		ResponsePanel.add(txt1);
		ResponsePanel.add(txt2);
		ResponsePanel.add(txt3);
		ResponsePanel.add(txt4);
		ResponsePanel.add(txt5);


		/*Container cp = getContentPane();
		cp.setLayout(new FlowLayout());
		cp.add(txt1);
		cp.add(txt2);
		cp.add(txt3);
		cp.add(txt4);
		cp.add(txt5);
		*/
		}

}

All parts commented out are things i've thought or considering including... So yeah once again any help or direction with this problem would be awesome! I can't even get the program to display, so i've got no idea if what what I'm doing is right or wrong. With regards to the buttons. I am trying to have them so that once you click on them, they update the textareas with the counts of how many times they've been clicked. Hopefully this is useful.

First, let's look at your MySurvey class:

public class MySurvey extends JFrame
{
	private JPanel CenterPanel = new JPanel();
	private JPanel QuestionPanel = new JPanel();
	private JPanel ButtonPanel = new JPanel();//Not sure to create these in center panel
	private JPanel ResponsePanel = new JPanel();//Not sure to create these in center panel

	public MySurvey (String header)
	{
	JFrame MySurvey = new JFrame();

	/*getContentPane().add(CenterPanel, BorderLayout.CENTER);
	getContentPane().add(QuestionPanel, BorderLayout.SOUTH);
	getContentPane().add(ButtonPanel, BorderLayout.NORTH);
	getContentPane().add(ResponsePanel, BorderLayout.WEST);
	*/
	MySurvey.add( CenterPanel, QuestionPanel); // add JPanels to frame


	}
}//end class

Line 3: You're creating a new CenterPanel object which extends JPanel. Therefore JPanel should not be in there. CenterPanel is the name of the CLASS, so pick something else for the name of the OBJECT itself, like below.

private  CenterPanel centerPanel = new CenterPanel();

You want to call the CenterPanel constructor, not the JPanel constructor. Same thing with line 5 (ButtonPanel). You want this:

private  ButtonPanel buttonPanel = new ButtonPanel();

Get rid of lines 4 and 6 since you are making your QuestionPanel and ResponsePanel objects part of your CenterPanel. You'll add them in CenterPanel, not here.

Line 10 - delete this line. MySurvey already extends JFrame, so you don't need to call any constructor.

Line 17 - You are already INSIDE the MySurvey constructor. Therefore, replace MySurvey with the key word this .

I changed to a GridLayout (you can make it whatever you like) and split line 17 in two. Here's my altered version of your MySurvey class.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class MySurvey extends JFrame
{
	private CenterPanel centerPanel = new CenterPanel();
	private ButtonPanel buttonPanel = new ButtonPanel();

	public MySurvey (String header)
	{
            this.setLayout (new GridLayout (2, 1));
	    this.getContentPane ().add (centerPanel);
            this.getContentPane ().add (buttonPanel);
	}
}//end class

Let's look at CenterPanel. Many of the same problems.

public class CenterPanel extends JPanel
{
	public CenterPanel()
	{
	//to group ButtonPanel and ResponsePanel
	CenterPanel.setLayout(new BorderLayout());
	CenterPanel.add(QuestionPanel, BorderLayout.WEST);
	CenterPanel.add(ResponsePanel, BorderLayout.EAST);
	}
}

In lines 6 - 8, change CenterPanel to this for the reasons stated in my explanation of MySurvey. CenterPanel is a class, this is an instance of a class, which is what you need. I also changed, again, the layout to GridLayout, just because in my opinion, it's easier than BorderLayout. You can change it back to your liking, but I figured I'd give you something that would display. Here's the revised code:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JPanel;

public class CenterPanel extends JPanel
{
    private ResponsePanel responsePanel = new ResponsePanel ();
    private QuestionPanel questionPanel = new QuestionPanel ();

    public CenterPanel()
    {
        //to group ButtonPanel and ResponsePanel
        this.setLayout(new GridLayout (2,1));
        this.add(questionPanel);
        this.add(responsePanel);
    }
}

I took out all the ActionListeners. One thing at a time. You need to get it to compile and display. You have many of the same problems in several places in other classes. I changed the init () functions to constructors.

ButtonPanel.java

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ButtonPanel extends JPanel
{
	Button b1 = new Button ("BUTTON 1");
	Button b2 = new Button ("BUTTON 2");
	Button b3 = new Button ("BUTTON 3");
	Button b4 = new Button ("BUTTON 4");
	Button b5 = new Button ("BUTTON 5");

    
	public ButtonPanel ()
	{
	    this.setLayout( new FlowLayout());
	    this.add(b1);
	    this.add(b2);
	    this.add(b3);
	    this.add(b4);
	    this.add(b5);
	}
}

ResponsePanel.java

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ResponsePanel extends JPanel
{
	JTextField txt1 = new JTextField("A");
	JTextField txt2 = new JTextField("B");
	JTextField txt3 = new JTextField("C");
	JTextField txt4 = new JTextField("D");
	JTextField txt5 = new JTextField("E");


	public ResponsePanel ()
	{
                this.setLayout (new GridLayout (1, 5));
		this.add(txt1);
		this.add(txt2);
		this.add(txt3);
		this.add(txt4);
		this.add(txt5);
	}
}

QuestionPanel.java

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class QuestionPanel extends JPanel
{
	public QuestionPanel()
	{
	    this.add(new JLabel("Please click on your response to \"There should be ASSIGNMENTS in RCM2311\""));
	}
}

I figured the best way to help is to give you some functioning code so you can compare and contrast. The way I do it is A way to do it, but not the ONLY way. The driver program was fine, so I didn't change that. Note the keyword this , note that the constructor calls are no longer to JFrame () and JPanel () . It should run and compile now and you should see everything. If not, I made a typo in copying and pasting.

commented: Nice post. +20

Wow, you've been a great help VernonDozier. Thank you so much. Now i'm trying to implement the actionlisteners and was wondering if you could assist me on this. Here is my new code for ButtonPanel and Response Panel. I simply want the text areas to count everytime I click on a button. I can't seem to make headway with this....

RESPONSE PANEL

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class ResponsePanel extends JPanel implements ActionListener

{
	JTextField txt1 = new JTextField("A", 7);
	JTextField txt2 = new JTextField("B", 7);
	JTextField txt3 = new JTextField("C", 7);
	JTextField txt4 = new JTextField("D", 7);
	JTextField txt5 = new JTextField("E", 7);
	
	private ButtonPanel myButton;

	public ResponsePanel ()

	{

		this.setLayout (new GridLayout (5, 1));
		this.add(txt1);
		this.add(txt2);
		this.add(txt3);
		this.add(txt4);
		this.add(txt5);

	}

	public ButtonActionListener (ButtonClick listening)

			{
					myButton = listening;
			}

	public void actionPerformed(ActionEvent e)

			{
				if (e.getSource( ) == b1)
				      txt1.setText("one");
				   else if (e.getSource( ) == b2)
				      txt2.setText("two");
				   else if (e.getSource( ) == b3)
				      txt3.setText("three");
				   else if (e.getSource( ) == b4)
					  txt4.setText("four");
				   else if (e.getSource( ) == b5)
	      			  txt5.setText("five");
			}


}

BUTTON PANEL

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class ButtonPanel extends JPanel

{
	Button b1 = new Button ("BUTTON 1");
	Button b2 = new Button ("BUTTON 2");
	Button b3 = new Button ("BUTTON 3");
	Button b4 = new Button ("BUTTON 4");
	Button b5 = new Button ("BUTTON 5");


	public ButtonPanel ()

	{
			this.setLayout (new GridLayout (5, 1));
			//setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
			//this.setLayout( new BoxLayout ());
			//this.setLayout( new FlowLayout());
			this.add(b1);
			this.add(b2);
			this.add(b3);
			this.add(b4);
			this.add(b5);

			ButtonActionListener bal = new ButtonActionListener(this);
			b1.addActionListener(bal);
			b2.addActionListener(bal);
			b3.addActionListener(bal);
			b4.addActionListener(bal);
			b5.addActionListener(bal);

	}


}

I've never used ButtonActionListener and don't know how to. I've always used a plain old ActionListener. NetBeans is telling me I have to add this line at the top in order to use it:

import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonActionListener;

I still get errors though, even when I add that line. Since I've never used ButtonActionListener. Here's the docs page for it:

http://java.sun.com/javase/6/docs/api/javax/swing/plaf/basic/BasicOptionPaneUI.ButtonActionListener.html

In particular, note this line from it:

This inner class is marked "public" due to a compiler bug. This class should be treated as a "protected" inner class. Instantiate it only within subclasses of BasicOptionPaneUI.

Here's the docs for BasicOptionPaneUI:

http://java.sun.com/javase/6/docs/api/javax/swing/plaf/basic/BasicOptionPaneUI.html

My advice is, at this stage of your Java development, it would be easier to use the regular old ActionListener. Why worry about compiler bugs and inner classes if you don't have to yet, especially since as far as I can tell, a regular ActionListener will work just fine?

If you want to "listen" to the buttons, have ButtonPanel implement ActionListener. If you need the ButtonPanel and the ResponsePanel to know about each other and communicate with each other, you need to give them a way to find each other. You can do that with another attribute and set and get functions.

ResponsePanel.java

// code

private ButtonPanel buttonPanel;

// code

public ResponsePanel ()
{
    // constructor code
}

// more code

public void setbuttonPanel (ButtonPanel buttonpanel)
{
    buttonPanel = buttonpanel;
}

public ButtonPanel getbuttonPanel ()
{
    return buttonPanel;
}

Same thing in the ButtonPanel class:

ButtonPanel.java

// code

private ResponsePanel responsePanel;

// code

public ButtonPanel ()
{
    // constructor code
}

// more code

public void setResponsePanel (ResponsePanel responsepanel)
{
    responsePanel = responsepanel;
}

public ResponsePanel getresponsePanel ()
{
    return responsePanel;
}

Add the same get and set methods to your CenterPanel class too:

CenterPanel.java

public void setResponsePanel (ResponsePanel responsepanel)
{
    responsePanel = responsepanel;
}

public ResponsePanel getresponsePanel ()
{
    return responsePanel;
}

Then at the end of your MySurvey constructor, you can add these lines:

buttonPanel.setResponsePanel (centerPanel.getResponsePanel ());
centerPanel.getResponsePanel ().setButtonPanel (buttonPanel);

Now your ButtonPanel and ResponsePanel classes will be able to communicate with each other!

Note that the way you have it now, you have ResponsePanel implementing ActionListener . Instead, have ButtonPanel implement ActionListener .

After the changes I mentioned in the last post, put this in actionPerformed in ButtonPanel (similar to what you have in ResponsePanel now):

if (e.getSource( ) == b1)
     responsePanel.txt1.setText("one");

I've tried to implement all your suggestions and am getting a lot of compiler errors that I can't seem to work out. I've put the ButtonPanel, ResponsePanel and CenterPanel. Any idea what I'm doing wrong?

CENTERPANEL

import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class CenterPanel extends JPanel implements ActionListener

{

	 private ResponsePanel responsePanel = new ResponsePanel ();
	 private ButtonPanel buttonPanel = new ButtonPanel ();

	 public CenterPanel()

	 {
		    //to group ButtonPanel and ResponsePanel
	 		this.setLayout(new BorderLayout ());
	 		this.add(responsePanel, BorderLayout.EAST);
	 		this.add(buttonPanel, BorderLayout.CENTER);

	 }

	 public void setResponsePanel (ResponsePanel responsepanel)
	 {
	     responsePanel = responsepanel;
	 }

	 public ResponsePanel getresponsePanel ()
	 {
	     return responsePanel;
}

}

BUTTONPANEL

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class ButtonPanel extends JPanel implements ActionListener

{
	Button b1 = new Button ("BUTTON 1");
	Button b2 = new Button ("BUTTON 2");
	Button b3 = new Button ("BUTTON 3");
	Button b4 = new Button ("BUTTON 4");
	Button b5 = new Button ("BUTTON 5");


	public ButtonPanel ()

	{
			this.setLayout (new GridLayout (5, 1));
			//setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
			//this.setLayout( new BoxLayout ());
			//this.setLayout( new FlowLayout());
			this.add(b1);
			this.add(b2);
			this.add(b3);
			this.add(b4);
			this.add(b5);

			ButtonActionListener bal = new ButtonActionListener(this);
			b1.addActionListener(bal);
			b2.addActionListener(bal);
			b3.addActionListener(bal);
			b4.addActionListener(bal);
			b5.addActionListener(bal);

	}

	public void setResponsePanel (ResponsePanel responsepanel)
	{
	    responsePanel = responsepanel;
	}

	public ResponsePanel getresponsePanel ()
	{
	    return responsePanel;
	}

	public void actionPerformed(ActionEvent e)

	{

	if (e.getSource( ) == b1)
	     responsePanel.txt1.setText("one");
	    else if (e.getSource( ) == b2)
	    	responsePanel.txt2.setText("one");
	    	else if (e.getSource( ) == b3)
	     		responsePanel.txt3.setText("one");
	     		else if (e.getSource( ) == b4)
	  				responsePanel.txt4.setText("one");
	     			else if (e.getSource( ) == b5)
	    				responsePanel.txt5.setText("one");
	}


}

RESPONSEPANEL

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class ResponsePanel extends JPanel implements ActionListener

{
	JTextField txt1 = new JTextField("A", 7);
	JTextField txt2 = new JTextField("B", 7);
	JTextField txt3 = new JTextField("C", 7);
	JTextField txt4 = new JTextField("D", 7);
	JTextField txt5 = new JTextField("E", 7);

	//private ButtonPanel myButton;

	public ResponsePanel ()

	{

		this.setLayout (new GridLayout (5, 1));
		this.add(txt1);
		this.add(txt2);
		this.add(txt3);
		this.add(txt4);
		this.add(txt5);

	}

	public void setbuttonPanel (ButtonPanel buttonpanel)
	{
	    buttonPanel = buttonpanel;
	}

	public ButtonPanel getbuttonPanel ()
	{
	    return buttonPanel;
	}

	

}

Also instead of the action updating the textfield with the response "one".

responsePanel.txt1.setText("one");

I'd like to count how many times a person has clicked on the button and display this instead. Any help? Cheers

I forgot to get rid of ButtonListener out of the ButtonPanel. I have now taken it out......... I mean I have changed it to plain old ActionListener as you've advised.

Read post 10 again. You left some things out. See red lines.

At the top of ButtonPanel, you must have this:

ResponsePanel responsePanel;
	Button b1 = new Button ("BUTTON 1");
	Button b2 = new Button ("BUTTON 2");
	Button b3 = new Button ("BUTTON 3");
	Button b4 = new Button ("BUTTON 4");
	Button b5 = new Button ("BUTTON 5");

At the top of ResponsePanel:

ButtonPanel buttonPanel;
	JTextField txt1 = new JTextField("A", 7);
	JTextField txt2 = new JTextField("B", 7);
	JTextField txt3 = new JTextField("C", 7);
	JTextField txt4 = new JTextField("D", 7);
	JTextField txt5 = new JTextField("E", 7);

At the botom of the MySurvey constructor, make sure you have this if you don't already:

buttonPanel.setresponsePanel (centerPanel.getresponsePanel ());
centerPanel.getresponsePanel ().setbuttonPanel (buttonPanel);

Note that the case matters in your function declaration. Thus these two functions are not the same:

void setresponsePanel (ResponsePanel responsepanel)
void setResponsePanel (ResponsePanel responsepanel)

See red letters above. I actually messed up when posting originally in post 10 and didn't keep them completely consistent. Doesn't matter which way you do it (letter 'r' capitalized or not capitalized), but be consistent. I would say that below is the best way actually since it matches the data member (the data member is responsePanel):

void setresponsePanel (ResponsePanel responsepanel)

As far as the arguments go, I don't see any mistakes, but case counts here too.

void setresponsePanel (ResponsePanel responsepanel)
{
    responsePanel = responsepanel;
}

ResponsePanel is the name of your class.
responsePanel is the data member in ButtonPanel and should be different from ResponsePanel.
responsepanel is a temporary local variable used only for that function. It should be different from both ResponsePanel and responsePanel.

I seem to doing a little bit better now, with a lot of direction. I only have two problems now. One to do with the ActionListener and the other to do with the following bit of code from the MySurveyPanel..

buttonPanel.setresponsePanel (centerPanel.getresponsePanel ());
	 centerPanel.getresponsePanel ().setbuttonPanel (buttonPanel);

The compiler is saying it cannot find symbol variable for buttonPanel for both button panels.

The other problem I'm having is obviously to do with ActionListeners. Below is my current code for ButtonPanel.

BUTTONPANEL

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class ButtonPanel extends JPanel implements ActionListener

{
	ResponsePanel responsePanel;

	Button b1 = new Button ("BUTTON 1");
	Button b2 = new Button ("BUTTON 2");
	Button b3 = new Button ("BUTTON 3");
	Button b4 = new Button ("BUTTON 4");
	Button b5 = new Button ("BUTTON 5");


	public ButtonPanel ()

	{
			this.setLayout (new GridLayout (5, 1));
			//setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
			//this.setLayout( new BoxLayout ());
			//this.setLayout( new FlowLayout());
			this.add(b1);
			this.add(b2);
			this.add(b3);
			this.add(b4);
			this.add(b5);

			ActionListener bal = new ActionListener();
			b1.addActionListener(bal);
			b2.addActionListener(bal);
			b3.addActionListener(bal);
			b4.addActionListener(bal);
			b5.addActionListener(bal);

	}

	public void setresponsePanel (ResponsePanel responsepanel)
	{
	    responsePanel = responsepanel;
	}

	public ResponsePanel getresponsePanel ()
	{
	    return responsePanel;
	}

	public void actionPerformed(ActionEvent e)

	{

	if (e.getSource( ) == b1)
	     responsePanel.txt1.setText("one");
	    else if (e.getSource( ) == b2)
	    	responsePanel.txt2.setText("one");
	    	else if (e.getSource( ) == b3)
	     		responsePanel.txt3.setText("one");
	     		else if (e.getSource( ) == b4)
	  				responsePanel.txt4.setText("one");
	     			else if (e.getSource( ) == b5)
	    				responsePanel.txt5.setText("one");
	}


}

The compiler is complaining that ActionListener is abstract and cannot be instantiated. It is saying this about the following line of code.

ActionListener bal = new ActionListener();

Any ideas what I'm doing wrong. As far as I can tell everything should be working but that isn't the case.

For the first problem, if the MySurvey class (I don't know what MySurveyPanel class is) is the way I suggested, as below:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class MySurvey extends JFrame
{
	private CenterPanel centerPanel = new CenterPanel();
	private ButtonPanel buttonPanel = new ButtonPanel();

	public MySurvey (String header)
	{
            this.setLayout (new GridLayout (2, 1));
	    this.getContentPane ().add (centerPanel);
            this.getContentPane ().add (buttonPanel);
            buttonPanel.setresponsePanel (centerPanel.getresponsePanel ());
            centerPanel.getresponsePanel ().setbuttonPanel (buttonPanel);
	}
}//end class

then the error doesn't make sense to me. Post the exact error message, including the exact line that it comes from and the exact class that it comes from. Make sure that the red above is all the same, as well as the green. Capitalization counts! Look in CenterPanel and ResponsePanel too and make sure the capitalization matches there too.

If so, repost the CenterPanel and ResponsePanel classes as well as the MySurvey class, and again, the EXACT error messages along with the lines and classes that the error messages come from.

For the other error: you shouldn't be creating a new ActionListener in ButtonPanel. Use the one you already have inside the ButtonPanel class, which is below:

public void actionPerformed(ActionEvent e)

	{

	if (e.getSource( ) == b1)
	     responsePanel.txt1.setText("one");
	    else if (e.getSource( ) == b2)
	    	responsePanel.txt2.setText("one");
	    	else if (e.getSource( ) == b3)
	     		responsePanel.txt3.setText("one");
	     		else if (e.getSource( ) == b4)
	  				responsePanel.txt4.setText("one");
	     			else if (e.getSource( ) == b5)
	    				responsePanel.txt5.setText("one");
	}

To use the code above as your Action Listener from inside ButtonPanel, again use the magical keyword this .

So change bal to this everywhere below:

b1.addActionListener(bal);
			b2.addActionListener(bal);
			b3.addActionListener(bal);
			b4.addActionListener(bal);
			b5.addActionListener(bal);

and delete the line below:

ActionListener bal = new ActionListener();
commented: Very detailed and useful help. +4

First of all I accidentally said MySurveyPanel instead of MySurvey class , sorry. Then what I've done differently is to add ResponsePanel and ButtonPanel to CenterPanel. So my code for MySurvey is a little different. Here is my code for all the above mentioned.

MySurvey

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class MySurvey extends JFrame
{
	private CenterPanel centerPanel = new CenterPanel();
	private QuestionPanel questionPanel = new QuestionPanel();

	public MySurvey (String header)

	{


	 this.setLayout (new BorderLayout ());
	 this.getContentPane ().add (centerPanel, BorderLayout.SOUTH);
	 this.getContentPane ().add (questionPanel, BorderLayout.NORTH);


	 buttonPanel.setresponsePanel (centerPanel.getresponsePanel ());
	 centerPanel.getresponsePanel ().setbuttonPanel (buttonPanel);

	}




}//end class

ButtonPanel

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class ButtonPanel extends JPanel implements ActionListener

{
	ResponsePanel responsePanel;

	Button b1 = new Button ("BUTTON 1");
	Button b2 = new Button ("BUTTON 2");
	Button b3 = new Button ("BUTTON 3");
	Button b4 = new Button ("BUTTON 4");
	Button b5 = new Button ("BUTTON 5");


	public ButtonPanel ()

	{
			this.setLayout (new GridLayout (5, 1));
			//setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
			//this.setLayout( new BoxLayout ());
			//this.setLayout( new FlowLayout());
			this.add(b1);
			this.add(b2);
			this.add(b3);
			this.add(b4);
			this.add(b5);

			//ActionListener bal = new ActionListener();
			b1.addActionListener(this);
			b2.addActionListener(this);
			b3.addActionListener(this);
			b4.addActionListener(this);
			b5.addActionListener(this);

	}


	public void setresponsePanel (ResponsePanel responsepanel)
	{
	    responsePanel = responsepanel;
	}

	public ResponsePanel getresponsePanel ()
	{
	    return responsePanel;
	}

	public void actionPerformed(ActionEvent e)

	{

	if (e.getSource( ) == b1)
	     responsePanel.txt1.setText("one");
	    else if (e.getSource( ) == b2)
	    	responsePanel.txt2.setText("one");
	    	else if (e.getSource( ) == b3)
	     		responsePanel.txt3.setText("one");
	     		else if (e.getSource( ) == b4)
	  				responsePanel.txt4.setText("one");
	     			else if (e.getSource( ) == b5)
	    				responsePanel.txt5.setText("one");
	}


}

ResponsePanel

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class ResponsePanel extends JPanel

{
	ButtonPanel buttonPanel;

	JTextField txt1 = new JTextField("A", 7);
	JTextField txt2 = new JTextField("B", 7);
	JTextField txt3 = new JTextField("C", 7);
	JTextField txt4 = new JTextField("D", 7);
	JTextField txt5 = new JTextField("E", 7);

	//private ButtonPanel myButton;

	public ResponsePanel ()

	{

		this.setLayout (new GridLayout (5, 1));
		this.add(txt1);
		this.add(txt2);
		this.add(txt3);
		this.add(txt4);
		this.add(txt5);

	}

	public void setbuttonPanel (ButtonPanel buttonpanel)
	{
	    buttonPanel = buttonpanel;
	}

	public ButtonPanel getbuttonPanel ()
	{
	    return buttonPanel;
	}



}

Post CenterPanel too please.

import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class CenterPanel extends JPanel

{

	 private ResponsePanel responsePanel = new ResponsePanel ();
	 private ButtonPanel buttonPanel = new ButtonPanel ();

	 public CenterPanel()

	 {
		    //to group ButtonPanel and ResponsePanel
	 		this.setLayout(new BorderLayout ());
	 		this.add(responsePanel, BorderLayout.EAST);
	 		this.add(buttonPanel, BorderLayout.CENTER);

	 }

	 public void setresponsePanel (ResponsePanel responsepanel)
	 {
	     responsePanel = responsepanel;
	 }

	 public ResponsePanel getresponsePanel ()
	 {
	     return responsePanel;
}

}

Here's the deal. Since you took buttonPanel out of MySurvey, these lines in MySurvey mess things up:

buttonPanel.setresponsePanel (centerPanel.getresponsePanel ());
centerPanel.getresponsePanel ().setbuttonPanel (buttonPanel);

It's looking for buttonPanel in MySurvey, which doesn't exist.

Take these lines out of MySurvey:

buttonPanel.setresponsePanel (centerPanel.getresponsePanel ());
centerPanel.getresponsePanel ().setbuttonPanel (buttonPanel);

and put these lines at the bottom of the CenterPanel constructor:

buttonPanel.setresponsePanel (responsePanel);
responsePanel.setbuttonPanel (buttonPanel);

Cheers, works well now... I do have another question though... I can't seem to get the textfield to add an incremental value every time I click on a button. Here is my current ButtonPanel.

BUTTONPANEL

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class ButtonPanel extends JPanel implements ActionListener

{
	ResponsePanel responsePanel;

	Button b1 = new Button ("BUTTON 1");
	Button b2 = new Button ("BUTTON 2");
	Button b3 = new Button ("BUTTON 3");
	Button b4 = new Button ("BUTTON 4");
	Button b5 = new Button ("BUTTON 5");

	int numClicks1 = 0;
	int numClicks2 = 0;
	int numClicks3 = 0;
	int numClicks4 = 0;
	int numClicks5 = 0;


	public ButtonPanel ()

	{
			this.setLayout (new GridLayout (5, 1));
			//setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
			//this.setLayout( new BoxLayout ());
			//this.setLayout( new FlowLayout());
			this.add(b1);
			this.add(b2);
			this.add(b3);
			this.add(b4);
			this.add(b5);

			//ActionListener bal = new ActionListener();
			b1.addActionListener(this);
			b2.addActionListener(this);
			b3.addActionListener(this);
			b4.addActionListener(this);
			b5.addActionListener(this);

	}


	public void setresponsePanel (ResponsePanel responsepanel)
	{
	    responsePanel = responsepanel;
	}

	public ResponsePanel getresponsePanel ()
	{
	    return responsePanel;
	}

	public void actionPerformed(ActionEvent e)

	{
		numClicks1++;
		numClicks2++;
		numClicks3++;
		numClicks4++;
		numClicks5++;

	if (e.getSource( ) == b1)
	     responsePanel.txt1.setText(numClicks1);
	    else if (e.getSource( ) == b2)
	    	responsePanel.txt2.setText(numClicks2);
	    	else if (e.getSource( ) == b3)
	     		responsePanel.txt3.setText(numClicks3);
	     		else if (e.getSource( ) == b4)
	  				responsePanel.txt4.setText(numClicks4);
	     			else if (e.getSource( ) == b5)
	    				responsePanel.txt5.setText(numClicks5);
	}


}

Will I need to change the code in the ButtonPanel or the ResponsePanel? Just in case here is my current ResponsePanel.

RESPONSEPANEL

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class ResponsePanel extends JPanel

{
	ButtonPanel buttonPanel;


	JTextField txt1 = new JTextField("0", 7);
	JTextField txt2 = new JTextField("0", 7);
	JTextField txt3 = new JTextField("0", 7);
	JTextField txt4 = new JTextField("0", 7);
	JTextField txt5 = new JTextField("0", 7);

	//private ButtonPanel myButton;

	public ResponsePanel ()

	{

		this.setLayout (new GridLayout (5, 1));
		this.add(txt1);
		this.add(txt2);
		this.add(txt3);
		this.add(txt4);
		this.add(txt5);

	}

	public void setbuttonPanel (ButtonPanel buttonpanel)
	{
	    buttonPanel = buttonpanel;
	}

	public ButtonPanel getbuttonPanel ()
	{
	    return buttonPanel;
	}



}

The problem i'm having when compiling is that the textfield doesn't recognize the int value. Any ideas?

Will I need to change the code in the ButtonPanel or the ResponsePanel?

Change it in ButtonPanel. That's where your setText function call is:

if (e.getSource( ) == b1)
	     responsePanel.txt1.setText(numClicks1);

setText takes a String, not an integer, so you need to convert your integer to a String using Integer.toString:

if (e.getSource( ) == b1)
	     responsePanel.txt1.setText (Integer.toString (numClicks1));

cheers for the toString. I'm now having trying trying to include the numClicks1++ in the if else statements. Adding them all the start just increments them all the time if one button is pressed. Any ideas how I should include them in the if statement? Or am I going off in the wrong tangent here?

Ignore my Last Post. I have solved the problem for myself. May be posting some more problems later. Thank you very much VernonDozier!

Ok now that I've got the whole program working I want to add some extra parts. I want to add three radioButtons underneath the question statement and when the radioButtons are added I want to have a statement below be shown for each click on all different buttons. I'm told to implement this through itemListener and NO other listener is to be used instead. Once the radioButton is clicked and the message is displayed I'd like to have the buttonpanel and responsepanel be new and seperate for each of the radioButtons. The only panel class i've changed since the previous one is QuestionPanel. It is displayed below.

QUESTIONPANEL

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class QuestionPanel extends JPanel implements ItemListener

{
	JPanel p = new JPanel();

	JRadioButton QuestionOneButton = new JRadioButton("First", true);
	JRadioButton QuestionTwoButton = new JRadioButton("Second", false);
	JRadioButton QuestionThreeButton = new JRadioButton("Third", false);
	JLabel First = new JLabel("BLAH BLAH 1");
	JLabel Second = new JLabel("BLAH BLAH 2");
	JLabel Third = new JLabel("BLAH BLAH 3");


	public QuestionPanel()

	{
	    this.setLayout (new GridLayout (2, 3));
		this.add(new JLabel("Please click on your response to "));

		//Group the radio buttons.
		ButtonGroup group = new ButtonGroup();
		group.add(QuestionOneButton);
		group.add(QuestionTwoButton);
		group.add(QuestionThreeButton);


		p.add(QuestionOneButton);
		p.add(QuestionTwoButton);
		p.add(QuestionThreeButton);

		QuestionOneButton.addItemListener(this);
		QuestionTwoButton.addItemListener(this);
		QuestionThreeButton.addItemListener(this);

		p.add(First);
		p.add(Second);
		p.add(Third);

		p.setLayout (new GridLayout(2, 3, 0, 4));

		this.add(p);

	}

}

I am having trouble trying to implement the actionListener. The compiler is complaining that QuestionPanel is not abstract and does not override abstract method.

http://java.sun.com/docs/books/tutorial/uiswing/events/itemlistener.html

If you have a line like this in your class:

public class QuestionPanel extends JPanel implements ItemListener

then you must have this function in your class:

public void itemStateChanged(ItemEvent e)

You put your implementation in that function (i.e. what to do when the radio buttons are pressed).

Regarding having three separate ButtonPanels and three separate ResponsePanels, that's certainly doable, but before tackling it, I would go through the code now and make sure you understand it all and WHY everything works. Often you can comment out a function and see what error(s) that causes, then put it in again if need be. Sometimes you end up with functions you no longer need/use and you can comment them out or delete them. Let the compiler do the work of catching errors for you.

To start, keep in mind why I had you write the get and set functions. Initially, you were doing something like this:

public class ButtonPanel
{
    // code
    private ResponsePanel responsePanel;
    // code
}

as opposed to

public class ButtonPanel
{
    // code
    public ResponsePanel responsePanel;
    // code
}

or

public class ButtonPanel
{
    // code
    ResponsePanel responsePanel;
    // code
}

You eventually took the word private out. You need get and set methods for the first scenario above, but not the other two, so while you are learning, it may be best to make everything public and thus not have to write any get and set methods. Then when it's working, you can start making them private if you wish. Do it to one data member at a time. The compiler will give you errors if there is a problem. Look at the errors. That will tell you where you need to write get and set functions.

Have a look at this link regarding naming standards:

http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html#367

It's important to keep things consistent. Hence my suggestion earlier to make the class have every word start with a capital letter (i.e. CenterPanel) and the data members have the first word lower case (centerPanel). The compiler doesn't care, but it'll make your life easier. So code like this:

JRadioButton QuestionOneButton = new JRadioButton("First", true);

in general, is to be avoided. Do this instead:

JRadioButton questionOneButton = new JRadioButton("First", true);

It's all about experimentation, but hopefully this'll make the experimentation quicker. Make everything public first for now. if you get an error with the word "abstract" in it, look in the class for the word "implements". If you have it, chances are high that you forgot to add a function to your class. Finally, use an IDE like NetBeans. It will, in addition to flagging the errors, put a dot next to the line that is the problem. You then press that dot and it often gives you some options on how to fix them and will do those options FOR YOU if you click the dot again. It's a time saver and a great way to learn.

I've tried to implement all the previous advice. I am however having two problems. I can't seem to be able to create the three new CenterPanels to keep track of each button pressed for each JButton question. The second problem i'm having is with the JRadioButtons. When I run the gui, I am able to click on any of the buttons but when I click on the next Radio button I need to click twice? Any advice? I will post all my current code below but the driver which hasn't changed.

MySurvey

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class MySurvey extends JFrame
{
	private CenterPanel centerPanel = new CenterPanel();
	private QuestionPanel questionPanel = new QuestionPanel();

	public MySurvey (String header)

	{

	 this.setLayout (new BorderLayout ());
	 this.getContentPane ().add (centerPanel, BorderLayout.SOUTH);
	 this.getContentPane ().add (questionPanel, BorderLayout.CENTER);

	}

	public void setquestionPanel (QuestionPanel questionpanel)
		{
		    questionPanel = questionpanel;
		}

		public QuestionPanel getquestionPanel ()
		{
		    return questionPanel;
	    }




}//end class

CenterPanel

import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class CenterPanel extends JPanel

{
	QuestionPanel questionPanel;

	 public ResponsePanel responsePanel = new ResponsePanel ();
	 public ButtonPanel buttonPanel = new ButtonPanel ();

	 public CenterPanel()

	 {
		    //to group ButtonPanel and ResponsePanel
	 		this.setLayout(new BorderLayout ());
	 		this.add(responsePanel, BorderLayout.EAST);
	 		this.add(buttonPanel, BorderLayout.CENTER);

		    buttonPanel.setresponsePanel (responsePanel);
	        responsePanel.setbuttonPanel (buttonPanel);
	 }

	public void setquestionPanel (QuestionPanel questionpanel)
	{
	    questionPanel = questionpanel;
	}

	public QuestionPanel getquestionPanel ()
	{
	    return questionPanel;
	}


	 public void setresponsePanel (ResponsePanel responsepanel)
	 {
	     responsePanel = responsepanel;
	 }

	 public ResponsePanel getresponsePanel ()
	 {
	     return responsePanel;
	 }

}

QuestionPanel

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class QuestionPanel extends JPanel implements ItemListener

{

	CenterPanel centerPanel;
	CenterPanel centerpanel1;
	CenterPanel centerpanel2;
	CenterPanel centerpanel3;

	JPanel p = new JPanel();
	JPanel s = new JPanel();

	JRadioButton questionOneButton = new JRadioButton("First", false);
	JRadioButton questionTwoButton = new JRadioButton("Second", false);
	JRadioButton questionThreeButton = new JRadioButton("Third", false);

    JLabel first = new JLabel("Please click on any of the buttons for questions");

	public QuestionPanel()

	{
	    this.setLayout (new GridLayout (3, 3));
		this.add(new JLabel("Please click on your response to the following question"));

		//Group the radio buttons.
		ButtonGroup group = new ButtonGroup();
		group.add(questionOneButton);
		group.add(questionTwoButton);
		group.add(questionThreeButton);


		p.add(questionOneButton);
		p.add(questionTwoButton);
		p.add(questionThreeButton);

		questionOneButton.addItemListener(this);
		questionTwoButton.addItemListener(this);
		questionThreeButton.addItemListener(this);

		s.add(first);

		p.setLayout (new GridLayout(1, 3, 0, 3));

		this.add(p);
		this.add(s);

	}

	public void setcenterPanel (CenterPanel centerpanel)
		{
		    centerPanel = centerpanel;
		}

		public CenterPanel getcenterPanel ()
		{
		    return centerPanel;
		}


        public void itemStateChanged(ItemEvent e)
        {
            Object source = e.getItemSelectable();

        if (source == questionOneButton) {
			first.setText("BLAH 1");
			centerPanel.add(centerpanel1);
        } else if (source == questionTwoButton) {
			first.setText("BLAH 2");
			centerPanel.add(centerpanel2);
        } 		else if (source == questionThreeButton) {
			first.setText("BLAH 3");
			centerPanel.add(centerpanel3);

        }


        }

}

ButtonPanel

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class ButtonPanel extends JPanel implements ActionListener

{
	ResponsePanel responsePanel;

	Button b1 = new Button ("BUTTON 1");
	Button b2 = new Button ("BUTTON 2");
	Button b3 = new Button ("BUTTON 3");
	Button b4 = new Button ("BUTTON 4");
	Button b5 = new Button ("BUTTON 5");

	int numClicks1 = 0;
	int numClicks2 = 0;
	int numClicks3 = 0;
	int numClicks4 = 0;
	int numClicks5 = 0;


	public ButtonPanel ()

	{
			this.setLayout (new GridLayout (5, 1));
			this.add(b1);
			this.add(b2);
			this.add(b3);
			this.add(b4);
			this.add(b5);

			b1.addActionListener(this);
			b2.addActionListener(this);
			b3.addActionListener(this);
			b4.addActionListener(this);
			b5.addActionListener(this);

	}


	public void setresponsePanel (ResponsePanel responsepanel)
	{
	    responsePanel = responsepanel;
	}

	public ResponsePanel getresponsePanel ()
	{
	    return responsePanel;
	}

	public void actionPerformed(ActionEvent e)

	{

	if  (e.getSource( ) == b1){	numClicks1++;
	     responsePanel.txt1.setText(Integer.toString(numClicks1));}
	    else if (e.getSource( ) == b2){numClicks2++;
	    	responsePanel.txt2.setText(Integer.toString(numClicks2));}
	    	else if (e.getSource( ) == b3){numClicks3++;
	     		responsePanel.txt3.setText(Integer.toString(numClicks3));}
	     		else if (e.getSource( ) == b4){numClicks4++;
	  				responsePanel.txt4.setText(Integer.toString(numClicks4));}
	     			else if (e.getSource( ) == b5){numClicks5++;
	    				responsePanel.txt5.setText(Integer.toString(numClicks5));}
	}


}

ResponsePanel

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class ResponsePanel extends JPanel

{
	ButtonPanel buttonPanel;


	JTextField txt1 = new JTextField("0", 7);
	JTextField txt2 = new JTextField("0", 7);
	JTextField txt3 = new JTextField("0", 7);
	JTextField txt4 = new JTextField("0", 7);
	JTextField txt5 = new JTextField("0", 7);

	public ResponsePanel ()

	{
		this.setLayout (new GridLayout (5, 1));
		this.add(txt1);
		this.add(txt2);
		this.add(txt3);
		this.add(txt4);
		this.add(txt5);

	}

	public void setbuttonPanel (ButtonPanel buttonpanel)
	{
	    buttonPanel = buttonpanel;
	}

	public ButtonPanel getbuttonPanel ()
	{
	    return buttonPanel;
	}



}

I'm not 100% sure that I follow what you want to do, but I feel confident that you are going about it the wrong way.

if (source == questionOneButton) {
	first.setText("BLAH 1");
	centerPanel.add(centerpanel1);

You are adding a CenterPanel to another CenterPanel when an item state has changed (i.e. this is occurring AFTER the initialization). You can certainly do that, but it's very risky to add panels like that if you are just starting out. It's an extra level of difficulty since your layout is changing during the program. Generally (though not always), you add all of your components when you initialize everything in the constructors. Often you add or subtract components according to a listener, but my strong advice is for you to not try to tackle that until you get much more experienced in Java and LayoutManagers.

You are adding a CenterPanel to another CenterPanel above too. Again, perfectly legal, but not advised until you understand what is going on better. I also don't think that's what you intend to do, but again I'm not 100% sure.

My advice would be to lay everything out on paper, decide exactly what panels contain what components and what panels contain other panels and what panels are directly added to the JFrame. Instead of having 5 buttons and 3 radio buttons, have 2 buttons and 2 radio buttons. For now, make everything public and get rid of all of the get and set methods(you can make things private and add them later if you wish). You want to make your code shorter so you can experiment. It's easier to change when there are fewer components. You should probably mark this thread solved since it's gotten long and has changed a lot from what it was initially. If need be, start a new thread with a new question when you get stuck. New threads with shorter code tend to get you input from more people (people tend not to look at long threads if they haven't commented on the thread previously). Keep in mind that you'll have to explain things from the start if you start a new thread since many people haven't seen THIS thread. Best of luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.