【问题标题】:Take data from List<class> and put it in to String object从 List<class> 获取数据并将其放入 String 对象
【发布时间】:2020-06-02 10:56:38
【问题描述】:

我正在制作一个 java 简单的应用程序,以便更好地学习代码,结合一些 intellji 上的 gui。

我已经有一个主页,我只是想制作一个弹出表,其中包含我产品上的所有数据,这是我的 tableView 代码:

public class ProductTable extends JFrame
{
    private Model.product product;
    private List<product> list;

    public ProductTable() {
        list = product.ProductList();

        // headers for the table
        String[] columns = {"Product Name",
                "Quantity",
                "Expiration day",
                "Need fridge"};
        Object[][] data = new Object[][] {
        };

        // create table with data
        JTable table = new JTable(data, columns);

        // add the table to the frame
        this.add(new JScrollPane(table));
        this.setTitle("ProductList");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ProductTable();
            }
        });
    }
}   

Product 课程中我有:

product.name = String
product.quantity = int
Product.Expiration_day = String
product.fridge = boolean

我想加载到字符串对象中

Data={ {product.name, product.quantity, Product.Expiration_day, Product.fridge} ,{.....,....,....,...}{..............}};

直到我把所有的数据都放进去,现在我知道如何知道大小了,但是我需要将它一个一个地加载到数据对象中,请有人指导一下吗?!

我的产品代码:

package Model;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class product
{
    private String product_name;
    private Integer product_quantity;
    private String experation_day;
    private boolean fridge = false;
    private boolean matched= false;
    private Connection connection;

    public product()
    {
        this.product_name = "";
        this.product_quantity = 0;
        this.experation_day = "";
    }

    public product(String product_name, Integer product_quantity, String experation_day) {
        this.product_name = product_name;
        this.product_quantity = product_quantity;
        this.experation_day = experation_day;
    }

    public Integer getProduct_quantity() {
        return product_quantity;
    }

    public String getExperation_day() {
        return experation_day;
    }

    public String getProduct_name()
    {
        return product_name;
    }

    public void setProduct_quantity(Integer product_quantity) {
        this.product_quantity = product_quantity;
    }

    public void setExperation_day(String experation_day) {
        this.experation_day = experation_day;
    }

    public void setProduct_name(String product_name)
    {
        this.product_name = product_name;
    }

    public List<product> ProductList() {

        String windownsUserName = System.getProperty("user.name");
        try
        {
            System.out.println("Connecting to a selected database...");
            Connection connection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\"+windownsUserName+"\\IdeaProjects\\Software-engineer-final-project\\untitled\\src\\DButills\\account.db");
            System.out.println("Connected database successfully...");
            String sqlQuery = "SELECT product_name, product_amount, product_expration_date FROM products";
            PreparedStatement ps = connection.prepareStatement(sqlQuery);
            ResultSet rs = ps.executeQuery();
            {

            }
            List<product> products = new ArrayList<>();
            while (rs.next()) {
                product product = new product();
                product.setProduct_quantity(rs.getInt("product_amount"));
                product.setProduct_name(rs.getString("product_name"));
                product.setExperation_day(rs.getString("product_expration_date"));
                products.add(product);
            }
            return products;

        } catch (SQLException e) {
            System.out.println("Query failed: " + e.getMessage());
            return null;
        }
    }
}

【问题讨论】:

  • 循环浏览产品列表,根据需要将数据、逗号和大括号附加到结果字符串以构建所需的格式。
  • 你也可以写一个toString方法?

标签: java swing jframe jtable


【解决方案1】:
'package viewer;

导入模型.product;

导入 javax.swing.*;

导入 java.awt.*;

导入 java.util.List;

公共类 ProductTable 扩展 JFrame

{ 私有 Model.product 产品;

private Container c;

private JTable table;

Object[] mdata;

private List<product> list;

String[] columns = {"Product Name", "Quantity", "Expiration day"};  //headers for the table
int i=1;

私有对象[][] 数据 = 新对象[][] { };

public ProductTable()
{
    list=product.ProductList();
   while(i<=list.size()) {

       mdata = new Object[]{list.get(i).getProduct_name(), list.get(i).getExperation_day(), list.get(i).getProduct_quantity().toString()};

       data[0][i]=mdata[0];

       data[1][i]=mdata[1];

       data[2][i]=mdata[2];

       i++;
   }

    //create table with data

    JTable table = new JTable(data, columns);

  //add the table to the frame

    this.setTitle("ProductList");

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setVisible(true);
}

}'

【讨论】:

    【解决方案2】:

    为什么需要 String?根据您提供的代码 - 您需要的是一个数组 Object[][],但其中填充了值,因此只需遍历您的产品列表并填写您的 数据按正确顺序排列。

    编辑:

    看看这个伪代码,它应该看起来像这样:

       String[] columns = {"Product Name",
                "Quantity",
                "Expiration day",
                "Need fridge"};
       Object[][] data = new Object[list.size()][columns.length];
       int columnCounter = 0;
       for(Product product : list) {
           data[columnCounter][0] = product.getName();
           data[columnCounter][1] = product.getQuantity();
           data[columnCounter][2] = product.getExpDay();
           data[columnCounter][3] = product.getNeedFridge();
           columnCounter++;
       }
    

    【讨论】:

    • 我试图这样做,但我找不到任何 boject[][].add(object[]) 函数。
    • 对于我的情况还有其他想法或解决方案吗?
    • java中不能添加到数组对象中,语法是data[i][j] = value;
    • 它看起来很棒,但我的代码没有像我计划的那样弹出表格......所以我看不到它是否工作
    猜你喜欢
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    相关资源
    最近更新 更多