简体   繁体   中英

populating JComboBox from text file with delimiters

在此处输入图片说明

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:

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

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

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

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

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

How do i split an list using a delimiter?

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.

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...

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

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

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

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 .

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.

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

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

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

this will output data from each break.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM