Results 1 to 8 of 8
  1. #1
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Populating combobox using text file

    Hello, everyone.

    I have a text file with a big database that looks like the following:

    ItemA_SubItem1A:ItemA:SubItem1A
    ItemA_SubItem2A:ItemA:SubItem2A
    ItemA_SubItem3A:ItemA:SubItem3A
    ItemB_SubItem1B:ItemB:SubItem1B
    ItemB_SubItem2B:ItemB:SubItem2B
    ItemB_SubItem3B:ItemB:SubItem3B
    ItemC_SubItem1C:ItemC:SubItem1C
    ItemC_SubItem2C:ItemC:SubItem2C

    I have a JPanel with a textbox where you type in Item. Then I have a combobox which should display subitems based on what is typed in the textbox.
    For instance, if I type "ItemA" in the text field, the combobox should have the following values in the dropdown menu: "SubItem1A", "SubItem2A","SubItem3A".

    I have created a code that scans through the text file and selects the necessary text:

    Java Code:
    // Typing in the textbox and setting the value to string 'item'.
    
    btnSetPorts.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
       item = txtItem.getText();
    
    ...
    
    
    // getting subItem based on the key, i.e. item:
    
    
    	public String getSubItem (String item) throws FileNotFoundException	{
    		
    		Scanner in = new Scanner(this.getClass().getResourceAsStream("/rwyDataset.txt"));
    		
    		HashMap<String, String> map = new HashMap<String, String>();
    		
    		while (in.hasNextLine())	{
    			String[] columns = in.nextLine().split(":");
    			map.put(columns[1], columns[2]);
    		}
    		
    		in.close();
    		
    		if(!map.containsKey(item))  {
    		    message.portNotFoundMessage();
    		}
    		
    		subItem= map.get(item);
    		
    		return subItem;
    		
    	}
    The code works but returns the first record in the database only, not the second and the third. Plus, I tried to use combobox.addItem() method and it does not work.

    I would appreciate if anyone can spend a few minutes to help.

    Thank you!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Populating combobox using text file

    Well, it appears you are trying to use the same key for different values. That won't work. A HashMap's key/value pair is a one-to-one mapping. Otherwise if it allowed the same key for multiple values, it wouldn't know which value to return for a given key. I would suggest you make the value of your HashMap an ArrayList of type String. Whenever you use that key then add the next value to the existing ArrayList. If this is the first value and it is null, then create the ArrayList and add the first value.

    Also, why not initialize your HashMap in its entirety first then your getSubItem(String key) could look like this.

    Java Code:
    public List<String> getSubItem(String key) {
        return map.get(key);  // assumes non-null map reference
    }
    Finally, it seems to me that you should be getting your last value of the DB not the first. Because for the same key of a HashMap, the previous value is replaced.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Populating combobox using text file -- Another issue

    Jim. Thank you so much. What you say makes perfect sense.
    I have redone my database, so now it looks like this:

    ItemA : SubItemA1, SubItemA2, SubItemA3, SubItemA4,
    ItemB : SubItemB1, SubItemB2, SubItemB3
    etc.

    Then I used the following code:

    Java Code:
    		JButton btnSetPorts = new JButton("Set Ports");
    		
    		btnSetPorts.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				icaoDep = txtDep.getText();                 // These are my text boxes
    				icaoDest = txtDest.getText();
    				icaoAlt = txtAlt.getText();
    					
    			try {
    				runway = airports.getRunway(icaoDep);             // getting a string with the combobox values
    			} catch (FileNotFoundException e) {
    				e.printStackTrace();
    			}
    	
    			String [] items = runway.split(",");
    
    			comboDep.setModel( new DefaultComboBoxModel((String[]) items));
    			
    			comboDep.addItemListener(new ItemListener() {
    				public void itemStateChanged(ItemEvent e) {
    					RunwayFinal = (String) e.getItem();
    				}
    			});
    				}
    			}
    		});
    It works well except for one problem. When I click on the button "Set Ports" it populates the combobox with the appropriate values. However, if I type another value in the textbox and hit the button again, the combobox does not change! Hitting the button should implement the code from the beginning and reset the combobox based on a new value but this is not happening.

    Thanks a lot for your help!
    Last edited by igorland; 04-16-2013 at 12:48 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Populating combobox using text file

    It is difficult to know what is going on without seeing all the code. However, you might try the following:

    1. Instead of creating a new DefaultComboModel, why not just manipulate the one you have. There are routines for deleting the entries and adding new ones.
    2. It should not be necessary to create a new ItemListener each time you press the button. Just provide the initial listener outside of the button actionListener.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: Populating combobox using text file

    Ahhhh! Found it. Forgot to put a bracket after an IF statement prior to that code. Now it works. Thanks, Jim.
    Actually if you can help me with one more thing? Is it possible to have e.getItem (in RunwayFinal = (String) e.getItem()) automatically pick up the first item in the combobox? I want to assign its value to a variable based on the selected item. It does not do it until I actually go to the combobox and select something. But I wanted to make it foolproof so that if the user leaves it untouched, the first item in combobox is assigned automatically. I can have the first item being "Select the item" but would prefer not to do it. Hope what I said makes sense. Again, thanks a lot!

  6. #6
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: Populating combobox using text file

    My code just in case:
    Java Code:
    		btnSetPorts.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				icaoDep = txtDep.getText();
    				icaoDest = txtDest.getText();
    				icaoAlt = txtAlt.getText();
    				if (("Alt ICAO").equals(icaoAlt))	{
    					icaoAlt = "NA";
    					txtAlt.setText("N/A");
    				}	
    				
    			System.out.println("Airports: " + icaoDep + "\t" + icaoDest + "\t" + icaoAlt);
    			
    			try {
    				runway = airports.getRunway(icaoDep);
    			} catch (FileNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    	
    	
    			
    			System.out.println("Runways: " + runway);
    			
    			items = runway.split(",");
    			
    			comDep.setModel( new DefaultComboBoxModel((String[]) items));
    			comDep.addItemListener(new ItemListener() {
    					public void itemStateChanged(ItemEvent e) {
    						RunwayFinal = (String) e.getItem();
    						System.out.println("CHosen: " + RunwayFinal);
    						
    					}
    				});
    				}
    			
    		});

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Populating combobox using text file

    The first item in the data model is the default selection even though it does not trigger an event. So if the user doesn't select something, just do
    RunwayFinal = comDep.getSelectedItem() when you need the information. It should return the default selection.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: Populating combobox using text file

    Worked. Awesome! Thank you so much, Jim. Can close the topic now. Cheers.

Similar Threads

  1. Populating a combobox from a text file.
    By Mike. in forum New To Java
    Replies: 1
    Last Post: 05-12-2012, 04:35 AM
  2. Populating databasefields in combobox
    By aborgeld in forum New To Java
    Replies: 27
    Last Post: 05-13-2011, 10:04 AM
  3. Populating a JList from a text file - Netbeans
    By Ben1 in forum AWT / Swing
    Replies: 1
    Last Post: 01-13-2011, 03:30 PM
  4. Replies: 0
    Last Post: 11-21-2010, 05:10 PM
  5. Replies: 3
    Last Post: 06-08-2010, 08:10 PM

Posting Permissions

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