jtable 标头中的复选框事件
您好,我有一个 jtable,列中带有复选框,列标题
问题是如果我单击第一个列标题, 第一个列标题是选择的,第二个列标题是
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 事件之前,因此我无法获取单击的列
任何想法?
hi i have a jtable with checkbox in column and column header
problem is if i click on the first column header,
the first column header is select and the the second one
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()));
i created a table cell renderer
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());
}
}
}
image at the top nothing selected
image at the bottom after a click on the column header (AM)
getTableCellRendererComponent happen before the mouseClicked event so i'm not able to get the clicked column
any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该在设置值循环中传递列索引
You should pass your column index in the set value loop
尝试此操作,
您必须指出要修改的列
并
try this
you mus indicate the column to modify
and
问题出在鼠标监听器上。两个实例都列出了鼠标单击标题上的列 ==0 的情况。因此,当您单击该标题时,两者都会被选中或取消选中。
The problem is in the mouse listener. Both instances are listing for mouse clicks on the header that have a column ==0. Therefore both get checked or unchecked when you click on that header.