public class InterfaceCallExample extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JButton callButton; private JTable resultTable; private DefaultTableModel tableModel; public InterfaceCallExample() { super("Interface Call Example"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); callButton = new JButton("查看数据"); callButton.addActionListener(this); panel.add(callButton); add(panel, BorderLayout.NORTH); tableModel = new DefaultTableModel(); tableModel.addColumn("id"); tableModel.addColumn("材料类型"); tableModel.addColumn("状态"); tableModel.addColumn("创建时间"); tableModel.addColumn("更改时间"); resultTable = new JTable(tableModel); add(new JScrollPane(resultTable), BorderLayout.CENTER); } public static void main(String[] args) { InterfaceCallExample example = new InterfaceCallExample(); example.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == callButton) { try { URL url = new URL("http://101.34.120.182:8081/wnapp/productType/getTypeId?category=1&status=0&type="); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String s = reader.readLine(); JSONObject jsonObject = new JSONObject(); JSONObject object = JSONObject.parseObject(s); Object o = object.get("data"); JSONObject object1 = JSONObject.parseObject(o.toString()); ArrayList<Map<String,Object>> list = (ArrayList<Map<String,Object>>) object1.get("list"); for (Map<String,Object> map : list){ String id = map.get("id").toString(); String titleName = map.get("titleName").toString(); String status = map.get("status").toString(); String createTime = map.get("createTime").toString(); String updateTime = map.get("updateTime").toString(); ArrayList<String> strings = new ArrayList<>(); strings.add(id); strings.add(titleName); strings.add(status); strings.add(createTime); strings.add(updateTime); String[] data = strings.toString().replace("[","").replace("]","").split(","); tableModel.addRow(data); } reader.close(); } catch (Exception ex) { ex.printStackTrace(); } } } } 将以上代码获取的每条数据后面添加删除和修改的按钮
时间: 2024-03-20 21:41:21 浏览: 71
可以在表格中添加一个新的列,用于显示每一行的“删除”和“修改”按钮。每当添加一行数据时,就在该列中添加对应的按钮。以下是修改后的代码:
```
public class InterfaceCallExample extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton callButton;
private JTable resultTable;
private DefaultTableModel tableModel;
private JButton[] editButtons; // 修改按钮
private JButton[] deleteButtons; // 删除按钮
public InterfaceCallExample() {
super("Interface Call Example");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
callButton = new JButton("查看数据");
callButton.addActionListener(this);
panel.add(callButton);
add(panel, BorderLayout.NORTH);
tableModel = new DefaultTableModel();
tableModel.addColumn("id");
tableModel.addColumn("材料类型");
tableModel.addColumn("状态");
tableModel.addColumn("创建时间");
tableModel.addColumn("更改时间");
tableModel.addColumn("操作"); // 操作列
resultTable = new JTable(tableModel);
add(new JScrollPane(resultTable), BorderLayout.CENTER);
}
public static void main(String[] args) {
InterfaceCallExample example = new InterfaceCallExample();
example.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == callButton) {
try {
URL url = new URL("http://101.34.120.182:8081/wnapp/productType/getTypeId?category=1&status=0&type=");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String s = reader.readLine();
JSONObject jsonObject = new JSONObject();
JSONObject object = JSONObject.parseObject(s);
Object o = object.get("data");
JSONObject object1 = JSONObject.parseObject(o.toString());
ArrayList<Map<String,Object>> list = (ArrayList<Map<String,Object>>) object1.get("list");
int rowCount = tableModel.getRowCount();
for (int i = 0; i < rowCount; i++) {
tableModel.removeRow(0);
}
editButtons = new JButton[list.size()];
deleteButtons = new JButton[list.size()];
for (int i = 0; i < list.size(); i++) {
Map<String,Object> map = list.get(i);
String id = map.get("id").toString();
String titleName = map.get("titleName").toString();
String status = map.get("status").toString();
String createTime = map.get("createTime").toString();
String updateTime = map.get("updateTime").toString();
ArrayList<String> strings = new ArrayList<String>();
strings.add(id);
strings.add(titleName);
strings.add(status);
strings.add(createTime);
strings.add(updateTime);
String[] data = strings.toString().replace("[","").replace("]","").split(",");
tableModel.addRow(data);
// 添加修改按钮
editButtons[i] = new JButton("修改");
editButtons[i].addActionListener(new EditButtonActionListener(tableModel, i));
resultTable.setValueAt(editButtons[i], i, 5);
// 添加删除按钮
deleteButtons[i] = new JButton("删除");
deleteButtons[i].addActionListener(new DeleteButtonActionListener(tableModel, i));
resultTable.setValueAt(deleteButtons[i], i, 6);
}
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
// 修改按钮事件处理器
private class EditButtonActionListener implements ActionListener {
private DefaultTableModel tableModel;
private int row;
public EditButtonActionListener(DefaultTableModel tableModel, int row) {
this.tableModel = tableModel;
this.row = row;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO: 实现修改操作
System.out.println("修改第" + row + "行数据");
}
}
// 删除按钮事件处理器
private class DeleteButtonActionListener implements ActionListener {
private DefaultTableModel tableModel;
private int row;
public DeleteButtonActionListener(DefaultTableModel tableModel, int row) {
this.tableModel = tableModel;
this.row = row;
}
@Override
public void actionPerformed(ActionEvent e) {
tableModel.removeRow(row);
}
}
}
```
在上述代码中,我们添加了两个内部类 `EditButtonActionListener` 和 `DeleteButtonActionListener`,分别用于处理“修改”和“删除”按钮的事件。在 `actionPerformed` 方法中,我们将每一行的“修改”和“删除”按钮添加到表格中,同时为每个按钮添加相应的事件处理器。当用户单击“修改”或“删除”按钮时,相应的事件处理器会被触发,从而实现对数据的修改或删除。
阅读全文