By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

how to display data in a table from an external file

aquagirl20
P: 2
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, 302 views)
Aug 19 '12 #1
Share this Question
Share on Google+
2 Replies


100+
P: 107
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

aquagirl20
P: 2
Thanks Rajkumar... I'll try to do this... I hope it will work...
Aug 20 '12 #3

Post your reply

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