gpt4 book ai didi

java - 如何使用字符串和数组将数据发送到 JTable

转载 作者:行者123 更新时间:2023-12-01 11:05:29 26 4
gpt4 key购买 nike

这是我从库存溢出中退出的第一个问题,我已经为一个类创建了一个类和 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 并创建字符串和对象

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);

}

}

}

最佳答案

由于您已经有一个包含 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

关于java - 如何使用字符串和数组将数据发送到 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33004826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com