0

我有一个ArrayList对象OrderEntry。每个对象都有:

public class OrderEntry
{
    private int quantity;
    private String itemName;
    private float rate;
    private float value;
}

事情是,我有一个JButton,当我单击此按钮时,我希望每个对象的值在一行中有 4 列。

我是 Swing 新手JTable/TableModel,谁能帮我举个例子?

4

2 回答 2

1

创建一个表模型来保存订单 - TableModel的实现。然后在表格中设置模型。这是一个好的开始 -如何使用表格教程。下面是一个示例,说明了AbstractTableModel.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;

public class OrderEntryTable {

    public OrderEntryTable() {
        JFrame frame = new JFrame("OrderEntryTable");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final OrderEntryModel model = new OrderEntryModel();
        JTable table = new JTable();
        table.setModel(model);
        JScrollPane scrollPane = new JScrollPane(table);
        JPanel content = new JPanel(new BorderLayout());
        content.add(scrollPane, BorderLayout.CENTER);
        JButton button = new JButton("Add order entry");
        content.add(button, BorderLayout.SOUTH);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                model.addOrderEntry(new OrderEntry(3, "name", 0.5f, 1.0f));
            }
        });
        frame.add(content);
        frame.pack();
        frame.setVisible(true);
    }

    public class OrderEntryModel extends AbstractTableModel {

        private static final long serialVersionUID = 1L;
        private List<OrderEntry> orders = new ArrayList<OrderEntry>();

        public OrderEntryModel() {
            //TODO - setup orders
        }

        public void addOrderEntry(OrderEntry entry) {
            orders.add(entry);
            int lastRow = orders.size() - 1;
            fireTableRowsInserted(lastRow, lastRow);
        }

        @Override
        public int getRowCount() {
            return orders.size();
        }

        @Override
        public int getColumnCount() {
            return 4;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            OrderEntry entry = orders.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    return entry.getQuantity();
                case 1:
                    return entry.getItemName();
                case 2:
                    return entry.getRate();
                case 3:
                    return entry.getValue();
            }
            return null;
        }

        @Override
        public String getColumnName(int column) {
            switch (column) {
                case 0:
                    return "Quantity";
                case 1:
                    return "ItemName";
                case 2:
                    return "Rate";
                case 3:
                    return "Value";
            }
            return null;
        }
    }

    public class OrderEntry {

        private int quantity;
        private String itemName;
        private float rate;
        private float value;

        public OrderEntry(int quantity, String itemName, 
               float rate, float value) {
            super();
            this.quantity = quantity;
            this.itemName = itemName;
            this.rate = rate;
            this.value = value;
        }

        public int getQuantity() {
            return quantity;
        }

        public void setQuantity(int quantity) {
            this.quantity = quantity;
        }

        public String getItemName() {
            return itemName;
        }

        public void setItemName(String itemName) {
            this.itemName = itemName;
        }

        public float getRate() {
            return rate;
        }

        public void setRate(float rate) {
            this.rate = rate;
        }

        public float getValue() {
            return value;
        }

        public void setValue(float value) {
            this.value = value;
        }
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new OrderEntryTable();
            }
        });
    }
}
于 2013-03-17T20:47:39.083 回答
1

有关更灵活、可重用(但更复杂)的表模型,请参阅Bean 表模型

于 2013-03-18T01:23:22.583 回答