使用分隔符从文本文件填充JComboBox

时间:2023-01-28 17:59:04

使用分隔符从文本文件填充JComboBox

I'm not looking for a complete answers just help on how to start it or maybe some references I could look at that may help me with this. Ok so I have to populate the JComboBox (accountnumber) from a text file. The txt files reads as:

我不是在寻找一个完整的答案,只是帮助你如何启动它,或者我可以看一些可以帮助我解决的参考资料。好的,我必须从文本文件中填充JComboBox(accountnumber)。 txt文件读作:

1231<>Jack Williams<>2015/1/21<>463.02
1232<>Jane Brown<>2015/1/21<>13510.54
1233<>Paul Gonzales<>2015/1/22<>680.17
1234<>Jian Chen<>2015/1/22<>1117.54
1235<>Lily Makki<>2015/1/22<>1124.89
1236<>Michael Lopez<>2015/1/23<>800.0
1237<>Jose Alvarez<>2015/1/23<>607.21
1238<>Tina Lin<>2015/1/24<>11077.0

It reads as acctNumber<>CustomerName<>openDate<>balance

它读作acctNumber <> CustomerName <> openDate <> balance

How would I go about starting this? Which would be easiest to split the 4 variables. array/arraylist/hashmap etc.?

我该如何开始呢?哪个分割4个变量最容易。数组/ arraylist / hashmap等?

I'm not familiar with file I/O. and trouble with collections so this is the only part I'm stuck on.

我不熟悉文件I / O.收藏的麻烦,所以这是我唯一坚持的部分。

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class AccountUtility {
List<String> accountsInfo = new ArrayList<>();
BufferedReader in;
File file  = new File("accounts.txt");
public AccountUtility(){
    ReadFile();
}
public void ReadFile(){
    String nxtLine = " ";

    try{
    in = new BufferedReader(new FileReader(file));

    while(nxtLine != null){
    nxtLine = in.readLine();
    accountsInfo.add(nxtLine);
    }
    for(String items : accountsInfo)
    System.out.println(items);
    in.close();
    }catch(IOException ex){
    }

}
public static void main(String[] args) {
    AccountUtility ut = new AccountUtility();

}

}

}

so i decided to use a list , this is just my accountutility class I just added a mainmethod so i can test just this class and the result when i Run it comes to

所以我决定使用一个列表,这只是我的accountutility类我刚添加了一个mainmethod所以我可以测试这个类和结果当我运行它来到

1231<>Jack Williams<>2015/1/21<>463.02
1232<>Jane Brown<>2015/1/21<>13510.54
1233<>Paul Gonzales<>2015/1/22<>680.17
1234<>Jian Chen<>2015/1/22<>1117.54
1235<>Lily Makki<>2015/1/22<>1124.89
1236<>Michael Lopez<>2015/1/23<>800.0
1237<>Jose Alvarez<>2015/1/23<>607.21
1238<>Tina Lin<>2015/1/24<>11077.0
null

1231 <> Jack Williams <> 2015/1/21 <> 463.02 1232 <> Jane Brown <> 2015/1/21 <> 13510.54 1233 <> Paul Gonzales <> 2015/1/22 <> 680.17 1234 <>陈剑<> 2015/1/22 <> 1117.54 1235 <> Lily Makki <> 2015/1/22 <> 1124.89 1236 <> Michael Lopez <> 2015/1/23 <> 800.0 1237 <> Jose Alvarez <> 2015/1 /23 <> 607.21 1238 <> Tina Lin <> 2015/1/24 <> 11077.0 null

How do i split an list using a delimiter?

如何使用分隔符拆分列表?

3 个解决方案

#1


1  

You're primary identifier is the account number, from this you need to be able to ascertain the account details.

您的主要标识符是帐号,您需要能够确定帐户详细信息。

This would lead me to use some kind of Map.

这会让我使用某种地图。

I would then create an Account class which held all the information in a simple, easy to use class, which provided appropriate setters and getters.

然后我会创建一个Account类,它将所有信息保存在一个简单易用的类中,该类提供了适当的setter和getter。

This would then lean me to the fact that I wouldn't actually need the Map, because all the information I need was in the Account class, so instead, I would simply create a ListCellRenderer for the combo box that would be capable of taking the account number from an instance of the Account class and display it appropriately...

这样我就不会理解我实际上不需要Map了,因为我需要的所有信息都在Account类中,所以相反,我只是为组合框创建一个ListCellRenderer,它能够获取来自Account类实例的帐号并正确显示...

