JTable-复选框添加操作侦听器 [英] JTable - Checkbox add action listener

查看:133
本文介绍了JTable-复选框添加操作侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的复选框创建了一个简单的JTable:

DefaultTableModel model = new DefaultTableModel();
jTable1.setModel(model);

model.addColumn("No:", no1);
model.addColumn("Remark", remark1);
model.addColumn("Color", colors1);
model.addColumn("Done");

TableColumn col1 = jTable1.getColumnModel().getColumn(0);
col1.setPreferredWidth(1);

TableColumn col4 = jTable1.getColumnModel().getColumn(3);
col4.setCellEditor(jTable1.getDefaultEditor(Boolean.class));
col4.setCellRenderer(jTable1.getDefaultRenderer(Boolean.class));
col4.setPreferredWidth(50);

jTable1.setShowGrid(true);
jTable1.setGridColor(Color.BLACK);
jTable1.setAutoCreateRowSorter(true); 

它工作正常,但是如果我想为复选框添加动作侦听器,该怎么办.例如,当我的复选框被选中时,我需要弹出一条确认消息.

解决方案

例如,当我的复选框被选中时,我需要弹出一个 确认消息.

您不需要添加 ActionListener 到渲染器/编辑器,但是您需要侦听表模型数据的更改.看一下侦听数据更改部分如何使用表格教程:

编辑

在这种情况下,请注意,当您使用布尔值时,有2种可能的值可以进行检查.但是,对于在其他情况下的输入验证,所描述的过程将无法简单地工作,因为当更改已经发生时,侦听器将收到通知,并且您将无法再设置该值,因为该值将不再存在. /p>

看看@kleopatra对这个问题的回答: JTable输入验证器.如前所述,一种更好的方法是提供自定义 CellEditor 并在 DefaultCellEditor 使用 JCheckBox 作为参数并覆盖上述方法.

I've created a simple JTable with check box like below:

DefaultTableModel model = new DefaultTableModel();
jTable1.setModel(model);

model.addColumn("No:", no1);
model.addColumn("Remark", remark1);
model.addColumn("Color", colors1);
model.addColumn("Done");

TableColumn col1 = jTable1.getColumnModel().getColumn(0);
col1.setPreferredWidth(1);

TableColumn col4 = jTable1.getColumnModel().getColumn(3);
col4.setCellEditor(jTable1.getDefaultEditor(Boolean.class));
col4.setCellRenderer(jTable1.getDefaultRenderer(Boolean.class));
col4.setPreferredWidth(50);

jTable1.setShowGrid(true);
jTable1.setGridColor(Color.BLACK);
jTable1.setAutoCreateRowSorter(true); 

It's working fine but how to do if I want to add action listener for the check box. For an example, when my check box is checked I need to pop up a confirmation message.

解决方案

For an example, when my check box is checked I need to pop up a confirmation message.

You don't need to add an ActionListener to the renderers/editors but you need to listen to table model data changes. Take a look to Listening for Data Changes section of How to Use Tables tutorial:

  • Add a new TableModelListener to your TableModel
  • Validate if the updated cell value is a Boolean and its value is true.
  • Ask the user to confirm the update. If s/he doesn't then set the cell's value back to false.
  • See TableModelEvent API as well.

Edit

Note in this case as you're working with booleans then there's 2 possible values to do the check. However for input validation in other cases the described procedure won't work simply because the listener will be notified when the change already happened and you won't be able to set the value back just because it won't be there any longer.

Take a look to @kleopatra's answer to this question: JTable Input Verifier. As stated there a better approach is providing a custom CellEditor and do the validation in stopCellEditing() method implementation. Just as a suggestion I'd use a DefaultCellEditor that takes a JCheckBox as parameter and override the aforementioned method.

这篇关于JTable-复选框添加操作侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