如何从外部文件向 JComboBox 添加项目?

发布于 2024-10-20 14:03:22 字数 2024 浏览 4 评论 0原文

请我需要一些帮助来从外部文件向 JComboBox in Java 添加项目。

这是我到目前为止的代码:

 //Loading the Names:

        File Names_File = new File("Data" + File.separator + "Names.txt");
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;

        String str_Data = ""; //For storing the input from the file.

        try
        {
            fis = new FileInputStream(Names_File);

            // Here BufferedInputStream is added for fast reading.
            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);


            str_Data = dis.readLine(); //Reading the line from the file.

            StringTokenizer st = new StringTokenizer(str_Data); //Tokenizing the line.

            //The below line adds only one item. The objective is adding all the items.

            //*** Requesting help here ***

            cmb_Name.addItem(st.nextToken("|"));

            //*** Requesting help here ***

            // Disposing and closing all the resources after using them.
            fis.close();
            bis.close();
            dis.close();
        }

        catch (FileNotFoundException e)
        {
            System.err.println("Error: File not found!");
            JOptionPane.showMessageDialog(null, "Error: File not found!", "Error Message",
                                          JOptionPane.ERROR_MESSAGE);
        }

        catch (IOException e)
        {
            System.err.println("Error: Unable to read from file!");
            JOptionPane.showMessageDialog(null, "Error: Unable to read from file!", "Error Message",
                                          JOptionPane.ERROR_MESSAGE);
        }

主要是我的外部文件的格式如下:

James|Robert|Alice

名称之间用 "|"< 分隔/strong> 符号。

我的上面的代码在此示例中仅添加一个元素,即(“James”)。我需要将所有三个添加到循环或其他内容中。我的想法是,我不确定我的外部文件中有多少个名字。因此,使用简单的计数器没有帮助。

任何建议将不胜感激!

预先感谢您的帮助

Please i need some help in adding items to a JComboBox in Java from an external file.

Here is my code so far:

 //Loading the Names:

        File Names_File = new File("Data" + File.separator + "Names.txt");
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;

        String str_Data = ""; //For storing the input from the file.

        try
        {
            fis = new FileInputStream(Names_File);

            // Here BufferedInputStream is added for fast reading.
            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);


            str_Data = dis.readLine(); //Reading the line from the file.

            StringTokenizer st = new StringTokenizer(str_Data); //Tokenizing the line.

            //The below line adds only one item. The objective is adding all the items.

            //*** Requesting help here ***

            cmb_Name.addItem(st.nextToken("|"));

            //*** Requesting help here ***

            // Disposing and closing all the resources after using them.
            fis.close();
            bis.close();
            dis.close();
        }

        catch (FileNotFoundException e)
        {
            System.err.println("Error: File not found!");
            JOptionPane.showMessageDialog(null, "Error: File not found!", "Error Message",
                                          JOptionPane.ERROR_MESSAGE);
        }

        catch (IOException e)
        {
            System.err.println("Error: Unable to read from file!");
            JOptionPane.showMessageDialog(null, "Error: Unable to read from file!", "Error Message",
                                          JOptionPane.ERROR_MESSAGE);
        }

Mainly the format of my external file is as follows:

James|Robert|Alice

The names are separated by the "|" symbols.

My above code is adding only one element namely ("James") in this example. I need to add all three maybe in a loop or something. The idea is that i don't know for sure how many names will be in my external file. Thus using simple counters will not help.

Any suggestions are greatly appreciated!

Thanks in advance for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小糖芽 2024-10-27 14:03:22

试试这个:

   StringTokenizer st = new StringTokenizer(str_Data); //Tokenizing the line.

   while(st.hasMoreTokens()) {
        cmb_Name.addItem(st.nextToken("|"));
   }

Try this:

   StringTokenizer st = new StringTokenizer(str_Data); //Tokenizing the line.

   while(st.hasMoreTokens()) {
        cmb_Name.addItem(st.nextToken("|"));
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文