嗨,我在列和列标题中有一个带有复选框的jtable
问题是如果我点击第一列标题, 第一列标题是select,第二列是
String[] columnNames = {"Am", "Pm", "Integer", "String"};
Object[][] data = {{true, true, 1, "BBB"}, {false, true, 12, "AAA"},
{true, false, 2, "DDD"}, {false, false, 5, "CCC"},
{true, true, 3, "EEE"}, {false, false, 6, "GGG"},
{true, true, 4, "FFF"}, {false, false, 7, "HHH"}};
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
@Override
public Class<?> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
JTable table = new JTable(model);
table.getColumnModel().getColumn(0).setHeaderRenderer(
new HeaderRenderer2(table.getTableHeader()));
table.getColumnModel().getColumn(1).setHeaderRenderer(
new HeaderRenderer2(table.getTableHeader()));
我创建了一个表格单元格渲染器
public class HeaderRenderer2 implements TableCellRenderer {
private final JCheckBox check = new JCheckBox();
public HeaderRenderer2(JTableHeader header) {
check.setOpaque(false);
check.setFont(header.getFont());
header.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JTable table = ((JTableHeader) e.getSource()).getTable();
TableColumnModel columnModel = table.getColumnModel();
int viewColumn = columnModel.getColumnIndexAtX(e.getX());
int modelColumn = table.convertColumnIndexToModel(viewColumn);
if (modelColumn == 0) {
check.setSelected(!check.isSelected());
TableModel m = table.getModel();
Boolean f = check.isSelected();
for (int i = 0; i < m.getRowCount(); i++) {
m.setValueAt(f, i, 0);
}
((JTableHeader) e.getSource()).repaint();
}
}
});
}
@Override
public Component getTableCellRendererComponent(
JTable tbl, Object val, boolean isS, boolean hasF, int row, int col) {
TableCellRenderer r = tbl.getTableHeader().getDefaultRenderer();
JLabel l = (JLabel) r.getTableCellRendererComponent(tbl, val, isS, hasF, row, col);
l.setIcon(new CheckBoxIcon(check));
return l;
}
private static class CheckBoxIcon implements Icon {
private final JCheckBox check;
public CheckBoxIcon(JCheckBox check) {
this.check = check;
}
@Override
public int getIconWidth() {
return check.getPreferredSize().width;
}
@Override
public int getIconHeight() {
return check.getPreferredSize().height;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
SwingUtilities.paintComponent(
g, check, (Container) c, x, y, getIconWidth(), getIconHeight());
}
}
}
顶部的图片没有选择 单击列标题(AM)后底部的图像
getTableCellRendererComponent发生在mouseClicked事件之前,所以我无法获得点击的列
任何想法?
答案 0 :(得分:3)
问题在于鼠标监听器。两个实例都列出了具有列== 0的标题上的鼠标单击。因此,当您单击该标题时,都会被选中或取消选中。
答案 1 :(得分:3)
您应该在设置值循环中传递列索引
public void mouseClicked(MouseEvent e) {
JTable table = ((JTableHeader) e.getSource()).getTable();
TableColumnModel columnModel = table.getColumnModel();
int viewColumn = columnModel.getColumnIndexAtX(e.getX());
int modelColumn = table.convertColumnIndexToModel(viewColumn);
if (modelColumn == 0 || modelColumn == 1) {
check.setSelected(!check.isSelected());
TableModel m = table.getModel();
Boolean f = check.isSelected();
for (int i = 0; i < m.getRowCount(); i++) {
m.setValueAt(f, i, modelColumn);
}
((JTableHeader) e.getSource()).repaint();
}
}
答案 2 :(得分:0)
试试这个
您必须指明要修改的列
String[] columnNames = {"Am", "Pm", "Integer", "String"};
Object[][] data = {{true, true, 1, "BBB"}, {false, true, 12, "AAA"},
{true, false, 2, "DDD"}, {false, false, 5, "CCC"},
{true, true, 3, "EEE"}, {false, false, 6, "GGG"},
{true, true, 4, "FFF"}, {false, false, 7, "HHH"}};
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
@Override
public Class<?> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
JTable table = new JTable(model);
table.getColumnModel().getColumn(0).setHeaderRenderer(
new HeaderRenderer2(table.getTableHeader(), 0));
table.getColumnModel().getColumn(1).setHeaderRenderer(
new HeaderRenderer2(table.getTableHeader(), 1));
和
public HeaderRenderer2(JTableHeader header, final int index) {
// index is the column to be modified
check.setOpaque(false);
check.setFont(header.getFont());
header.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JTable table = ((JTableHeader) e.getSource()).getTable();
TableColumnModel columnModel = table.getColumnModel();
int viewColumn = columnModel.getColumnIndexAtX(e.getX());
int modelColumn = table.convertColumnIndexToModel(viewColumn);
if (modelColumn == index) {
check.setSelected(!check.isSelected());
TableModel m = table.getModel();
Boolean f = check.isSelected();
for (int i = 0; i < m.getRowCount(); i++) {
m.setValueAt(f, i, index);
}
((JTableHeader) e.getSource()).repaint();
}else return;
}
}
);
}