//assume there is one space between the number and the person 's name
//
import java.io.*;
import javax.swing.*;
import java.awt.*;
class ReadFile2 extends JFrame
{
final int MAXCHOICE = 100;
String myLine[], peopleName[];
int myInt[];
int count=0;
JComboBox jc;
public ReadFile2(String tempFile)
{
super("JComboBox Demo");
getContentPane().setLayout(new FlowLayout());
jc = new JComboBox();
getContentPane().add(jc);
int pos;
String tempStr;
myLine = new String[MAXCHOICE];
peopleName = new String[MAXCHOICE];
myInt = new int[MAXCHOICE];
File f = new File(tempFile);
try
{
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
myLine[count] = br.readLine();
while(count<=MAXCHOICE-1)
{
count++;
myLine[count] = br.readLine();
if (myLine[count]==null)
{
count--;
break;
}
}
br.close();
fr.close();
}
catch (Exception e)
{
}
for (int i=0;i<count;i++)
{
if (myLine!=null)
{
System.out.println(myLine);
pos = myLine.indexOf(" ");// search for position of space in each line
if (pos!=-1)
{
myInt = Integer.parseInt(myLine.substring(0,pos));
tempStr = "";
for (int x=pos; x<myLine.length(); x++)
{
tempStr+=myLine.charAt(x);
}
peopleName = tempStr;
}
jc.addItem(peopleName);
}
}
}
public static void main(String args[])
{
ReadFile2 rfObj = new ReadFile2("persons.txt");
rfObj.setSize(400,400);
rfObj.setVisible(true);
}
}
public static void main(String args[])
{
ReadFile2 rfObj = new ReadFile2("persons.txt");
rfObj.setSize(400,400);
rfObj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rfObj.setVisible(true);
}
// this will make the JFrame that can be close with a mouse click
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.