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