This is my code for populating 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. 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. What can i do for that?
这是我填充JcomboBox的代码。我尝试从JFileChooser中选择一个文本文件,然后读取并放入一个数组列表并将其放入组合框中。但我的问题是,即使我把它放在数组列表中并显示它(system.out.println(list)),但仍然无法填充到组合框。我能做些什么?
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
}
}
}
1 个解决方案
#1
0
You're creating a new JComboBox
with the file results, but the UI still contains the old one. Just remove the old one and add the new one.
您正在使用文件结果创建新的JComboBox,但UI仍包含旧的。只需删除旧的并添加新的。
#1
0
You're creating a new JComboBox
with the file results, but the UI still contains the old one. Just remove the old one and add the new one.
您正在使用文件结果创建新的JComboBox,但UI仍包含旧的。只需删除旧的并添加新的。