473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

how to display data in a table from an external file

aquagirl20
I'm new in using tables. I have a series of getter methods and I wants to display them in a table. I've been trying to use the external file that my program saves but I'm having a hard time how to display it in the table. Then my classmate told me that it's easier to just use methods in our CarRecordManager class. The external file contains several data types... (int, String, String, double, int) to be specific... In that order as well How can I do this? Can somebody please help me?

Here is the RecordManager class:
Expand|Select|Wrap|Line Numbers
  1. package util;
  2.  
  3. import java.util.*;
  4. import java.io.*;
  5.  
  6. public class CarRecordManager{
  7. private RandomAccessFile raf;
  8. private int NUMBER_OF_SPACES = 0;
  9. private StringBuilder SPACE;
  10. private Product[] productList;
  11.  
  12. public CarRecordManager(String fileName)throws IOException{
  13.     raf = new RandomAccessFile(fileName, "rw");
  14.     countSpace();
  15. }
  16. /*
  17.     add operation
  18. */
  19. public void addNewCar(Product p)throws IOException{
  20.     //error traps
  21.     if(!inRange(p.getProductID())) return;
  22.     if(productExists(p.getProductName())){
  23.         System.out.println(p.getProductName()+" already exists. Operation cancelled.");
  24.         return;
  25.     }
  26.  
  27.     //main operation
  28.     int index;
  29.     if(hasSpace()){
  30.         index = getIndexOfSpace();
  31.         p.appendID(index);
  32.     }else{
  33.         index = p.getProductID();
  34.     }
  35.     raf.seek(index*Product.RECORD_SIZE);
  36.     p.writeData(raf);
  37.     refresh();
  38.     raf.close();
  39. }
  40. /*
  41.     delete operation
  42. */
  43. public void deleteRecordAt(int index)throws IOException{
  44.     //error traps
  45.     if(!inRange(index)) return;
  46.     if(productList[index].getProductName().equals("?")) return;
  47.  
  48.     //main operation
  49.     raf.seek(index * Product.RECORD_SIZE);
  50.     Product tempProduct = new Product(index, "?", 0.0, 0);
  51.     tempProduct.writeData(raf);
  52.     refresh();
  53. }
  54.  
  55. /*
  56.     append operators
  57. */
  58. public void appendQuantityAt(int index, int quantity)throws IOException{
  59.     //error traps
  60.     if(!inRange(index)) return;
  61.     if(productList[index].getProductName().equals("?")) return;
  62.  
  63.     //main operation
  64.     raf.seek(index * Product.RECORD_SIZE);
  65.     Product tempProduct = productList[index];
  66.     tempProduct.appendQuantity(quantity);
  67.     tempProduct.writeData(raf);
  68.     refresh();
  69. }
  70. public void appendPriceAt(int index, double price)throws IOException{
  71.     //error traps
  72.     if(!inRange(index)) {
  73.             return;
  74.         }
  75.     if(productList[index].getProductName().equals("?")) {
  76.             return;
  77.         }
  78.  
  79.     //main operation
  80.     raf.seek(index * Product.RECORD_SIZE);
  81.     Product tempProduct = productList[index];
  82.     tempProduct.appendPrice(price);
  83.     tempProduct.writeData(raf);
  84.     refresh();
  85. }
  86. public void appendNameAt(int index,String name)throws IOException{
  87.     //error traps
  88.     if(!inRange(index)) return;
  89.     if(productList[index].getProductName().equals("?")) return;
  90.  
  91.     //main operation
  92.     raf.seek(index * Product.RECORD_SIZE);
  93.     Product tempProduct = productList[index];
  94.     tempProduct.appendName(name);
  95.     tempProduct.writeData(raf);
  96.     refresh();
  97. }
  98. public void appendProductAt(int index, Product product)throws IOException{
  99.     //error traps
  100.     if(!inRange(index)) return;
  101.     if(productList[index].getProductName().equals("?")) return;
  102.  
  103.     //main operation
  104.     raf.seek(index * Product.RECORD_SIZE);
  105.     product.appendID(index);
  106.     product.writeData(raf);
  107.     refresh();
  108. }
  109. /*
  110.     record accessor
  111. */
  112. public Product getRecordAt(int index){
  113.     return productList[index];
  114. }
  115. /*
  116.     property accessors
  117. */
  118. public int getProductIDAt(int index){
  119.     return productList[index].getProductID();
  120. }
  121. public String getProductNameAt(int index){
  122.     return productList[index].getProductName();
  123. }
  124. public double getProductPriceAt(int index){
  125.     return productList[index].getProductPrice();
  126. }
  127. public int getProductQuantityAt(int index){
  128.     return productList[index].getProductQuantity();
  129. }
  130. /*
  131.     index accessors
  132. */
  133. public int indexOf(String name){
  134.     int index = -1;
  135.     for (Product e : productList){
  136.         if(e.getProductName().equalsIgnoreCase(name)){
  137.             index = e.getProductID();
  138.         }
  139.     }
  140.     return index;
  141. }
  142. public int indexOf(Product p){
  143.     int index = -1;
  144.     for (Product e : productList){
  145.         if(e.getProductName().equalsIgnoreCase(p.getProductName())){
  146.             index = e.getProductID();
  147.         }
  148.     }
  149.     return index;
  150. }
  151. public int getLastIndex()throws IOException{
  152.     return (int)(raf.length() / Product.RECORD_SIZE);
  153. }
  154.  
  155. /*
  156.     boolean checkers
  157. */
  158. public boolean inRange(int index)throws IOException{
  159.     if(index>getLastIndex()) return false;
  160.     else return true;
  161. }
  162. public boolean hasSpace(){
  163.     if(NUMBER_OF_SPACES>0) return true;
  164.     else return false;
  165. }
  166. public boolean dataExistsAt(int index){
  167.     if(index >= productList.length){
  168.         System.out.println("Index out of bound!");
  169.         return false;
  170.     }
  171.     if(productList[index].getProductName().equals("?")){
  172.         System.out.println("Index exists but is considered a Space! ");
  173.         return false;
  174.     }else return true;
  175. }
  176. public boolean productExists(String name){
  177.     boolean found=false;
  178.     for (Product e : productList){
  179.         if(e.getProductName().equalsIgnoreCase(name)){
  180.             found=true;
  181.         }
  182.     }
  183.     return found;
  184. }
  185.  
  186. /*
  187.     console helps
  188. */
  189. public void print(){
  190.     for (Product e : productList){
  191.         if(!e.getProductName().equals("?")){
  192.             System.out.println(e);
  193.         }
  194.     }
  195. }
  196. public void printRecordAt(int index){
  197.     System.out.println(productList[index]);
  198. }
  199. public void printByProductID(){
  200.     sortByProductID();
  201.     print();
  202. }
  203. public void printByProductName(){
  204.     sortByProductName();
  205.     print();
  206. }
  207. public void printByProductPrice(){
  208.     sortByProductPrice();
  209.     print();
  210. }
  211. public void printByProductQuantity(){
  212.     sortByProductQuantity();
  213.     print();
  214. }
  215.  
  216. /*
  217.     sorting operations
  218. */
  219. public void sortByProductID(){
  220.     for(int i=0;i<productList.length;i++){
  221.         for(int j=i+1;j<productList.length;j++){
  222.             if(productList[i].getProductID() > productList[j].getProductID()){
  223.                 Product temp = productList[i];
  224.                 productList[i] = productList[j];
  225.                 productList[j] = temp;
  226.             }
  227.         }
  228.     }
  229. }
  230. public void sortByProductName(){
  231.                         Arrays.sort(productList);
  232.     //operation erased
  233. }
  234. public void sortByProductPrice(){
  235.     for(int i=0;i<productList.length;i++){
  236.         for(int j=i+1;j<productList.length;j++){
  237.             if(productList[i].getProductPrice() > productList[j].getProductPrice()){
  238.                 Product temp = productList[i];
  239.                 productList[i] = productList[j];
  240.                 productList[j] = temp;
  241.             }
  242.         }
  243.     }
  244. }
  245. public void sortByProductQuantity(){
  246.     for(int i=0;i<productList.length;i++){
  247.         for(int j=i+1;j<productList.length;j++){
  248.             if(productList[i].getProductQuantity() > productList[j].getProductQuantity()){
  249.                 Product temp = productList[i];
  250.                 productList[i] = productList[j];
  251.                 productList[j] = temp;
  252.             }
  253.         }
  254.     }
  255. }
  256.  
  257. /*
  258.     private methods
  259. */
  260. private void countSpace()throws IOException{
  261.     productList = new Product[getLastIndex()];
  262.     for (int i = getLastIndex() - 1; i >= 0; i--){
  263.         productList[i] = new Product();
  264.         raf.seek(i * Product.RECORD_SIZE);
  265.         productList[i].readData(raf);
  266.     }
  267.     SPACE = new StringBuilder();
  268.     for (Product e : productList){
  269.         if(e.getProductName().equals("?")){
  270.             NUMBER_OF_SPACES++;
  271.             SPACE.append(e.getProductID()+",");
  272.         }
  273.     }
  274. }
  275. private int getIndexOfSpace()throws IOException{
  276.     int index = 0, ctrl = 0;
  277.     String temp = "";
  278.     String space = SPACE.toString();
  279.     while(space.charAt(ctrl) != ','){
  280.         temp += Character.toString(space.charAt(ctrl));
  281.         ctrl++;
  282.     }
  283.     index = Integer.parseInt(temp);
  284.     while((ctrl)>=0){
  285.         SPACE.deleteCharAt(ctrl);
  286.         ctrl--;
  287.     }
  288.     refresh();
  289.     return index;
  290.  
  291. }
  292. private void reset()throws IOException{
  293.     sortByProductID();
  294.     refresh();
  295. }
  296. private void refresh()throws IOException{
  297.     countSpace();
  298. }
  299. }
