Example JList
Posted in Java, on January 17, 2014 at 17:39
// main method
DefaultListModel listModel;
listModel = new DefaultListModel();

storeAcc = new StoredAccounts();
List<String> accounts = new ArrayList<String>();
accounts = storeAcc.getAccounts(thisUserID);

for (int i = 0; i < accounts.size(); i++) {
listModel.addElement(accounts.get(i));
}
list_1 = new JList(listModel);
scrollPane.setViewportView(list_1);	



// accounts class
class StoredAccounts{
	public static List<String> getAccounts(String user) throws IOException{
		FileInputStream fis = new FileInputStream("userAccounts.txt");
		BufferedReader br = new BufferedReader(new InputStreamReader(fis));
	 
		String line = null;
		List<String> myList = new ArrayList<String>();
		
		while ((line = br.readLine()) != null) {
			String[] accountLine = line.split("\t",-1); 
			if(user.equals(accountLine[1])){
				myList.add(accountLine[2]);
			}
		}
	 
		br.close();

		return myList;
	}
}

Share this code

| More

Use the link below to share the code:
HTML
BBCode