简体   繁体   English

从文本文件Java填充JComboBox

[英]Populate JComboBox from Text file java

This is my code for populating JcomboBox. 这是我用于填充JcomboBox的代码。 I try to select a text file from JFileChooser, then read and put into an array list and put it in the combo box. 我尝试从JFileChooser中选择一个文本文件,然后读取并放入数组列表中并将其放在组合框中。 But my problem is, even i put it in the array list and show it(system.out.println(list)), but still can't populate to the combobox. 但是我的问题是,即使我将其放在数组列表中并显示(system.out.println(list)),但仍然无法填充到组合框。 What can i do for that? 我该怎么办?

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

}

} }

You're creating a new JComboBox with the file results, but the UI still contains the old one. 您正在使用文件结果创建一个新的JComboBox ,但是UI仍然包含旧的JComboBox Just remove the old one and add the new one. 只需删除旧的并添加新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM