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 data in multidimensional arrays?

Status
Not open for further replies.

javaken

Technical User
May 15, 2004
20
BE
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();
}
});
}
}
 
a) it is possible to sort, but not on column 3, since there are only 3 columns, (which is only seen, when you look at your previous posts) and we start counting from 0.
Sorting is an issue which doesn't fit well to arrays.

b) Removing elements isn't well to arrays too.

c) Your question isn't very clear to me. Obviously you may make people do your work for you here, but you have to be more specific in the requirements.

seeking a job as java-programmer in Berlin:
 
If you need an array to shrink or grow, use an ArrayList or Vector class. Arrays are static in size, so not really suited for your purpose.

I'm not sure what you mean by sorting on column 3 - what column ?
 
a) Right. I meant column 2 .... visual column 3

b) c)

The purpose is to put selected combinations in an arraylist (or other collection), when a new combination is made (see button add)

We want to use the arraylist:

to remove (new button (to do))
After removing one combination, the purpose is updating (refreshing) our Textarea (see source above), updating our arraylist (to do)

to sort on
column 1 (new button (to do))
column 2 (new button (to do))
(column 1 exist of all selected persons combobox1,
column 2 exist of all selected persons combobox2)


 
Put your Objects in a ArrayList.
Create a Class which implements Comparator.
Create a method, to change the behaviour of your Comparator (define the column, which is the key for comparison).

Call Collections.sort (yourArrayList, yourComparator);

This isn't trivial, but very useful to understand.
You may define multiple keys (when the problem fits), to compare for multiple columns, i.e.: compare by City, if equal compare by Street, if equal ...

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top