
11-29-2009, 12:57 AM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 5
Rep Power: 0
|
|
Reading from file and adding to JTable
I need to read in contacts from a file (each contact has 4 lines each - name, phone number etc.)
I then have to display these contacts in a JTable.
I tried to read the details into an arrayList, but then realised that after every 4 lines i need {} to separate the contacts.
Would I be able to create substrings surrounded by {} and then put them in a table that way?
Sorry if this doesn't make much sense, I'm getting myself pretty confused.
|
|

11-29-2009, 01:32 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
|
If I were doing this, I would read in each row of data (name, phone number, etc) into a Vector<Object> and then place the newly created Vector into a Vector<Vector>. I'd then create a DefaultTableModel object using this Vector<Vector> plus a Vector<String> that holds my column names, and then set my JTable's model to this model. YMMV. Much luck!
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
|
|

11-29-2009, 01:42 AM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 5
Rep Power: 0
|
|
|
I understand what you mean, I'll have a go at doing that and see how it goes.
Just out of interest, would my idea work?
We've been learning about arrayLists in class, but we haven't thouched on vectors at all.
|
|

11-29-2009, 02:35 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
|
ArrayList could work if you used an AbstractTableModel, but that would involve a lot more work.
I have no idea what you meant though about the brackets: { and }.
|
|

11-29-2009, 03:29 AM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 5
Rep Power: 0
|
|
|
I've been playing around with the Vectors but am struggling now.
The layout of the text file is this:
smith, bob
0207393933
0774545435
1 london road, london
jones, mary
06546546
056506
123 elm road
etc
So each 'contact' has 4 lines in the text file.
In the example we used in class we were using objects, not vectors, and each 'contact' was surrounded by {} e.g. Object[][] addresses = {{"Hancock, Bob", "01202719029", "07676101393","1 Charminster Road, Bournemouth, Dorset, BH8 8UE"}};
I've managed to read the whole file in as one long vector. How would I go about breaking it up so that it will split it up into sections of 4 lines?
|
|

11-29-2009, 03:35 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
|
Those curly braces are java code for the boundaries of an array, and they are not String literals. You do not want to add them to your data. You could use an array of arrays, as you are doing in class, but this would require that you know in advance how many rows of data that your table will have. Most of the time this is not the case, and in these situations, Vectors would work much better.
For more help, I think that you'll have to post your code (using code tags).
Much luck.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
|
|

11-29-2009, 03:57 AM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 5
Rep Power: 0
|
|
Well here's what I've got so far:
|
Code:
|
class AddressBook2 extends JFrame
{
//String[][] addresses = {{"Hancock, Bob", "01202719029", "07676101393",
//"1 Charminster Road, Bournemouth, Dorset, BH8 8UE"}};
DefaultTableModel contactsTable;
JTable table;
Vector<Object> vectors = new Vector<Object>();
public AddressBook2() throws IOException
{
this.setSize(new Dimension(640, 480));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridBagLayout());
BufferedReader fileIn = new BufferedReader(new FileReader("AddressBook.buab"));
while (true)
{
String line = fileIn.readLine();
if (line == null)
break;
vectors.add(line);
//System.out.println(addresses2);
}
fileIn.close(); |
Here's the bit for the table:
|
Code:
|
this.contactsTable = new DefaultTableModel (vectors,new String[] {"Surname", "First Name", "Home Phone Number", "Mobile number", "Address"});
table = new JTable(this.contactsTable); |
|
|

11-29-2009, 04:34 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
You're just using one Vector here and that won't work. You actually need to have one Vector<String> that holds a row of data, say call it rowVector, and then a second Vector<Vector<String>>, say dataVector that holds all the rowVectors. A third Vector will hold the column names. For instance:
|
Code:
|
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
@SuppressWarnings("serial")
public class TableModelTest extends JPanel {
public static final String FILE_PATH = "src/yr2009/m11/d/";
public static final String FILE_NAME = "FuSwing2.txt";
public static final String[] COLUMNS = {"Column 1", "Column 2", "Column 3", "Column 4"};
private JTable table = new JTable();
private DefaultTableModel model;
public TableModelTest() {
setLayout(new BorderLayout());
add(new JScrollPane(table), BorderLayout.CENTER);
Vector<String> colNames = new Vector<String>(Arrays.asList(COLUMNS));
try {
Scanner scanner = new Scanner(new File(FILE_PATH + FILE_NAME));
int count = 0;
Vector<Vector<String>> dataVector = new Vector<Vector<String>>();
Vector<String> rowVector = new Vector<String>();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
rowVector.add(line);
count++;
count %= COLUMNS.length;
if (count == 0) {
dataVector.add(rowVector);
rowVector = new Vector<String>();
}
}
model = new DefaultTableModel(dataVector, colNames);
table.setModel(model);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private static void createAndShowUI() {
JFrame frame = new JFrame("FuSwing2");
frame.getContentPane().add(new TableModelTest());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
} |
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
|
|

11-29-2009, 07:49 PM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 5
Rep Power: 0
|
|
|
Thank you!
I wasn't sure how to get all the row vectors inside a bigger vector, if that makes sense??
I do have a couple of questions about your code though -
1. I understand that you set the count to 0 before reading in the lines, and it increases by one each time until it gets to 4 (the length of the columns string), but why do you have if (count == 0) , then it adds the rowvector to the datavector. Surely it should be when the count is equal to the column.length?
or am i missing something?
2. What does this symbol mean? %=
|
|

11-29-2009, 08:41 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
|
% is the mod operator and returns the remainder (if any) that is left after dividing by that number, and works like so:
1 % 5 is 1
2 % 5 is 2
3 % 5 is 3
4 % 5 is 4
5 % 5 is 0
6 % 5 is 1
7 % 5 is 2
8 % 5 is 3
9 % 5 is 4
10 % 5 is 0
%= is the mod equivalent of += operator. So
count %= 4
is the same as
count = count % 4
if count is 4 then
count %= 4 will change count to hold 0
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 05:28 PM.
|
|