Results 1 to 4 of 4
- 05-05-2010, 08:24 PM #1
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
String array from file to ComboBox
Hi.
I have a file: myFile.txt
In that file is in every line: name + '\t' + numberOfYears.
I want to get String array of names and put that names to be ComboBox items.
I'm thinking like that:
1. make class ReadFromFile
2. in that class write a function getNames which opens a file, read line by line
and get from each line name and put it in string array. This function return that string array.
3. make class MyPanel extends JPanel
4. in MyPanel create object ReadFromFile and call function getNames.
5. use return value from this function to populate items in ComboBox.
please tell me your thinking, or share some ideas with me.
Please give me advice, or tell me better way of thinking and planing, or tell me is something wrong with my planning.
- 05-05-2010, 09:11 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Your thinking is fine, go ahead.
kind regards,
Jos
- 05-05-2010, 09:17 PM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
With String[] it goes with null exception, so I change it to ArrayList.
I've written first part of code in a small program:
when I write the whole program will post it here.Java Code:import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.io.FileNotFoundException; import java.io.IOException; public class StudNames { public static void main(String[] args) { ReadFromFile namesOfStudents = new ReadFromFile(); ArrayList<String> students = namesOfStudents.getNames("students.txt"); System.out.println(students.toString()); } } class ReadFromFile { int indexOfNames = 0; ArrayList<String> names = new ArrayList<String>(); public ArrayList<String> getNames(String filename) { BufferedReader reader = null; try { int positionOfTab = 0; String lineOfString; reader = new BufferedReader(new FileReader(filename)); while((lineOfString = reader.readLine()) != null) { positionOfTab = lineOfString.indexOf('\t'); if(positionOfTab > 0) names.add(lineOfString.substring(0, positionOfTab)); else names.add(""); } } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } return names; } }
- 05-06-2010, 05:29 PM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
program that reads data from the file "students.txt" (format of students.txt is:
studentName + '\t' + age).
Then it takes all the studentNames and places as ComboBox item.
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 namesOfStudents = new ReadFromFile(); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { StudFrame frame = new StudFrame("Students names"); 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> listOfNames = rff.getNames("students.txt"); comboBox = new JComboBox(listOfNames.toArray()); add(comboBox); } } class ReadFromFile { int indexOfNames = 0; ArrayList<String> names = new ArrayList<String>(); public ArrayList<String> getNames(String filename) { BufferedReader reader = null; try { int positionOfTab = 0; String lineOfString; reader = new BufferedReader(new FileReader(filename)); while((lineOfString = reader.readLine()) != null) { positionOfTab = lineOfString.indexOf('\t'); if(positionOfTab > 0) names.add(lineOfString.substring(0, positionOfTab)); else names.add(""); } } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } return names; } }
Similar Threads
-
create a 2d char array from a 1D string array
By jschmall12 in forum New To JavaReplies: 1Last Post: 04-27-2010, 09:01 PM -
creating itemstate event handler of array of combobox at runtime.
By pophils in forum AWT / SwingReplies: 0Last Post: 03-22-2010, 06:45 AM -
Convert Char Array to String Array
By Mayur in forum New To JavaReplies: 8Last Post: 10-12-2009, 11:41 AM -
String array to byte array?!
By Joe2003 in forum Advanced JavaReplies: 5Last Post: 02-28-2009, 06:09 AM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks