java-JTable设置禁用复选框查找不可编辑的单元格
作者:互联网
我有一个布尔值列的JTable.根据存储在模型中的状态,我使它们中的一些或全部不可编辑(模型的isCellEditable()返回false).但是,这不会使JTable布尔渲染器将复选框渲染为不可编辑单元格已禁用.
除了编写自定义布尔渲染器以外,还有什么方法可以实现?
如果需要编写自己的渲染器,除了JCheckbox之外,还应该扩展哪个类?我只需要在渲染之前禁用该复选框即可,并且不想实现所有渲染代码并处理选定的外观.
解决方法:
However this does not make the JTable boolean renderer to render the checkboxes as disabled for uneditable cell.
这是正确的,因为这是默认渲染器的行为:JCheckBox是不可编辑的,但不能禁用.
Is there a way how to achive this other than writing custom boolean renderer?
不,据我所知.
If I need to write my own renderer what class should I extend other than JCheckbox?
扩展任何类以实现TableCellRenderer接口不是强制性的.您可以完美地将JCheckBox作为渲染器的类成员.实际上,组合优先于继承.
I just simply need to disable the checkbox before rendering and do not want to implement all the rendering code and handle selected look and stuff.
这并不困难,您可以控制正在发生的事情.考虑下面的示例:
class CheckBoxCellRenderer implements TableCellRenderer {
private final JCheckBox renderer;
public CheckBoxCellRenderer() {
renderer = new JCheckBox();
renderer.setHorizontalAlignment(SwingConstants.CENTER);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Color bg = isSelected ? table.getSelectionBackground() : table.getBackground();
renderer.setBackground(bg);
renderer.setEnabled(table.isCellEditable(row, column));
renderer.setSelected(value != null && (Boolean)value);
return renderer;
}
}
有关相关问题,请参阅此问题与解答:JXTable: use a TableCellEditor and TableCellRenderer for a specific cell instead of the whole column
标签:jcheckbox,boolean,swing,jtable,java 来源: https://codeday.me/bug/20191029/1956897.html