This would mean I'd only need a List or a ComboBoxModel to hold the account details

这意味着我只需要一个List或一个ComboBoxModel来保存帐户详细信息

Take a closer look at How to Use Combo Boxes for more details

仔细查看如何使用组合框了解更多详细信息

#2


1  

To display an Object, Swing components will use the toString() method the Object placed in it.

要显示Object,Swing组件将使用放置在其中的Object的toString()方法。

One approach is to create a Data class that holds the name, ID, etc., implement toString() to display what you want (in your case, the Account Number), and then put a list of these objects in your JComboBox.

一种方法是创建一个包含名称,ID等的Data类,实现toString()以显示您想要的内容(在您的情况下为Account Number),然后在JComboBox中放置这些对象的列表。

Then on change of selection in the combo, get the selected item, cast it to the data class, and then call getDate(), getName(), etc. to populate the textfields.

然后在更改组合中的选择时,获取所选项目,将其强制转换为数据类,然后调用getDate(),getName()等来填充文本字段。

If you want to actually show the extra details of the Customer in the combo (after all, who really knows the person by account number?), then take a look at one approach here: DetailedComboBox

如果你想在组合中实际显示客户的额外细节(毕竟,谁真的通过账号知道这个人?),那么请看一下这里的一种方法:DetailedComboBox

#3


-1  

Justin, I'm in the same boat and created a similar program that reads from text file. There was an excellent given for cells (e.g. Excel or Sql), but need to have it read from a text file. I'm thinking:

贾斯汀,我在同一条船上创建了一个类似的程序,从文本文件中读取。对于单元格(例如Excel或Sql)有一个很好的给定,但需要从文本文件中读取它。我在想:

String tmp [] = line.split ("<>");

this will output data from each break.

这将从每个中断输出数据。

#1


1  

You're primary identifier is the account number, from this you need to be able to ascertain the account details.

您的主要标识符是帐号,您需要能够确定帐户详细信息。

This would lead me to use some kind of Map.

这会让我使用某种地图。

I would then create an Account class which held all the information in a simple, easy to use class, which provided appropriate setters and getters.

然后我会创建一个Account类,它将所有信息保存在一个简单易用的类中,该类提供了适当的setter和getter。

This would then lean me to the fact that I wouldn't actually need the Map, because all the information I need was in the Account class, so instead, I would simply create a ListCellRenderer for the combo box that would be capable of taking the account number from an instance of the Account class and display it appropriately...

这样我就不会理解我实际上不需要Map了,因为我需要的所有信息都在Account类中,所以相反,我只是为组合框创建一个ListCellRenderer,它能够获取来自Account类实例的帐号并正确显示...

This would mean I'd only need a List or a ComboBoxModel to hold the account details

这意味着我只需要一个List或一个ComboBoxModel来保存帐户详细信息

Take a closer look at How to Use Combo Boxes for more details

仔细查看如何使用组合框了解更多详细信息

#2


1  

To display an Object, Swing components will use the toString() method the Object placed in it.

要显示Object,Swing组件将使用放置在其中的Object的toString()方法。

One approach is to create a Data class that holds the name, ID, etc., implement toString() to display what you want (in your case, the Account Number), and then put a list of these objects in your JComboBox.

一种方法是创建一个包含名称,ID等的Data类,实现toString()以显示您想要的内容(在您的情况下为Account Number),然后在JComboBox中放置这些对象的列表。

Then on change of selection in the combo, get the selected item, cast it to the data class, and then call getDate(), getName(), etc. to populate the textfields.

然后在更改组合中的选择时,获取所选项目,将其强制转换为数据类,然后调用getDate(),getName()等来填充文本字段。

If you want to actually show the extra details of the Customer in the combo (after all, who really knows the person by account number?), then take a look at one approach here: DetailedComboBox

如果你想在组合中实际显示客户的额外细节(毕竟,谁真的通过账号知道这个人?),那么请看一下这里的一种方法:DetailedComboBox

#3


-1  

Justin, I'm in the same boat and created a similar program that reads from text file. There was an excellent given for cells (e.g. Excel or Sql), but need to have it read from a text file. I'm thinking:

贾斯汀,我在同一条船上创建了一个类似的程序,从文本文件中读取。对于单元格(例如Excel或Sql)有一个很好的给定,但需要从文本文件中读取它。我在想:

String tmp [] = line.split ("<>");

this will output data from each break.

这将从每个中断输出数据。