jinlin
2025-04-28 efce7ce3e63712ecc8b4c3039a73b508fc3ea880
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.example.client.service;
 
import com.example.client.dto.ColumnDto;
import com.example.client.dto.JComboBoxItem;
import com.example.client.model.TableButton;
import com.example.client.utils.CommonTable;
import com.example.server.boatFleet.service.BoatFleetService;
import com.example.server.teamGroup.model.SysTeamGroupClass;
import com.example.server.teamGroup.service.SysTeamGroupClassService;
import com.example.server.user.model.SysUser;
import com.example.server.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
 
 
@Service
public class TeamGroupManageService {
    @Autowired
    private SysTeamGroupClassService sysTeamGroupClassService;
    @Autowired
    private TeamGroupAddOrUpdate addOrUpdate;
 
    private JTable table;
    private List<ColumnDto> columnDto;
 
    public JPanel createTable(Integer width, Integer height, JFrame jFrame) {
        JPanel panel = new JPanel();
        List<SysTeamGroupClass> list;
        JPanel topJpanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        topJpanel.setPreferredSize(new Dimension(width, 37));
        topJpanel.setBackground(Color.WHITE);
        JPanel centerJpanel = new JPanel();
        centerJpanel.setPreferredSize(new Dimension(width - 20, height - 100));
        centerJpanel.setBackground(Color.WHITE);
        panel.add(topJpanel, BorderLayout.NORTH);
        panel.add(centerJpanel, BorderLayout.CENTER);
 
        JButton btnInsert = new JButton("新增");
 
        topJpanel.add(btnInsert);
 
 
        List<TableButton> buttonList = new ArrayList<>();
        buttonList.add(new TableButton("edit", "编辑"));
        buttonList.add(new TableButton("del", "删除"));
        list = sysTeamGroupClassService.getListByTree();
 
        columnDto = new ArrayList<>();
        //columnDto.add(new ColumnDto("ID", "id", -1, null,false));
        columnDto.add(new ColumnDto("序号", "", (width - 10) / 3, "autoCreate", false, null,null));
        columnDto.add(new ColumnDto("部门/专业", "name", (width - 3) / 3, null, false, null,null));
        columnDto.add(new ColumnDto("操作", "", (width - 10) / 3, "", true, buttonList,null));
 
        table = CommonTable.createCommonTable(list, columnDto);
        table.setRowHeight(25);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 
        tableModelListener(table,jFrame,list);
 
        btnInsert.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jFrame.setEnabled(false);
                SysTeamGroupClass data = new SysTeamGroupClass();
                addOrUpdate.openDialog(data,jFrame, columnDto,table);
            }
        });
 
        JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setViewportView(table);
        scrollPane.getViewport().setBackground(Color.WHITE);
        scrollPane.setPreferredSize(new Dimension(width - 20, height - 100));
        centerJpanel.add(scrollPane);
 
        return panel;
    }
 
    public void tableModelListener(JTable table,JFrame jFrame,List<SysTeamGroupClass> list){
        table.getModel().addTableModelListener(e -> {
            // 检查事件类型
            if (e.getType() == TableModelEvent.UPDATE) {
                // 获取变化的行和列
                int row = e.getFirstRow();
                int column = e.getColumn();
 
                // 获取新的值
                Object newValue = table.getModel().getValueAt(row, column);
                // 输出变化信息
                if (newValue.equals("edit")){
                    jFrame.setEnabled(false);
                    SysTeamGroupClass data = list.get(row);
                    addOrUpdate.openDialog(data,jFrame, columnDto,table);
                }else if(newValue.equals("del")) {
                    int n = JOptionPane.showConfirmDialog(null, "是否删除?", "提示", JOptionPane.YES_NO_OPTION);
                    if (n == 0) {
                        DefaultTableModel model = (DefaultTableModel) table.getModel();
                        SysTeamGroupClass data = list.get(row);
                        sysTeamGroupClassService.deleteLogic(data.getId());
                        list.remove(row);
                        model.removeRow(row);
                    }
                }
                System.out.println("单元格变化: 行=" + row + ", 列=" + column + ", 新值=" + newValue);
            }
        });
    }
 
}