Java 从文本文件填充JCombobox

Java 从文本文件填充JCombobox,java,swing,netbeans,file-io,jcombobox,Java,Swing,Netbeans,File Io,Jcombobox,我已经看到一个老问题,答案是下面这样的代码,但是如果我使用netbeans,我已经设计了我的comboBox。所以我认为(正如您所想象的,我对Java和netbeans非常陌生!)应该更改代码的最后一行,我应该在哪里插入这段代码 BufferedReader input = new BufferedReader(new FileReader(filePath)); List<String> strings = new ArrayList<String>(); try {

我已经看到一个老问题,答案是下面这样的代码,但是如果我使用netbeans,我已经设计了我的comboBox。所以我认为(正如您所想象的,我对Java和netbeans非常陌生!)应该更改代码的最后一行,我应该在哪里插入这段代码

BufferedReader input = new BufferedReader(new FileReader(filePath));
List<String> strings = new ArrayList<String>();
try {
    String line = null;
    while (( line = input.readLine()) != null){
        strings.add(line);
    }
}

catch (FileNotFoundException e) {
    System.err.println("Error, file " + filePath + " didn't exist.");
}
finally {
    input.close();
}

String[] lineArray = strings.toArray(new String[]{});

JComboBox comboBox = new JComboBox(lineArray); 
BufferedReader input=new BufferedReader(new FileReader(filePath));
列表字符串=新的ArrayList();
试一试{
字符串行=null;
而((line=input.readLine())!=null){
字符串。添加(行);
}
}
catch(filenotfounde异常){
System.err.println(“错误,文件“+filePath+”不存在。”);
}
最后{
input.close();
}
String[]lineArray=strings.toArray(新字符串[]{});
JComboBox组合框=新的JComboBox(线性阵列);

1.这些代码行是无用的

List<String> strings = new ArrayList<String>();
String[] lineArray = strings.toArray(new String[]{});
JComboBox comboBox = new JComboBox(lineArray); 
List strings=new ArrayList();
String[]lineArray=strings.toArray(新字符串[]{});
JComboBox组合框=新的JComboBox(线性阵列);
2.直接向列表中添加新项目,也可以对项目进行排序


3.可能有,使用

1.这些代码行是无用的

List<String> strings = new ArrayList<String>();
String[] lineArray = strings.toArray(new String[]{});
JComboBox comboBox = new JComboBox(lineArray); 
List strings=new ArrayList();
String[]lineArray=strings.toArray(新字符串[]{});
JComboBox组合框=新的JComboBox(线性阵列);
2.直接向列表中添加新项目,也可以对项目进行排序


3.可能有,使用

您可以通过调用其
setModel
方法来更改现有JComboBox的项

无论如何,您可能会发现该方法更易于使用:

try {
    final List<String> lines = Files.readAllLines(Paths.get(filePath),
        Charset.defaultCharset());

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            comboBox.setModel(
                new DefaultComboBoxModel<String>(
                    lines.toArray(new String[0])));
        }
    });
} catch (IOException e) {
    e.printStackTrace();
}
试试看{
最终列表行=Files.readAllLines(path.get(filePath),
defaultCharset());
invokeLater(新的Runnable(){
公开募捐{
comboBox.setModel(
新的DefaultComboxModel(
行。toArray(新字符串[0]);
}
});
}捕获(IOE异常){
e、 printStackTrace();
}

您可以通过调用现有JComboBox的
setModel
方法来更改其项

无论如何,您可能会发现该方法更易于使用:

try {
    final List<String> lines = Files.readAllLines(Paths.get(filePath),
        Charset.defaultCharset());

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            comboBox.setModel(
                new DefaultComboBoxModel<String>(
                    lines.toArray(new String[0])));
        }
    });
} catch (IOException e) {
    e.printStackTrace();
}
试试看{
最终列表行=Files.readAllLines(path.get(filePath),
defaultCharset());
invokeLater(新的Runnable(){
公开募捐{
comboBox.setModel(
新的DefaultComboxModel(
行。toArray(新字符串[0]);
}
});
}捕获(IOE异常){
e、 printStackTrace();
}

除非此输入是从用户处获得的,否则它很可能会成为必须通过URL访问的。除非此输入是从用户处获得的,否则它很可能会成为必须通过URL访问的。