CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2010
    Posts
    4

    Need help in Populating Items for 3 jcomboboxes by action listeners

    Can any one help me for populating the List of Items for 3 combo boxes at runtime by using action listeners

    The main idea is First I must load the data for the 3 combo boxes and

    when I select an Item in the 1st combobox then the related data should appear in the remaining 2 comboboxes.

    for example let the comboboxes should be APPSET, APP, PACKAGE

    APPSET(animals,houses)APP(horse,cat,dog,duplex,bunglow)PACKAGE(dog is pet, cat is pet, horse is pet, duplex is 2 storied, bunglow is more than 2)

    so if we select (animals) in APPSET then APP combox should populate the items as(horse,cat,dog)and PACKAGE should populate(dog is pet, cat is pet, horse is pet)

    and if we again select an item say(dog) from APP combobox then the PACKAGE combobox should display only (dog is pet)

    Please any one help me in this

    It is very urgent

    Thank you very much

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

    Re: Need help in Populating Items for 3 jcomboboxes by action listeners

    So what code do you have so far, and what exactly are you stuck on?

    Please remember to use code tags when posting code.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Sep 2010
    Posts
    4

    Re: Need help in Populating Items for 3 jcomboboxes by action listeners

    Hello keang

    Sir,

    I am using the following code and I used 2 actionlisteners 1 in the APPSET and the other in the APP

    Code:
    // creating AppSet Label
    	private JLabel getAppSetLabel() {
    		if (this.appSetLabel == null) {
    			this.appSetLabel = new JLabel("App Set:");
    		}
    		return this.appSetLabel;
    	}
    	//Getting data into APPSet Combo Box
    	private JComboBox getAppSetComboBox() {
    		if (this.appSetComboBox == null) {
    			this.appSetComboBox = new JComboBox();
    			// Populate the App Set list
    			this.appSetComboBox.addItem("All Application");
    			ArrayList<String> anAppSetList = CBRCPMDailyT20AppManager.instance()
    					.getAppSetList();
    			for (int i = 0; i < anAppSetList.size(); i++) {
    				this.appSetComboBox.addItem(anAppSetList.get(i));
    			}			
    			this.appSetComboBox.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent eventset) {
    					appSetComboBox_actionPerformed(eventset);
    				}
    			});
    			//this.getAppComboBox();
    			//this.appSetComboBox.removeActionListener(appSetComboBox);
    		}
    		return this.appSetComboBox;
    		
    	}
    
    
    //Implementing the action listener
    	private void appSetComboBox_actionPerformed(ActionEvent eventset) {		
    		String appSet = this.getAppSetComboBox().getSelectedItem().toString();		
    		//String appSet = "AMADEUS_ABG";
    		this.appSetComboBox.setSelectedItem(appSet);		
    		this.getAppComboBox().removeAllItems();
    		this.getPackageComboBox().removeAllItems();
    		this.appComboBox.addItem("All Application");
    		this.packageComboBox.addItem("All Package");
    		ArrayList<String> anAppList = CBRCPMDailyT20AppManager.instance()
    				.getAppList(appSet);//replace by appSet
    		for (int i = 0; i < anAppList.size(); i++) {
    			this.appComboBox.addItem(anAppList.get(i));
    		}
    		ArrayList<String> anPackageList = CBRCPMDailyT20AppManager.instance()
    		.getPackageList(appSet);//replace by appSet
            for (int l = 0; l < anPackageList.size(); l++) {
    	    this.packageComboBox.addItem(anPackageList.get(l));
           }
    		this.appComboBox.setSelectedIndex(0);
    		this.packageComboBox.setSelectedIndex(0);
    	    
    	}
    	
    	
    	//creating App Label
    	private JLabel getAppLabel() {
    		if (this.appLabel == null) {
    			this.appLabel = new JLabel("Application:");
    		}
    		return this.appLabel;
    	}
    	//bookingsCombo.removeActionListener(new BookingComboListener());
    	//Getting data for App Combo Box
    	public JComboBox getAppComboBox() {			
    		if (this.appComboBox == null) {
    			this.appComboBox = new JComboBox();
    			// Populate the App list
    			this.appComboBox.addItem("All Application");
    			ArrayList<String> anAppList = CBRCPMDailyT20AppManager.instance()
    					.getAppList();
    			for (int j = 0; j < anAppList .size(); j++) {
    				this.appComboBox.addItem(anAppList .get(j));
    			}	
    			 this.appSetComboBox.removeActionListener(appSetComboBox);
    	 		 this.appComboBox.addActionListener(new ActionListener() {
    				
    				public void actionPerformed(ActionEvent eventpack) {
    					appComboBox_actionPerformed(eventpack);
    				}
    			});
    		}
    		return this.appComboBox;
    	}
    	
    	//private JComboBox getAppComboBoxpac implements getAppComboBox;
    	//{
    	
    	
    	private void appComboBox_actionPerformed(ActionEvent eventpack) {		
    		//String appSet = "AMADEUS_CPM";
    		String app = this.getAppComboBox().getSelectedItem().toString();
    		this.appComboBox.setSelectedItem(app);		
    		this.getPackageComboBox().removeAllItems();
    		this.packageComboBox.addItem("All Package");
    		ArrayList<String> anPackageList = CBRCPMDailyT20AppManager.instance()
    				.getPackageListapp(app);//replace by app
    		for (int j = 0; j < anPackageList.size(); j++) {
    			this.packageComboBox.addItem(anPackageList.get(j));
    		}
    		this.packageComboBox.setSelectedIndex(0);
    	}
    	
    
    private JLabel getPackageLabel() {
    		if (this.packageLabel == null) {
    			this.packageLabel = new JLabel("Package:");
    		}
    		return this.packageLabel;
    	}
    
    	//Getting Data for package combo box
    	
    	private JComboBox getPackageComboBox() {		
    		if (this.packageComboBox == null) {
    			this.packageComboBox = new JComboBox();
    			// Populate the Package list
    			this.packageComboBox.addItem("All Package");
    			ArrayList<String> anPackageList = CBRCPMDailyT20AppManager.instance()
    					.getPackageList();
    			for (int i = 0; i < anPackageList.size(); i++) {
    				this.packageComboBox.addItem(anPackageList.get(i));
    			}
    		}
    		return this.packageComboBox;
    	}

    and I am getting the error as follows



    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at client.cpm.dailyT20.CBRCPMDailyT20View.appComboBox_actionPerformed(CBRCPMDailyT20View.java:243)
    at client.cpm.dailyT20.CBRCPMDailyT20View.access$1(CBRCPMDailyT20View.java:241)
    at client.cpm.dailyT20.CBRCPMDailyT20View$2.actionPerformed(CBRCPMDailyT20View.java:234)
    at javax.swing.JComboBox.fireActionEvent(Unknown Source)
    at javax.swing.JComboBox.contentsChanged(Unknown Source)
    at javax.swing.JComboBox.intervalRemoved(Unknown Source)
    at javax.swing.AbstractListModel.fireIntervalRemoved(Unknown Source)
    at javax.swing.DefaultComboBoxModel.removeAllElements(Unknown Source)
    at javax.swing.JComboBox.removeAllItems(Unknown Source)
    at client.cpm.dailyT20.CBRCPMDailyT20View.appSetComboBox_actionPerformed(CBRCPMDailyT20View.java:194)
    at client.cpm.dailyT20.CBRCPMDailyT20View.access$0(CBRCPMDailyT20View.java:190)
    at client.cpm.dailyT20.CBRCPMDailyT20View$1.actionPerformed(CBRCPMDailyT20View.java:181)
    at javax.swing.JComboBox.fireActionEvent(Unknown Source)
    at javax.swing.JComboBox.setSelectedItem(Unknown Source)
    at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    this error is because I am using the following line in the APPSET ActionListener in order to remove all the items in the App combobox and add the current selected items

    If I remove this line I am not getting error but my Items in the App combobox are repeating many times
    Code:
    this.getappComboBox.removeAllItems();
    Can you please check it and give me any reply

    Thanks in Advance

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

    Re: Need help in Populating Items for 3 jcomboboxes by action listeners

    The problem is not the line you have mentioned. The problem is occurring in the appComboBox_actionPerformed() method on line 243 (always read the exception stack trace, tells you where the problem is).

    Can you tell me which line line 243 is.

    Your code is confusing why are you sometimes calling methods to get the JComboBox and at other times using an instance field?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Sep 2010
    Posts
    4

    Re: Need help in Populating Items for 3 jcomboboxes by action listeners

    Hello Keang Sir,

    I am using the code for appComboBox_actionPerformed as follows

    Code:
    private void appComboBox_actionPerformed(ActionEvent eventpack) {		
    		//String appSet = "AMADEUS_CPM";
    		String app = this.getAppComboBox().getSelectedItem().toString();
    		this.appComboBox.setSelectedItem(app);		
    		this.getPackageComboBox().removeAllItems();
    		this.packageComboBox.addItem("All Package");
    		ArrayList<String> anPackageList = CBRCPMDailyT20AppManager.instance()
    				.getPackageListapp(app);//replace by app
    		for (int j = 0; j < anPackageList.size(); j++) {
    			this.packageComboBox.addItem(anPackageList.get(j));
    		}
    		this.packageComboBox.setSelectedIndex(0);
    	}

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

    Re: Need help in Populating Items for 3 jcomboboxes by action listeners

    Ok, so which of these lines is line No. 243?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Sep 2010
    Posts
    4

    Re: Need help in Populating Items for 3 jcomboboxes by action listeners

    Hello Keang Sir,

    Thank you very much,

    My problem is solved

    I used something as below and had got of my erros and I am getting the data for the 3 combo boxes,

    [code]

    Object selecteditem = getAppComboBox().getSelectedItem();
    if (selectedItem == null) return;
    String appSet = selectedItem.toString(); [\code]

    I used this in the appComboBox action performed ,

    So it had taken out the problem,


    Thank you very much,

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