这是我的第一个问题,从库存溢出退出了一点我已经为一个类和JTextField
一个类,我有一个问题,一旦我点击完整的订单按钮它应该保存所有的数据和信息很好,酷JTable
我被认为使用String[]
和Object
但基本上我不知道如何做到这一点???
这是我的软件类
class Software extends Products {
private float ram;
private float processor;
//SoftwareProducts sf = new SoftwareProducts();
public Software(int productID, String productName,
int productYear, String productPublishHouse) {
super(productID, productName, productYear, productPublishHouse);
this.ram = ram;
this.processor = processor;
//super(productID,productName, productYear, productPublishHouse);
}
public void setRam(float ram) {
this.ram = ram;
}
public float getRam() {
return ram;
}
public void setProcessor(float processor) {
this.processor = processor;
}
public float getProcessor() {
return processor;
}
}
//int productID,String productName,int productYear,
String productPublishHouse
这是我的客户订单类,基本上我需要从中运行JTable并生成String和Object
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.io.*;
public class CustomerOrder extends JPanel
implements ActionListener {
//JButton asd = new JButton("BUTTON");
//JTextField productID;
//JTextField productName;
//JTextField productCost;
//JTextField productPYear;
//JTextField productPHouse;
JButton showOrder;
DefaultListModel<String> model = new DefaultListModel<>();
JList<String> orderList = new JList(model);
JTable softwareTabel = new JTable();
//DefaultTableModel tableModel = new DefaultTableModel(0, 1);
public CustomerOrder() {
//super();
// JLabel toLabel=new JLabel("Product ID: ");
//JTextField to=new JTextField();
setLayout(new FlowLayout());
/*
productName = new JTextField(12);
add(productName);
productCost = new JTextField(12);
add(productCost);
productPYear = new JTextField(12);
add(productPYear);
productPHouse = new JTextField(12);
add(productPHouse);
*/
showOrder = new JButton("SHOW ORDER");
showOrder.setSize(25, 40);
showOrder.addActionListener(this);
add(showOrder);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == showOrder) {
model.removeAllElements();
/* GridLayout exLayout = new GridLayout(3, 3);
JLabel ram,processor;
ram = new JLabel("RAM");
processor = new JLabel("Processor");
String softwaredata[] = {"ID","RAM","Processor","Product ID","Product Name","Product Year","Product Year","Product PublishHouse"};
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(8, 3));
add(ram);
add(processor);
JTable table = new JTable();*/
//DeafultTableModel dm = new DeafultTableModel(0,0);
//String header[] = new String[] {"RAM", "Processor","ProductID","Product Name","Product Year","Product Publish House"};
//dm.setColumnIdentifiers(header);
//Object[][] software = new Object[8][3];
//model.addRow(orderList.toArray());
int x = 0;
while (x < main.softwareList.size()) {
//./model.addElement(main.softwareList.get(x).getproductYear());
model.addElement("" + main.softwareList.get(x).getproductID());
model.addElement("" + main.softwareList.get(x).getRam());
model.addElement("" + main.softwareList.get(x).getProcessor());
model.addElement("" + main.softwareList.get(x).getproductID());
model.addElement(main.softwareList.get(x).getproductName());
model.addElement("" + main.softwareList.get(x).getproductYear());
model.addElement(main.softwareList.get(x).getproductPublishHouse());
//model.addElement(main.softwareList.get(x).getproductID());*/
x++;
//System.out.println("as");
}
ArrayList<String> table1 = new ArrayList();
ArrayList<ArrayList<String>> table2 = new ArrayList();
table1.add("Product ID");
table1.add("RAM");
table1.add("Processor");
table1.add("Product Name");
table1.add("Product Year");
table1.add("Product PHouse");
Object[] software = table1.toArray();
String[][] softwarest = new String[table1.size()][];
int i = 0;
for (List<String> next : table2) {
softwarest[i++] = next.toArray(new String[next.size()]);
}
JTable newTable = new JTable(softwarest, software);
JFrame frame = new JFrame();
frame.getContentPane().add(new JScrollPane(newTable));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//add(model);
add(orderList);
//Object[] column = {"RAM:", "Processor:"};
//Object[][] data = {{"RAM:", "Processor:"}};
//JTable table = new JTable();
//table.setShowGrid(false);
//table.setTableHeader(null);
}
}
}
答案 0 :(得分:1)
由于您已经拥有了一个包含Produce
基本属性的POJO,因此更简单(更可重用)的解决方案是为其创建一个专用的ProductTableModel
...
public static class ProductTableModel extends AbstractTableModel {
protected static final String[] COLUMN_NAMES = {"Product ID",
"RAM",
"Processor",
"Product Name",
"Product Year",
"Product PHouse"};
private List<Product> products;
public ProductTableModel(List<Product> products) {
this.products = new ArrayList<>(products);
}
@Override
public int getRowCount() {
return products.size();
}
@Override
public int getColumnCount() {
return COLUMN_NAMES.length;
}
@Override
public String getColumnName(int column) {
return COLUMN_NAMES[column];
}
public Product getProductAt(int row) {
return products.get(row);
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Product product = getProductAt(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = product.getID();
break;
case 1:
value = product.getRAM();
break;
case 2:
value = product.getProcessor();
break;
case 3:
value = product.getName();
break;
case 4:
value = product.getYear();
break;
case 5:
value = product.getPHouse();
break;
}
return value;
}
}
然后你可以简单地做一些像......
ProductTableModel model = new ProductTableModel(listOfProducts);
productTable.setModel(model); // A JTable