📄 checkboxcelleditor.java
字号:
package com.vbank.admin.component;
import java.awt.Component;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
public class CheckBoxCellEditor extends DefaultCellEditor {
private static JCheckBox m_checkBox = new JCheckBox();
private int selectnum;
public CheckBoxCellEditor() {
super(m_checkBox);
m_checkBox.setOpaque(false);
m_checkBox.setHorizontalAlignment(JLabel.CENTER);
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if (value instanceof Boolean&&column!=1)
{
Boolean b = (Boolean) value;
m_checkBox.setSelected(b.booleanValue());
System.out.println("当前选择状态:"+b.booleanValue());
System.out.println("当前行:"+row);
//为什么false才行,待解
if((b.booleanValue())==false){
if(selectnum<14){
selectnum++;
System.out.println("已选择" + selectnum + "项!\n");
}
else{
JOptionPane.showMessageDialog(null,"只能选择两项,请重新选择!");
}
}
else{
selectnum--;
System.out.println("已选择" + selectnum + "项!\n");
}
}
m_checkBox.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
m_checkBox.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
/**
* Component component = getComponent(); if (component instanceof JCheckBox){ m_checkBox = (JCheckBox)component;
*
*
* table.setValueAt(Boolean.valueOf(String.valueOf(m_checkBox.isSelected())),row,column); return m_checkBox; } else return component;
*/
return m_checkBox;
}
public Object getCellEditorValue() {
return new Boolean(m_checkBox.isSelected());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -