2013-12-12 43 views
0

這是我用於填充的JComboBox代碼。 我嘗試選擇的JFileChooser一個文本文件,然後閱讀並放入數組列表,並把它在組合框中。但我的問題是,即使我把它放在數組列表,並顯示它(的System.out.println(名單)),但仍不能填充到ComboBox。我能爲此做些什麼?填充的JComboBox從文本文件的Java

public class Read2 extends JFrame implements ActionListener{ 
private static final int FRAME_WIDTH = 500; 
private static final int FRAME_HEIGHT = 500; 
private static final int FRAME_X_ORIGIN = 150; 
private static final int FRAME_Y_ORIGIN = 250; 


private JComboBox cb; 
private JButton b; 
private JPanel J; 
int len = 0; 
int room = 1; 
int line = 1; 
int north = 0; 
int east = 0; 
int west = 0; 
int south = 0; 
int up = 0; 
int down = 0; 
//String s=""; 
String s2=""; 
Room newRoom; 



HashMap<Integer,Room> Map = new HashMap<Integer,Room>(); 


public Read2(){ 
Container contentPane; 

    //set the frame properties 
    setTitle ("Creat your own map"); 
    setSize (FRAME_WIDTH, FRAME_HEIGHT); 
    setResizable(false); 
    setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN); 
    contentPane = getContentPane(); 
    contentPane.setLayout(new BorderLayout()); 

    J= new JPanel(); 
    J.setLayout(new BorderLayout()); 

    cb = new JComboBox(); 
    b = new JButton ("insert"); 

    b.addActionListener(new ActionListener(){ 

     @Override 
     public void actionPerformed(ActionEvent event) { 
      // TODO Auto-generated method stub 
      //String fileName="";//ask hillary for import 
      JFileChooser fc = new JFileChooser(); 
      int r = fc.showOpenDialog(null); 
       if (r == JFileChooser.APPROVE_OPTION) { 
        File filename = fc.getSelectedFile(); 
        //File directory = fc.getCurrentDirectory(); 
         FileInputStream inputfile = null; 
         try { 
          inputfile = new FileInputStream(filename); 
          FileReader in = new FileReader(filename.getAbsoluteFile()); 
          BufferedReader br=new BufferedReader(in); 
          String s1 =""; 
          String s= ""; 
          while ((s1=br.readLine())!=null){ 
           switch(line % 8){ 
           case 1: 
            s = s1;    
            break; 
           case 2: 
            s2 = s1; 
            break; 
           case 3: 
            north = Integer.parseInt(s1); 
            break; 
           case 4: 
            east = Integer.parseInt(s1); 
            break; 
           case 5: 
            south = Integer.parseInt(s1); 
            break; 
           case 6: 
            west = Integer.parseInt(s1); 
            break; 
           case 7: 
            up = Integer.parseInt(s1); 
            break; 
           case 0: 
            down = Integer.parseInt(s1); 

            newRoom = new Room(s,s2, north, east, south, west, up, down); 
            Map.put(room, newRoom); 
            room++; 
            break; 
            } 
           line++; 
          //System.out.println(s1); 

           } 

          List<String> list = new ArrayList<String>(); 
          //System.out.println(list); 

          if(list !=null){  
           //list = new ArrayList<String>(); 

           //list = new ArrayList<String>(); 
          for(int i = 1; i <=Map.size(); i++){ 
            String d = Map.get(i).getImg(); 

            list.add(d); 
            } 

            cb= new JComboBox(list.toArray()); 

          //find out the problem for this and it will be solved  

          System.out.println(list); 



          inputfile.close(); 

          } 
         }catch(IOException e){} 
         System.out.println("Could not find file"); 

       } 



     }}); 
    J.add(b, BorderLayout.NORTH); 
    J.add(cb,BorderLayout.CENTER); 

    contentPane.add(J,BorderLayout.CENTER); 
    setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
} 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    Read2 r = new Read2(); 
    r.setVisible(true); 
} 

@Override 
public void actionPerformed(ActionEvent arg0) { 
    // TODO Auto-generated method stub 

} 

}

回答

0

你正在創建一個新JComboBox與文件的結果,但仍然UI包含舊的。只要刪除舊的,並添加新的。

+0

請問是怎麼刪除舊的一個時,我有新的 – user3097076

+0

事實上,它很可能是最容易更換的內容。你可以調用removeAllItems()來清除它,然後在循環中調用addItem(Object)來添加所有新的。奇怪的是,'JComboBox'沒有'addItems(List)'或'addItems(Object [])'方法。 – MattPutnam