Results 1 to 4 of 4
  1. #1
    the reporter is offline Member
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default Retrieving data from text file and putting it into comboboxes

    Hi everyone im stuck with the part on trying to retrieve data from the textfile and then putting it into the combo boxes. I've uploaded my textfile as well. Can i know what's wrong with my code? In my text file there is
    Apple,AA
    Yahoo,YHOO

    What i want to do is i wan to only show the words Apple and then Yahoo in my combo boxes any idea how to do that? Here's my code

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    
    import java.util.ArrayList;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    import javax.swing.JFrame;
    import javax.swing.JComboBox;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class StudNames {
    	public static void main(String[] args) {
    		ReadFromFile namesOfStocks= new ReadFromFile();
    		
    		SwingUtilities.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				StockFrame frame = new StockFrame("Stocks List");
    				frame.showGUI();
    			}
    		});
    	}
    
    }
    
    class StudFrame extends JFrame {
    	PanelWithComboBox panel = new PanelWithComboBox();
    	
    	public StudFrame(String name) {
    		setTitle(name);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(300,300);
    		add(panel);
    	}
    	
    	public void showGUI() {
    		setVisible(true);
    	}
    }
    
    class PanelWithComboBox extends JPanel {
    	JComboBox comboBox;
    	
    	ReadFromFile rff = new ReadFromFile();
    	
    	
    	public PanelWithComboBox() {
    		ArrayList<String> listOfStock = rff.getStock("/C:/Java Sem 2/FYP SEM 1/stocklist.txt");
    		comboBox = new JComboBox(listOfStock.toArray());
    		add(comboBox);
    	}
    }
    
    class ReadFromFile {
    	int indexOfNames = 0;
    	ArrayList<String> stockNames = new ArrayList<String>();
    	
    	public ArrayList<String> getStock(String filename) {
    		BufferedReader reader = null;
    		try {
    			int positionOfTab = 0;
    			String lineOfString;
    			reader = new BufferedReader(new FileReader("/C:/Java Sem 2/FYP SEM 1/stocklist.txt"));
    			while((lineOfString = reader.readLine()) != null) {
    				positionOfTab = lineOfString.indexOf('\t');
    				
    				if(positionOfTab > 0)
    					stockName.add(lineOfString.substring(0, positionOfTab));
    				else
    					stockName.add("");
    			}
    		} catch(FileNotFoundException e) {
    			e.printStackTrace();
    		} catch(IOException e) {
    			e.printStackTrace();
    		}
    		
    		return stockName;
    	}
    }
    Attached Files Attached Files
    Last edited by the reporter; 06-02-2010 at 05:19 PM.

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    I would suggest creating a class which represents what you are reading in. Then create a method which reads the File and returns a List of objects of the class you just created.

    Then when you create the ComboBox, just obtain the List and add all the objects to it.

  3. #3
    the reporter is offline Member
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default

    do u mind showing that in example of a code? able to edit my code?

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    I'll provide the class to represent the data, but I'm not entirely sure what it is being used for so I'll just name it Stock.

    Stock Class:
    Java Code:
    public class Stock {
    	
    	private String name;
    	
    	private String tickerSymbol;
    	
    	public Stock(String name, String tickerSymbol) {
    		super();
    		this.name = name;
    		this.tickerSymbol = tickerSymbol;
    	}
    	
    	public String getName() {
    		return name;
    	}
    	
    	public String getTickerSymbol() {
    		return tickerSymbol;
    	}
    
    }
    Now all you have to do is create a method that would read the file and return a List<Stock>. Then use this to populate the ComboBox when you create it.

    Is the problem you are having is obtaining the File to read from? Also I notice you are looking for Tab's when the data is comma delimited, not tab delimited. The String's split() method is helpful.

Similar Threads

  1. Reading data from a text file
    By Cheguvara in forum New To Java
    Replies: 2
    Last Post: 02-02-2010, 03:33 PM
  2. Store data is not retrieving
    By JavaJunkie in forum CLDC and MIDP
    Replies: 2
    Last Post: 05-30-2009, 09:08 AM
  3. reading data from text file .. help plz
    By Thug heart in forum New To Java
    Replies: 7
    Last Post: 02-15-2009, 08:29 PM
  4. error while retrieving data from data base
    By kirtesh4u in forum New To Java
    Replies: 5
    Last Post: 11-15-2008, 05:10 PM
  5. Retrieving data from the DB
    By yuchuang in forum JDBC
    Replies: 2
    Last Post: 11-27-2007, 09:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •