Existing code below
Other questions:
+ Is it possible to sort this, for example on column 3?
+ What to do if you want remove one row in such an array?
+ What to do to refresh your list (see textArea in source code below...)?
file: persons.txt = a list of persons:
0 John
1 Mary
2 Jenny
3 Michael
4 Jane
the existing code is ....
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 = 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();
}
});
}
}
Other questions:
+ Is it possible to sort this, for example on column 3?
+ What to do if you want remove one row in such an array?
+ What to do to refresh your list (see textArea in source code below...)?
file: persons.txt = a list of persons:
0 John
1 Mary
2 Jenny
3 Michael
4 Jane
the existing code is ....
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 = 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();
}
});
}
}