I wanted to display the data with a click of a button but I don't know how to. Can somebody please help me.

I tried to use the getters from the CarRecordManager but I wasn't successful.

Here's the part that I tried to code for the displaying of data:

Expand|Select|Wrap|Line Numbers
  1. public void readFile() throws Exception{
  2. for(int i = 0; i<inventoryTable.getRowCount(); i++){
  3.  inventoryTable.(cars.getProductIDAt(i), new Object[]{cars.getProductIDAt(i), cars.getProductNameAt(i), cars.getProductPriceAt(i),
  4.                        cars.getProductQuantityAt(i)});}}
And this is also that part on how I add items to my external files.

Expand|Select|Wrap|Line Numbers
  1. public void add() throws Exception{
  2.  
  3.             String brand = brandTxt.getText();
  4.             String model = modelTxt.getText();
  5.             String displacement = engineTxt.getText();
  6.             double price = Double.parseDouble(priceTxt.getText());
  7.             int quantity = Integer.parseInt(quantityTxt.getText());
  8.  
  9.             switch(brand){
  10.                 case "Honda":
  11.                     cars = new CarRecordManager("hondaInventory.txt");
  12.                     cars.addNewCar(new Product(cars.getLastIndex(), model, displacement, price, quantity));
  13.                     JOptionPane.showMessageDialog(null, "You have successfully added a car under " + brand+".");
  14.                     break;
  15.  
  16.                 case "Ford":
  17.                     cars = new CarRecordManager("fordInventory.txt");
  18.                     cars.addNewCar(new Product(cars.getLastIndex(), model, displacement, price, quantity));
  19.                     JOptionPane.showMessageDialog(null, "You have successfully added a car under " + brand+".");
  20.                     break;
  21.  
  22.                 case "Toyota":
  23.  
  24.                    cars = new CarRecordManager("toyotaInventory.txt");
  25.                   cars.addNewCar(new Product(cars.getLastIndex(), model, displacement, price, quantity));
  26.                    JOptionPane.showMessageDialog(null, "You have successfully added a car under " + brand+".");
  27.                    break; 
  28.             }
  29.      }
