2013-05-20 173 views
0

我想從字符串數組填充我的java awt列表。 字符串數組獲取.csv文件中的值。 (時區) e.g 澳大利亞,墨爾本 澳大利亞,悉尼 歐洲,倫敦從字符串數組填充java.awt.list

注:有兩個列表框。第一個爲地區,第二個爲城市。 這個想法是在用戶選擇第一個區域後進行第二個更新。

我似乎無法能夠從這些值填充列表

任何幫助將不勝感激 感謝

String fileName = "C:\\Users\\Seb\\IdeaProjects\\TimeMachine\\src\\regions.csv"; 
    File file = new File(fileName); 
    try { 
     Scanner inputStream = new Scanner(file); 
     inputStream.useDelimiter(System.getProperty("line.separator")); //Stops the white spaces creating a new entry in array 
     while (inputStream.hasNext()) { 
      String data = inputStream.next(); //gets the whole line 
      String[] arrayLocations = data.split(","); 
      System.out.println(arrayLocations[0]); 
      System.out.println(arrayLocations[1]); 

      listRegion = Arrays.asList(arrayLocations); 
     } 
     inputStream.close(); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 

出現在listRegion = Arrays.asList(arrayLocations);

整個代碼

錯誤
import java.applet.Applet; 
import java.awt.*; 
import java.awt.List; 
import java.io.*; 
import java.util.Arrays; 
import java.util.Scanner; 

public class TimeMachine extends Applet 
{ 
private List listRegion = new List(); 
private List listSubRegion = new List(); 

public void init() 
{ 
    setLayout(new BorderLayout()); 

    Panel buttons = new Panel(new BorderLayout()); 
    buttons.setBackground(Color.cyan); 
    buttons.add(listRegion, BorderLayout.WEST); 
    buttons.add(listSubRegion, BorderLayout.CENTER); 
    add(buttons, BorderLayout.NORTH); 
} 

public void getLocationInfo() 
{ 
    String fileName = "C:\\Users\\Seb\\IdeaProjects\\TimeMachine\\src\\regions.csv"; 
    File file = new File(fileName); 
    try { 
     Scanner inputStream = new Scanner(file); 
     inputStream.useDelimiter(System.getProperty("line.separator")); //Stops the white spaces creating a new entry in array 
     while (inputStream.hasNext()) { 
      String data = inputStream.next(); //gets the whole line 
      String[] arrayLocations = data.split(","); 
      System.out.println(arrayLocations[0]); 
      System.out.println(arrayLocations[1]); 

      listRegion = Arrays.asList(arrayLocations); 
     } 
     inputStream.close(); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
} 

}

+0

使用'java.util.List'會不會更容易? – Keppil

+1

什麼是'listRegion'?你會得到什麼錯誤? – BobTheBuilder

+0

你可以在GUI窗口中顯示java.util.List嗎? –

回答

1

你混合兩個java.util.Listjava.awt.List,只使用java.awt.List

listRegion = new List(); 
while (inputStream.hasNext()) { 
      String data = inputStream.next(); //gets the whole line 
      String[] arrayLocations = data.split(","); 
      System.out.println(arrayLocations[0]); 
      System.out.println(arrayLocations[1]); 
      for(String location : arrayLocations) 
       listRegion.add(location); 
     } 

更正式地說,下面是一個工作代碼,其中region.csv必須包含地區,國家格式,其中重複的處理方式:

import java.applet.Applet; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.List; 
import java.awt.Panel; 
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Arrays; 
import java.util.HashMap; 
import java.util.HashSet; 
import java.util.Map; 
import java.util.Scanner; 
import java.util.Set; 

public class TimeMachine extends Applet implements ItemListener{ 
    private static final long serialVersionUID = 1L; 

    private List listRegion = new List(); 
    private List listSubRegion = new List(); 
    private Map<String, Set<String>> regionCountryMap = new HashMap<String, Set<String>>(); 

    public void init() 
    { 
     setLayout(new BorderLayout()); 

     getLocationInfo(); 
     Panel buttons = new Panel(new BorderLayout()); 
     buttons.setBackground(Color.cyan); 
     buttons.add(listRegion, BorderLayout.WEST); 
     buttons.add(listSubRegion, BorderLayout.CENTER); 
     add(buttons, BorderLayout.NORTH); 
    } 

    private void getLocationInfo() { 
     String fileName = "e:\\regions.csv"; 
     File file = new File(fileName); 
     try { 
      Scanner inputStream = new Scanner(file); 
      inputStream.useDelimiter(System.getProperty("line.separator")); 
      listRegion = new List(); 
      while (inputStream.hasNext()) 
      { 
       String data = inputStream.next(); // gets the whole line 
       String[] arrayLocations = data.split(","); 
       if(arrayLocations.length != 2){ 
        System.out.println("Invalid entry in file : "+ Arrays.toString(arrayLocations)); 
        continue; 
       } 
       String region = arrayLocations[0]; 
       if(!regionCountryMap.containsKey(region)) 
       { 
        listRegion.add(region); 
        regionCountryMap.put(region, new HashSet<String>()); 
       } 
       regionCountryMap.get(region).add(arrayLocations[1]);//Set handles duplicates as well 
      } 
      inputStream.close(); 
      System.out.println("Region, Country list has been parsed successfully"); 
      listRegion.addItemListener(this); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void itemStateChanged(ItemEvent e) { 
     Set<String> countries = regionCountryMap.get(listRegion.getSelectedItem()); 
     listSubRegion.removeAll(); 
     for(String country : countries) { 
      listSubRegion.add(country); 
     } 
     listSubRegion.validate(); 
     listSubRegion.repaint(); 
    } 
} 
+0

非常感謝。這工作完美。反正只有在listRegion中只顯示[0]數組的一部分? –

+0

嘗試使用'listRegion.select(index)' – harsh

+0

也許我措辭我的問題錯了,但沒有奏效。我試過'list.region.add(location,1)'但仍然在第一個列表框中顯示[1]數組的部分。我只希望這些地區出現在他們的地區。感謝您的幫助,但其他想法 –