jinlin
4 天以前 66f0597bf6a1e79540c6bc51dedf561c22f3bdb5
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
package com.example.client.utils;
 
import com.example.client.dto.ColumnDto;
 
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxEditor;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
public class MultiSelectComboBox2 extends JComboBox<String> {
    public Set<Integer> selectedIndices = new HashSet<>();
    private String defaultText; // 默认显示文本
 
    public MultiSelectComboBox2(String[] items, List<ColumnDto> columnDto, TableColumnModel columnModel) {
        super(items);
        this.defaultText = "自定义表头"; // 设置默认文本
        setEditable(true);
        // 自定义渲染器
        setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                JCheckBox checkBox = new JCheckBox();
                checkBox.setText(value.toString()); // 使用字符串作为文本
                checkBox.setSelected(selectedIndices.contains(index)); // 设置选中状态
                checkBox.setEnabled(list.isEnabled());
                checkBox.setFont(list.getFont());
                checkBox.setFocusPainted(false);
                checkBox.setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
                checkBox.setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());
                return checkBox;
            }
        });
 
        // 自定义编辑器
        setEditor(new BasicComboBoxEditor() {
            private JTextField textField;
 
            @Override
            public Component getEditorComponent() {
                if (textField == null) {
                    textField = new JTextField(defaultText); // 设置默认文本
                    textField.setEditable(false); // 设置为不可编辑
                }
                return textField;
            }
 
            @Override
            public void setItem(Object anObject) {
                // 禁用自动更新编辑器文本的行为
                if (textField != null) {
                    textField.setText(defaultText); // 始终显示默认文本
                }
            }
        });
 
        // 添加动作监听器
        addActionListener(e -> {
            int selectedIndex = getSelectedIndex();
            if (selectedIndex != -1) {
                if (selectedIndices.contains(selectedIndex)) {
                    selectedIndices.remove(selectedIndex);
                    if (columnModel != null) {
                        columnModel.getColumn(selectedIndex).setPreferredWidth(2);
                    }
                } else {
                    selectedIndices.add(selectedIndex);
                    if (columnModel != null) {
                        Integer width = columnDto.get(selectedIndex).getColumnWidth();
                        columnModel.getColumn(selectedIndex).setPreferredWidth(width);
                    }
                }
            }
        });
 
        // 默认选中所有项
        for (int i = 0; i < getItemCount(); i++) {
            selectedIndices.add(i); // 将所有索引添加到选中集合
        }
    }
 
    public Set<String> getSelectedItems() {
        Set<String> selectedItems = new HashSet<>();
        for (Integer index : selectedIndices) {
            selectedItems.add((String) getItemAt(index));
        }
        return selectedItems;
    }
 
    public void setSelectedItems(Set<String> items) {
        selectedIndices.clear(); // 清空当前选中项
        for (int i = 0; i < getItemCount(); i++) {
            String item = getItemAt(i);
            if (items.contains(item)) {
                selectedIndices.add(i); // 将对应的索引添加到选中集合
            }
        }
    }
 
    public Set<Integer> getUnselectedIndices() {
        Set<Integer> unselectedIndices = new HashSet<>();
        for (int i = 0; i < getItemCount(); i++) {
            if (!selectedIndices.contains(i)) {
                unselectedIndices.add(i);
            }
        }
        return unselectedIndices;
    }
 
    public Set<Integer> getSelectedIndices() {
        return new HashSet<>(selectedIndices);
    }
}