I have attached a screenshot of my program on how it looks like.
Attached Images
File Type: jpg screenshot.jpg (59.5 KB, 357 views)
Aug 19 '12 #1
2 2407
rajujrk
107 100+
Hi,

See, if you want to load the data into JTable, you have to use DefaultTableModel. I have given below sample how to load the data into the JTable. You can place the code where you want

Expand|Select|Wrap|Line Numbers
  1. // Initialize JTable and DefaultTableModel
  2. JTable jtbTable =new javax.swing.JTable();
  3.     DefaultTableModel dtmRecordDetails = null;
  4.     dtmRecordDetails = new DefaultTableModel(
  5.                 new Object[][]{},
  6.                 new String[]{
  7.                     "PRODUCT",
  8.                     "PRICE",
  9.                     "QUANTITY",
  10.                 }) {
  11.  
  12.             boolean[] canEdit = new boolean[]{
  13.                 false,false,false
  14.             };
  15. // Set the Model in JTable
  16.        jtbTable.setModel(dtmRecordDetails);
  17.  
  18. .
  19. .
  20. .
  21. .
  22.  
  23. // Add the Records in each row
  24.         Vector vctData=new Vector(3,2);
  25.         v.addElement(strProduct);
  26.         v.addElement(dblPrice);
  27.         v.addElement(intQty);
  28.         dtmRecordDetails.addRow(v);
  29.  
Thanks
Rajkumar
Aug 20 '12 #2
Thanks Rajkumar... I'll try to do this... I hope it will work...
Aug 20 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Igor Shulgin | last post by:
Hi! What is standard and direct way (within Oracle 9.2 stored procedures) for writing binary data from Oracle to external file on disk? We have tried to use UTL_FILE package, but it operates on...
1
by: Kevin Myers | last post by:
Hello, I'm an experienced application developer in some languages (including various SQL dialects), but have very little experience with MS Access or VBA, and am having trouble figuring out how...
5
by: Xero | last post by:
How do you display an external file to the user in vb.net? Thanks. -- Xero http://www.chezjeff.net My personal web portal
4
by: Geoff Cox | last post by:
Hello, Just starting C# with MS Visual Studio Pro 2005 ... How do I access array data which is in an external file? I would prefer to have it there to make the code less cluttered.. Thanks...
2
by: teddarr | last post by:
I'm having trouble reading the first 2 lines of data in an external file. I am supposed to use a while loop to read the first 2 lines of an external file that contains several random integers. I...
1
by: teddarr | last post by:
I'm having trouble reading the first 2 lines of data in an external file. I am supposed to use a while loop to read the first 2 lines of an external file that contains several random integers. I...
4
by: McGowan | last post by:
Hi, I'm trying to display data from a mysql database in a HTML table but for some reason my code isn't working. At the moment I have got it to read and display the headers and the first row of the...
4
by: seth_hickel | last post by:
With other solutions I would get a recordset, read each record and display data by formating my html as I wanted to display values of each record. I am trying to display data in a three column...
4
by: shelley_2000 | last post by:
What is the best approach to collect and load Employee Resume Data from External Employees who may not have Microsoft access? If is likely they will have Microsoft Word, but not Microsoft Access. ...
0
by: amel86 | last post by:
Hello everyone, My problem is i want to display data in txtfile to the xml file. For example i write www.yahoo.com in the test.txt and i want to display the link (www.yahoo.com) in the xml file....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.