从java文本文件填充JComboBox
这是我填充JcomboBox的代码。 我尝试从JFileChooser中选择一个文本文件,然后读取并放入一个数组列表,然后将其放入组合框中。但我的问题是,即使我将其放入数组列表并显示(system.out.println(list)),但仍然无法填充到组合框中。我能为此做些什么从java文本文件填充JComboBox,java,text,arraylist,jcombobox,populate,Java,Text,Arraylist,Jcombobox,Populate,这是我填充JcomboBox的代码。 我尝试从JFileChooser中选择一个文本文件,然后读取并放入一个数组列表,然后将其放入组合框中。但我的问题是,即使我将其放入数组列表并显示(system.out.println(list)),但仍然无法填充到组合框中。我能为此做些什么 public class Read2 extends JFrame implements ActionListener{ private static final int FRAME_WIDTH = 500; priv
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
}
公共类Read2扩展JFrame实现ActionListener{
专用静态最终整数帧_宽度=500;
专用静态最终整型框架高度=500;
私有静态最终整数帧×原点=150;
私有静态最终整数帧_Y_原点=250;
私人JComboBox cb;
私人JButton b;
私人JPanel J;
int len=0;
室内=1;
内线=1;
int北=0;
int东=0;
int-west=0;
int-south=0;
int up=0;
int-down=0;
//字符串s=“”;
字符串s2=“”;
新房;
HashMap=newHashMap();
公众阅读2(){
容器内容窗格;
//设置框架属性
setTitle(“创建自己的地图”);
设置尺寸(框宽、框高);
可设置大小(假);
设置位置(帧X_原点、帧Y_原点);
contentPane=getContentPane();
setLayout(新的BorderLayout());
J=新的JPanel();
J.setLayout(新的BorderLayout());
cb=新的JComboBox();
b=新的按钮(“插入”);
b、 addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件){
//TODO自动生成的方法存根
//String fileName=“;//请求导入
JFileChooser fc=新的JFileChooser();
int r=fc.showOpenDialog(null);
if(r==JFileChooser.APPROVE\u选项){
文件名=fc.getSelectedFile();
//File directory=fc.getCurrentDirectory();
FileInputStream inputfile=null;
试一试{
inputfile=新文件InputStream(文件名);
FileReader in=newfilereader(filename.getAbsoluteFile());
BufferedReader br=新的BufferedReader(in);
字符串s1=“”;
字符串s=“”;
而((s1=br.readLine())!=null){
交换机(第%8行){
案例1:
s=s1;
打破
案例2:
s2=s1;
打破
案例3:
north=Integer.parseInt(s1);
打破
案例4:
east=Integer.parseInt(s1);
打破
案例5:
south=Integer.parseInt(s1);
打破
案例6:
west=整数.parseInt(s1);
打破
案例7:
up=Integer.parseInt(s1);
打破
案例0:
down=Integer.parseInt(s1);
新房间=新房间(s、s2、北、东、南、西、上、下);
地图。放置(房间、新房);
房间++;
打破
}
line++;
//系统输出打印LN(s1);
}
列表=新的ArrayList();
//系统输出打印项次(列表);
如果(list!=null){
//列表=新的ArrayList();
//列表=新的ArrayList();
对于(int i=1;i您正在使用文件结果创建一个新的JComboBox
,但是UI仍然包含旧的。只需删除旧的并添加新的即可。我可以问一下如何删除旧的吗当我有了新的内容时,实际上,替换内容可能最简单。您可以调用removeAllItems()
将其清除,然后在循环中调用addItem(Object)
将所有新项添加到其中。奇怪的是,JComboBox
没有addItems(List)
或addItems(Object[])
方法。