简体   繁体   English

如何从不同的类向jtable添加数据?

[英]How to add data to jtable from different class?

I am working on a GUI project based on groceryproducts. 我正在基于杂货产品的GUI项目中工作。 For the program, I have four window(the first one is main window, the second to add product to inventory and add the product in delimited text file, the third to remove product from inventory as well remove it from delimited text file and fourth to display products added to inventory from delimited text file).Now, I have finished making the three windows but I seem to been stuck when working on displaying products added to inventory. 对于该程序,我有四个窗口(第一个是主窗口,第二个是将产品添加到库存并将产品添加到定界文本文件中,第三个是将产品从库存中删除以及从定界文本文件中删除它,第四个是显示由定界文本文件添加到库存中的产品。)现在,我已经完成了三个窗口的制作,但是在显示添加到库存中的产品时似乎卡住了。 For viewing products I have a file in windows package containing all window classes for different windows in which viewGroceryProductInventory class has JTable added to it. 对于查看产品,我在windows包中有一个文件,其中包含不同窗口的所有窗口类,其中viewGroceryProductInventory类已将JTable添加到其中。

    package prog24178.javaassassins.project.window;
    import java.util.ArrayList;
    import javax.swing.*;
    import javax.swing.table.*;
    import prog24178.javaassassins.guiproject.GroceryProduct;
    import prog24178.javaassassins.project.io.ViewProductsFromInventory;

  /**
    *
    * @author Bharat
    */
    public class ViewGroceryProductsWindow extends JPanel {

    //Create table with column names



                private String[] columnNames = {"Name of Product"
                                    ,"UPC Code","Price","Stock"
                                    ,"International","Department"};
                private Object[][] data = {};
                private DefaultTableModel tableModel = 
                               new DefaultTableModel(data,columnNames);
                private JTable table = new JTable(tableModel);



                public ViewGroceryProductsWindow(){
                JScrollPane jscroll = new JScrollPane(table);

                this.add(jscroll);


                tableModel.addRow(data);
         }

    }

I have another io package in which there are different classes to remove and add product to delimited text file. 我有另一个io包,其中有不同的类要删除,并将产品添加到带分隔符的文本文件中。 For ViewAGroceryProductInInventory class I have finished extracting the data from the file but now what should I do to add my fields into row in JTable which is defined in the windows class in window package? 对于ViewAGroceryProductInInventory类,我已经完成了从文件中提取数据的操作,但是现在我该怎么做才能将字段添加到JTable中的行中,该JTable在window包的windows类中定义? I know the data for row has to be in inserted into tableModel in order to display the row. 我知道行的数据必须插入到tableModel中才能显示行。 Now, the problem I am facing is that I am doing this from another class in another package , so how I can add the fields from text file to the row data in the class in window package from the class in io package? 现在,我面临的问题是我正在另一个包中的另一个类中执行此操作,那么如何从io包中的类将文本文件中的字段添加到window包中的类中的行数据中呢?

package prog24178.javaassassins.project.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import prog24178.javaassassins.guiproject.GroceryProduct;


/**
  *
  * @author Bharat
  */
 public class ViewProductsFromInventory{    


       Scanner input = null;  
       int counter = 0;

       public ViewProductsFromInventory(){

             File file = new File("GroceryProduct.txt");
             if(!file.exists()){
                    System.out.println("GroceryProduct.txt doesn't exist");
                    System.exit(0);
             }

             try{

                  input = new Scanner(file);
                  ArrayList<GroceryProduct> inventory = new ArrayList();
                  int counter=0;
                  while(input.hasNext()){

                       String record = input.next();      
                       String[] fields = record.split("\\|");

                       String nameOfProduct= fields[0];

                       String upcCodeOfProduct = fields[1];


                       double priceOfProduct = Double.parseDouble(fields[2]);
                       Boolean productInStock = Boolean.parseBoolean(fields[3]);
                       Boolean FromInternational = Boolean.parseBoolean(fields[4]);
                       String productDepartment = fields[5];                
                       GroceryProduct groceryProduct = 
                                      new GroceryProduct(nameOfProduct,
                                                        upcCodeOfProduct,priceOfProduct,
                                                         productInStock,
                                                       FromInternational,
                                                     productDepartment);


                       inventory.add(groceryProduct);

               }
                input.close();

               }catch(FileNotFoundException ex){

                System.out.print(ex.getLocalizedMessage());

             }
      }
   }

Column names and row data are part of the program's data model , so I'd give ProductsFromInventory a public getModel() method that returns a fully constructed TableModel , suitable for initializing your JTable view . 列名和行数据是程序数据模型的一部分 ,因此我将给ProductsFromInventory一个公共的getModel()方法,该方法返回一个完全构造的TableModel ,适用于初始化JTable 视图

If you're allowed to define the format, you could supply the file name as a parameter to the constructor, and read the (delimited) names from the first line of the file. 如果允许定义格式,则可以将文件名作为参数提供给构造函数,然后从文件的第一行读取(定界的)名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM