我有ArrayList
个OrderEntry
个对象。每个对象都有:
public class OrderEntry
{
private int quantity;
private String itemName;
private float rate;
private float value;
}
事情是,我有一个JButton
,当我点击这个按钮时,我希望每个对象的值连续有4列。
我是Swing和JTable/TableModel
的新手,有人可以帮我解决一个例子吗?
答案 0 :(得分:1)
创建一个表模型来保存订单 - TableModel的实现。然后在表格中设置模型。这是一个很好的开始 - How to Use Tables教程。以下示例说明了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();
}
});
}
}
答案 1 :(得分:1)
要获得更灵活,可重用(但更复杂)的表格模型,请参阅Bean Table Model。