Suppose you have folowing persons.txt file with a list of persons:
0 John
1 Mary
2 Jenny
3 Michael
4 Jane
For the code below special thanks for prosper!!! to read this in 2 comboboxes.
My question now:
you selected one item in combobox1 and one item in combobox2. (Above selected item in combobox2 can be not the same as combobox1!)
How to put this combination in a array?? such as
0 Michael Jane
1 John Jenny
...
How to display this on the same screen (java-swing) as our comboboxes itselves?
ID person person =>titles on screen display combinations
1 Michael Jane
2 John Jenny
...
The ID with number 1 represents our first combination, ....
existing code in Java (with special thanks for Prosper)
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;
JComboBox jc2;
public ReadFile2(String tempFile)
{
super("JComboBox Demo");
getContentPane().setLayout(new FlowLayout());
jc = new JComboBox();
jc2 = new JComboBox();
getContentPane().add(jc);
getContentPane().add(jc2);
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);
jc2.addItem(peopleName);
}
}
}
public static void main(String args[])
{
ReadFile2 rfObj = new ReadFile2("persons.txt");
rfObj.setSize(400,400);
rfObj.setVisible(true);
}
}
A lot of thanks ....
0 John
1 Mary
2 Jenny
3 Michael
4 Jane
For the code below special thanks for prosper!!! to read this in 2 comboboxes.
My question now:
you selected one item in combobox1 and one item in combobox2. (Above selected item in combobox2 can be not the same as combobox1!)
How to put this combination in a array?? such as
0 Michael Jane
1 John Jenny
...
How to display this on the same screen (java-swing) as our comboboxes itselves?
ID person person =>titles on screen display combinations
1 Michael Jane
2 John Jenny
...
The ID with number 1 represents our first combination, ....
existing code in Java (with special thanks for Prosper)
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;
JComboBox jc2;
public ReadFile2(String tempFile)
{
super("JComboBox Demo");
getContentPane().setLayout(new FlowLayout());
jc = new JComboBox();
jc2 = new JComboBox();
getContentPane().add(jc);
getContentPane().add(jc2);
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);
jc2.addItem(peopleName);
}
}
}
public static void main(String args[])
{
ReadFile2 rfObj = new ReadFile2("persons.txt");
rfObj.setSize(400,400);
rfObj.setVisible(true);
}
}
A lot of thanks ....