Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to put selected items in comboboxes in an array? 1

Status
Not open for further replies.

javaken

Technical User
May 15, 2004
20
BE
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 ....
 
let's try this

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class Demo implements ActionListener
{
  private JComboBox nameList1;
  private JComboBox nameList2;
  private JTextArea textArea;
  private int index = 0;
  private String fileName = "persons.txt";

  public void addComponent(Container pane)
  {
    JPanel pane1 = new JPanel();
    pane1.setLayout(new BoxLayout(pane1, BoxLayout.X_AXIS));

    nameList1 = new JComboBox();
    pane1.add(nameList1);
    nameList2 = new JComboBox();
    pane1.add(nameList2);
    loadFile(fileName);

    JPanel pane2 = new JPanel();

    pane2.setLayout(new BoxLayout(pane2, BoxLayout.Y_AXIS));
    JButton button = new JButton("action");
    button.addActionListener(this);
    button.setAlignmentX(Component.CENTER_ALIGNMENT);
    pane2.add(button);

    textArea = new JTextArea(5, 20);
    textArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea, 
                             JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                             JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setAlignmentX(Component.CENTER_ALIGNMENT);
    pane2.add(scrollPane, BorderLayout.CENTER);

    pane.add(pane1, BorderLayout.PAGE_START);
    pane.add(pane2, BorderLayout.CENTER);
  }

  public void actionPerformed(ActionEvent e)
  {
    String name1 = (String)nameList1.getSelectedItem();
    String name2 = (String)nameList2.getSelectedItem();
    textArea.append(index+"  "+name1+"  "+name2+"\n");
    index++;
  }

  private void loadFile(String fn)
  {
    int i = 0;
    String[] line = new String[100];
    String[] tempString = new String[100];
    File f = new File(fn);

    try
    {
      FileReader fr = new FileReader(fn);
      BufferedReader br = new BufferedReader(fr);
      
      while ((line[i] = br.readLine()) != null)
        i++;

      br.close();
      fr.close();
    }
    catch (Exception e)
    {
      System.out.println("Error");
    }

    for (int j=0; j<i; j++)
    {
      if (line[j]!=null)
      {
        int pos = line[j].indexOf(" ");
        if (pos!=-1)
          tempString[j]=line[j].substring(pos+1);
        nameList1.addItem(tempString[j]);
        nameList2.addItem(tempString[j]);
      }
    }
  }

  private static void create()
  {
    JFrame.setDefaultLookAndFeelDecorated(false);

    JFrame frame = new JFrame("Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Demo example = new Demo();
    example.addComponent(frame.getContentPane());

    frame.pack();
    frame.setVisible(true);

    }

  public static void main(String[] args)
  {
    javax.swing.SwingUtilities.invokeLater(new Runnable()
                                           {
                                             public void run()
                                             {
                                               create();
                                             }
                                           });
  }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top