Creating a JTable which is reading from a file.Problem with reading from file
Page 1 of 1
13 Replies - 3271 Views - Last Post: 01 December 2010 - 08:08 PM
#1
Creating a JTable which is reading from a file.
Posted 21 November 2010 - 02:54 PM
I am really new on Java Programming and i would appreceate your help if you don't mind.
I have an assignment wich is really complicated and I have stucked in this point. Well I am gonna give a really simple example of the help I need cause my english aren't very good and I'm gonna lose you on trying to follow of what the assignment asks and what help I need.
Well let's say I have a JTable (wich I have created and dessigned) with 3 columns and 5 rows. Let's say that in the first column we want to see a customer's name, in the second column the customer's Last name, and in the third column the telephone number of this customer.
This is what I have done!
My problem is that I want to add these data from a txt file named data.txt to the respective column. In my txt file the data are of form:
Laura
Smith
26569
James
Taylor
36594
Sonia
Brown
65946
Could you please give me a hint on how I will read these kind of info respectevly on the right column and row?
Replies To: Creating a JTable which is reading from a file.
#2
Re: Creating a JTable which is reading from a file.
Posted 21 November 2010 - 05:20 PM
So build your JTable from a model that take care of the data handling
MyModel model = new MyModel();
JTable table = new JTable(model);
.....
class MyModel extends AbstractTableModel {
String[] header = {"Col1", "Col2", "Col3"};
String[][] arrayToStoreData;
// contructor
MyModel() {
// read your data from file store it into an arrayToStoreData
}
public int getColumnCount() {
return 3;
}
public int getRowCount() {
return 5;
}
public String getColumnName(int col) {
return header[col];
}
public Object getValueAt(int row, int col) {
return arrayToStoreData[row][col];
}
}
#3
Re: Creating a JTable which is reading from a file.
Posted 22 November 2010 - 02:13 PM
#4
Re: Creating a JTable which is reading from a file.
Posted 25 November 2010 - 03:08 AM
#5
Re: Creating a JTable which is reading from a file.
Posted 25 November 2010 - 10:28 AM
#6
Re: Creating a JTable which is reading from a file.
Posted 25 November 2010 - 10:31 AM
macosxnerd101, on 25 November 2010 - 05:28 PM, said:
it doesn't do that for me....i only get the data, no header.
i thought there is a method or something to make it use the 'header'.
#7
Re: Creating a JTable which is reading from a file.
Posted 25 November 2010 - 08:06 PM
Bocard, on 25 November 2010 - 11:31 AM, said:
macosxnerd101, on 25 November 2010 - 05:28 PM, said:
it doesn't do that for me....i only get the data, no header.
i thought there is a method or something to make it use the 'header'.
Is your JTable in a JScrollPane. It has to be there for the header to appear automatically.
From JTable API
Note that if you wish to use a JTable in a standalone view (outside of a JScrollPane) and want the header displayed, you can get it using getTableHeader() and display it separately.
Actually macosxnerd101 statement is not 100% accurate. The headers are not passed to the JTable at creation time. The JTable will query the model calling getColumnName() method to get the header. These can be changed on the fly anytime.
This post has been edited by pbl: 25 November 2010 - 08:09 PM
#8
Re: Creating a JTable which is reading from a file.
Posted 26 November 2010 - 08:17 AM
static String[] header= {"N", "B", "M"};
this is how i create the model (the name of the class is Table) and how i add them to the frame.
Table myModel = new Table(); table = new JTable(myModel); frame.setLayout(new BorderLayout()); frame.add(table.getTableHeader(), BorderLayout.NORTH); frame.add(table, BorderLayout.CENTER);
#9
Re: Creating a JTable which is reading from a file.
Posted 26 November 2010 - 03:46 PM
Bocard, on 26 November 2010 - 09:17 AM, said:
static String[] header= {"N", "B", "M"};
this is how i create the model (the name of the class is Table) and how i add them to the frame.
Table myModel = new Table(); table = new JTable(myModel); frame.setLayout(new BorderLayout()); frame.add(table.getTableHeader(), BorderLayout.NORTH); frame.add(table, BorderLayout.CENTER);
The model class is Table
The JTable variable name is table
you are loking for trouble.. this will become very confusing
Rename your Table class Model
And put the JTable in a Scroll pane, one day you might need it and if not the scroll bar won't appears anyway
Model myModel = new Model(); JTable table = new JTable(model); frame.setLayout(new BorderLayout()); frame.add(new JScrollPane(table), BorderLayout.CENTER);
#10
Re: Creating a JTable which is reading from a file.
Posted 26 November 2010 - 05:26 PM
#11
Re: Creating a JTable which is reading from a file.
Posted 26 November 2010 - 08:01 PM
#12
Re: Creating a JTable which is reading from a file.
Posted 27 November 2010 - 02:50 AM
import javax.swing.table.*;
public class Model extends AbstractTableModel
{
static String[] header= {"N", "B", "M"};
private String[][] arrayToStoreData = new String[DolphinMain.members.size()][4];
public Model()
{
int i = 0;
for (Swimmer s : DolphinMain.members)
{
arrayToStoreData[i][0] = s.getName();
arrayToStoreData[i][1] = s.getBDate()+"-"+s.getBMonth()+"-"+s.getBYear();
arrayToStoreData[i][2] = Integer.toString(s.getMembership_type());
arrayToStoreData[i][3] = Double.toString(s.getFee());
i++;
}
}
public int getColumnCount()
{
return 4;
}
public int getRowCount()
{
return DolphinMain.members.size();
}
public Object getValueAt(int row, int col )
{
return arrayToStoreData[row][col];
}
}
thx for helping me pbl.
#13
Re: Creating a JTable which is reading from a file.
Posted 01 December 2010 - 08:04 AM
#14
Re: Creating a JTable which is reading from a file.
Posted 01 December 2010 - 08:08 PM
Bocard, on 01 December 2010 - 09:04 AM, said:
Sorry for late reply... I missed it
In your Model class you have this method missing
public String getColumnName(int col) {
return header[col];
}
this method not being there the father method is called which returns (Excel style):
A for col 0, B for col 1, C.... , Z for col 25.
|
|

New Topic/Question
Reply




MultiQuote







|