13 Replies - 4805 Views - Last Post: 01 December 2010 - 08:08 PM Rate Topic: -----

#1 MufaT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-November 10

Creating a JTable which is reading from a file.

Posted 21 November 2010 - 02:54 PM

Hi guys,
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?

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a JTable which is reading from a file.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Creating a JTable which is reading from a file.

Posted 21 November 2010 - 05:20 PM

JTable is just a fancy GUI interface to display the data contain in it's model
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];
          }
       }


Was This Post Helpful? 2
  • +
  • -

#3 MufaT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-November 10

Re: Creating a JTable which is reading from a file.

Posted 22 November 2010 - 02:13 PM

ok thank you very much! I got it! :) I appreceate the help! :)
Was This Post Helpful? 0
  • +
  • -

#4 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 223
  • Joined: 24-September 08

Re: Creating a JTable which is reading from a file.

Posted 25 November 2010 - 03:08 AM

this was very helpful, it works for me too, but I do have a question, how do you use the header array as the column names for your table?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,123
  • Joined: 27-December 08

Re: Creating a JTable which is reading from a file.

Posted 25 November 2010 - 10:28 AM

When the TableModel is passed to the JTable constructor, the JTable functionality uses the header array and renders the column titles.
Was This Post Helpful? 0
  • +
  • -

#6 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 223
  • Joined: 24-September 08

Re: Creating a JTable which is reading from a file.

Posted 25 November 2010 - 10:31 AM

View Postmacosxnerd101, on 25 November 2010 - 05:28 PM, said:

When the TableModel is passed to the JTable constructor, the JTable functionality uses the header array and renders the column titles.



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'.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Creating a JTable which is reading from a file.

Posted 25 November 2010 - 08:06 PM

View PostBocard, on 25 November 2010 - 11:31 AM, said:

View Postmacosxnerd101, on 25 November 2010 - 05:28 PM, said:

When the TableModel is passed to the JTable constructor, the JTable functionality uses the header array and renders the column titles.



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

Was This Post Helpful? 2
  • +
  • -

#8 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 223
  • Joined: 24-September 08

Re: Creating a JTable which is reading from a file.

Posted 26 November 2010 - 08:17 AM

Thx for the answer, now I called the gettableHeader method and I get a header, but not the string array i have in my model. I get A B C as the columns instead of the strings i have in my model:
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);


Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Creating a JTable which is reading from a file.

Posted 26 November 2010 - 03:46 PM

View PostBocard, on 26 November 2010 - 09:17 AM, said:

Thx for the answer, now I called the gettableHeader method and I get a header, but not the string array i have in my model. I get A B C as the columns instead of the strings i have in my model:
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);


Was This Post Helpful? 0
  • +
  • -

#10 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 223
  • Joined: 24-September 08

Re: Creating a JTable which is reading from a file.

Posted 26 November 2010 - 05:26 PM

yeah..you're right :P. I modified the code now, and i added the JScrollPane but i still don't get the header. I get the A B C columns again.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Creating a JTable which is reading from a file.

Posted 26 November 2010 - 08:01 PM

View PostBocard, on 26 November 2010 - 06:26 PM, said:

yeah..you're right :P. I modified the code now, and i added the JScrollPane but i still don't get the header. I get the A B C columns again.

Show us the code of your new/renamed Model class
Was This Post Helpful? 0
  • +
  • -

#12 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 223
  • Joined: 24-September 08

Re: Creating a JTable which is reading from a file.

Posted 27 November 2010 - 02:50 AM

this is the code for that model:

   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.
Was This Post Helpful? 0
  • +
  • -

#13 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 223
  • Joined: 24-September 08

Re: Creating a JTable which is reading from a file.

Posted 01 December 2010 - 08:04 AM

sorry for double post, but I still didn't figure out how to show the header I want. Any more ideas?
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Creating a JTable which is reading from a file.

Posted 01 December 2010 - 08:08 PM

View PostBocard, on 01 December 2010 - 09:04 AM, said:

sorry for double post, but I still didn't figure out how to show the header I want. Any more ideas?

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1