Internal Component Changes

Posted on 2011-10-27
Last Modified: 2012-05-12
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);
				case 2:
					aQuestionPanel = new QuestionTypeText(aQuestions);
				case 3:
					aQuestionPanel = new QuestionTypeCombo(aQuestions);
				case 4:
					aQuestionPanel = new QuestionTypeNote(aQuestions);
					aQuestionPanel = new JLabel("Question Type Not Available (" +aQuestions.getQuestionType()+ ")");

				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);

Open in new window

Question by:CloudStrife209
    LVL 86

    Expert Comment

    >>and i'm guessing this is also where i would add the listener.

    Yes - what would be the problem with that?

    Author Comment

    no problem i just have no idea how
    LVL 10

    Accepted Solution

    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:

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Connect to IP Camera using Java 4 62
    factorial example challenge 10 44
    endX challenge 2 33
    computer science syllabus 3 40
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now