Java Swing - JTable不在滚动窗格中显示

时间:2017-07-11 16:11:15

标签: java swing jtable

我创建了一个类Cart,里面是一个JTable和两个ArrayLists。出于某种原因,我的JTable没有显示。

这是我的购物车类:

class Cart {
    ArrayList<Product> products = new ArrayList<>(); // Holds the products themselves
    ArrayList<Integer> quantities = new ArrayList<>(); // Holds the quantities themselves
    JTable prdTbl = new JTable(); // The GUI Product Table
    DefaultTableModel prdTblModel = new DefaultTableModel(); // The Table Model
    Object[] columns = {"Description","Price","Quantity","Total"}; // Column Identifiers
    DecimalFormat fmt = new DecimalFormat("$#,##0.00;$-#,##0.00"); // Decimal Format for formatting USD ($#.##)

    Cart() {
        setTableStyle();
    }

    void renderTable() {        
        // Re-initialize the Table Model
        this.prdTblModel = new DefaultTableModel();

        // Set the Table Style
        setTableStyle();

        // Create a row from each list entry for product and quantity and add it to the Table Model
        for(int i = 0; i < products.size(); i++) {
            Object[] row = new Object[4];

            row[0] = products.get(i).getName();
            row[1] = products.get(i).getPrice();
            row[2] = quantities.get(i);
            row[3] = fmt.format(products.get(i).getPrice() * quantities.get(i));

            this.prdTblModel.addRow(row);
        }

        this.prdTbl.setModel(this.prdTblModel);
    }

    void setTableStyle() {
        this.prdTblModel.setColumnIdentifiers(columns);
        this.prdTbl.setModel(this.prdTblModel);
        this.prdTbl.setBackground(Color.white);
        this.prdTbl.setForeground(Color.black);
        Font font = new Font("Tahoma",1,22);
        this.prdTbl.setFont(font);
        this.prdTbl.setRowHeight(30);
    }

    JTable getTable() {
        renderTable(); // Render Table
        return this.prdTbl;
    }
}

注意:已经删除了一些方法,例如addProduct()和removeProduct(),因为我觉得它们并不是必需的。如果您需要看到它们,请询问。

这是我的Swing应用程序窗口的initialize()方法:

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 590, 425);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Cart cart = new Cart();

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    frame.getContentPane().add(tabbedPane, "cell 0 0,grow");

    JPanel cartPanel = new JPanel();
    tabbedPane.addTab("Cart", null, cartPanel, null);
    cartPanel.setLayout(new MigLayout("", "[grow]", "[][grow]"));

    JScrollPane scrollPane = new JScrollPane();
    cartPanel.add(scrollPane, "cell 0 1,grow");

    table = new JTable();
    scrollPane.setViewportView(table);

    JButton btnAdd = new JButton("Add");
    btnAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String[] item = {"Macadamia", "Hazelnut", "Almond", "Peanut", "Walnut", "Pistachio", "Pecan", "Brazil"};
            Double[] price = {2.00, 1.90, 1.31, 0.85, 1.12, 1.53, 1.25, 1.75};

            int choice = (int) (Math.random() * item.length);

            Product p = new Product(item[choice], price[choice]);

            cart.addProduct(p);
            table = cart.getTable();
        }
    });
    cartPanel.add(btnAdd, "flowx,cell 0 0");

    JButton btnRemove = new JButton("Remove");
    cartPanel.add(btnRemove, "cell 0 0");

    JButton btnClear = new JButton("Clear");
    cartPanel.add(btnClear, "cell 0 0");

}

我不确定我是否在这里遗漏了什么?它过去一直很好用吗?我也尝试在table = cart.getTable();处打印出值,并且它似乎正在接受这些值,因此它让我相信它与Swing initialize()而不是我的{Cart有关。 {1}}类,但以防万一我也发布了Cart类。

2 个答案:

答案 0 :(得分:1)

您确定要添加正确的表吗?您的代码显示:

table = new JTable();
scrollPane.setViewportView(table);

我无法看到声明的表格,更多的表格中没有任何内容,没有行没有列,而在actionListener中,您使用新实例初始化表格:

table = cart.getTable();

但scrollPane包含另一个JTable实例。

答案 1 :(得分:1)

看起来您从未将购物车与购物车相关联;我认为你的问题在这里:

 JPanel cartPanel = new JPanel();

你制作了新的面板,但从未将你的购物车挂在上面。看起来不错。

祝你好运!