Results 1 to 3 of 3
  1. #1
    pinto_g is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Unhappy Dynamic List for Country and City

    I want to create two drop down list for Country and City. So if the user selects a country from dropdown list the other drop down list should automatically be refreshed to show its corresponding cities.

    I tried using JCOMBOBOX but there is no method in this class there i can pass the data at run time.

    Can any one pls send me a sample code?

    Thanks

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    if you have swing knowledge this shoudn't be a problem. pass the reference of the JComboBox to the construcor of the mouse listener and the if a country is selected make a call to JComboBox.removeAllItems() and then fill the JComboBox with the related cities. i have an example but i don't know if this implementation is state of the art. let me know!

    save this code in file CountryDemo.java
    Java Code:
    import java.awt.*;
    
    import javax.swing.*;
    import java.io.*;
    
    public class CountryDemo extends JFrame {
    	
    	private static final long serialVersionUID = 1L;
    	JButton exit;
    	JComboBox countries, cities;
    	Panel center, south, north, east;
    	JLabel label;
    
    	public CountryDemo(String title) {
    		super(title);
    		exit = new JButton("Exit");
    		center = new Panel();
    		south = new Panel();
    		north = new Panel();
    		east = new Panel();
    		south.add(exit, BorderLayout.CENTER);
    		countries = new JComboBox();
    		try {
    			File file = new File(
    					"/pathToYourCounties/countries.txt");
    			BufferedReader in = new BufferedReader(new FileReader(file));
    			String str;
    
    			while ((str = in.readLine()) != null) {
    				countries.addItem(str);
    			}
    		} catch (IOException e) {
    			System.out.println("A exception occured " + e);
    			return;
    		}
    		
    
    		center.add(countries);
    		
    		cities = new JComboBox();
    		cities.addItem("        ");
    		east.add(cities);
    		label = new JLabel("Wählen sie ein Land!");
    		north.add(label);
    		
    		Container contentPane = getContentPane();
    		contentPane.add(center, BorderLayout.CENTER);
    		contentPane.add(south, BorderLayout.SOUTH);
    		contentPane.add(north, BorderLayout.NORTH);
    		contentPane.add(east, BorderLayout.EAST);
    		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    		myListener listener = new myListener(countries, cities);
    		countries.addActionListener(listener);
    		exit.addActionListener(listener);
    	}
    
    	public static void main(String[] args) {
    		JFrame f = new CountryDemo("Stadt Auswahl");
    		f.setSize(300, 200);
    		f.setVisible(true);
    	}
    
    }
    make sure the file countries.txt is filled with some country-names.

    save the next code in MyListener.java
    Java Code:
    import java.awt.List;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class MyListener implements ActionListener {
    
    	JComboBox countries, cities;
    
    	public MyListener(JComboBox countries, JComboBox cities) {
    		this.countries = countries;
    		this.cities = cities;
    
    	}
    
    	public void actionPerformed(ActionEvent e) {
    
    		String button = e.getActionCommand();
    		String country = "";
    
    		if (countries.getSelectedIndex() == -1) {
    			System.out.println("nothing selected");
    			return;
    		}
    
    		else if (button.equals("Exit")) {
    			System.out.println("Land: "
    					+ countries.getSelectedItem().toString() + "\t Stadt: "
    					+ cities.getSelectedItem().toString());
    			System.exit(0);
    		}
    
    		country = countries.getSelectedItem().toString();
    
    		try {
    			File file = new File("pathToYourCities/"
    					+ country.toString() + ".txt");
    			int count = 0;
    			BufferedReader in = new BufferedReader(new FileReader(file));
    			String str;
    			cities.removeAllItems();
    			while ((str = in.readLine()) != null) {
    				cities.addItem(str);
    			}
    		} catch (IOException ex) {
    			System.out.println("A exception occured " + ex);
    			return;
    		}
    
    	}
    }
    make sure, the country-files with the cities exist. So is the country Agypt exists in the country list, the code will look for a cities file Agypt.txt. so, if you add countries and cities then you have to edit txt-files instead of changing your java code. but if the file is not found, you will get an exeception instead of a list cities. you will find my files as attachment.

    i must admit, the jframe looks scrubby, but my intention is to show how the reference is passed. good luck.

    perhaps some java prof know an easier solution. post it.
    Attached Files Attached Files
    Last edited by j2me64; 03-10-2010 at 02:29 PM.

Similar Threads

  1. Replies: 2
    Last Post: 11-22-2009, 05:24 PM
  2. Replies: 3
    Last Post: 11-21-2009, 05:48 PM
  3. IPAddress to Country
    By sandeeprao.techno in forum Advanced Java
    Replies: 2
    Last Post: 12-19-2008, 01:39 AM
  4. How to access ArrayList in List of List?
    By alvations in forum New To Java
    Replies: 5
    Last Post: 10-08-2008, 12:23 PM
  5. Replies: 2
    Last Post: 08-27-2008, 02:40 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
  •