473,545 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to display data in a table from an external file

aquagirl20
2 New Member
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 CarRecordManage r 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 CarRecordManage r 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, 359 views)
Aug 19 '12 #1
2 2416
rajujrk
107 New Member
Hi,

See, if you want to load the data into JTable, you have to use DefaultTableMod el. 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
2 New Member
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
9027
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 files consisting out of "line" of size no more than 32767 bytes and adds new_line symbol upon closing files. Also we checked out interMedia types...
1
2471
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 to even get started with the following seemingly simple task... I have a table with a composite primary key made up of three columns. The lowest...
5
4744
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
1829
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 Geoff
2
1778
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 think I need 2 loops. I also think it is best not to reinitialize the variables 'sum' and 'num' before the second loop executes. The overall goal...
1
1810
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 think I need 2 loops. I also think it is best not to reinitialize the variables 'sum' and 'num' before the second loop executes. The overall goal is...
4
21282
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 table and it actually creates the remaining rows in the html table but it doesn't put any data in them. This is my code so far: <?php $con =...
4
2059
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 three row table with paging. How does this translate to ASP.NET - or - what ASP.NET tools do I use to accomplish this and how. Thank you.
4
3385
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. Is there any type of form/template that I could send to employees to fill out and send back for me to import into the Access Database? Or should...
0
1256
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. Please anyone help me. <albuminfo> <artLocation>slideshow/slide1.jpg</artLocation> <artist>The Doors</artist>
0
7478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7410
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7668
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.