• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Internal Component Changes

I've created the below component and will be adding this to my main form

 component example
I'm trying to figure out how i would add either an action or a listener to the component to catch the change in radio button selection. Both buttons set a variable called Status which is a string value within the component itself, but i need to use that value to update something on the main form.

This components will be added while the program is running. I've included the section of code that selects the type of component to be added and adds it to the panel its going to part of, and i'm guessing this is also where i would add the listener.
for (int i = 0; i < theseQuestions.size(); i++) {
				aQuestions = theseQuestions.get(i);
				switch (aQuestions.getQuestionType()) {
				case 1:
					aQuestionPanel = new QuestionTypeRadio(aQuestions);
					break;
				case 2:
					aQuestionPanel = new QuestionTypeText(aQuestions);
					break;
				case 3:
					aQuestionPanel = new QuestionTypeCombo(aQuestions);
					break;
				case 4:
					aQuestionPanel = new QuestionTypeNote(aQuestions);
					break;
				default:
					aQuestionPanel = new JLabel("Question Type Not Available (" +aQuestions.getQuestionType()+ ")");
					break;
				}
			
				
				aQuestionPanel.getPropertyChangeListeners();
				

				gbc_Quesions.fill = GridBagConstraints.BOTH;
				gbc_Quesions.insets = new Insets(5, 5, 5, 0);
				gbc_Quesions.gridx = 0;
				gbc_Quesions.gridy = pnlQuestions.getComponentCount();
				pnlQuestions.add(aQuestionPanel, gbc_Quesions);
				
				contentPane.validate();
				contentPane.repaint();
			}

Open in new window

0
CloudStrife209
Asked:
CloudStrife209
1 Solution
 
CEHJCommented:
>>and i'm guessing this is also where i would add the listener.

Yes - what would be the problem with that?
0
 
CloudStrife209Author Commented:
no problem i just have no idea how
0
 
gordon_vt02Commented:
You probably just want to add PropertyChangeSupport to your class.  Give it addPropertyChangeListener(PropertyChangeListener) and removePropertyChangeListener(PropertyChangeLIstener) methods, then fire a propertyChanged event when the value of 'Status' changes.  Your main class can listen for that event, with the key 'Status' and act accordingly.  You can add a PropertyChangeSupport object to your component and delegate the addPCL(), removePCL(), and firePropertyChanged() methods to it so you don't have to worry about managing the listeners yourself.

API Docs:
java.beans.PropertyChangeListener
java.beans.PropertyChangeSupport
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now