JTable禁用单元格中的Checkbox

JTable禁用单元格中的Checkbox

本文介绍了JTable禁用单元格中的Checkbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个JTable并且我希望灰色显示我尝试使用自定义渲染器检查isEnabled()然后更改背景颜色但仍然无法工作的所有禁用复选框单元格。
有什么建议吗?
谢谢!!!

解决方案

Date.class ,已成为默认值。

  table.setDefaultRenderer(Date.class,new DateRenderer()); 

可以通过覆盖获得相同的外观 prepareRenderer(),如下所示,但无论类如何,都会为所有单元格调用该方法。因此, prepareRenderer()非常适合影响整行,如。

  private final JTable table = new JTable(model){

@Override
public Component prepareRenderer(TableCellRenderer renderer,int row,int col){
Component c = super.prepareRenderer(渲染器,行,col);
if(col == DATE_COL){
Calendar calendar = Calendar.getInstance();
calendar.setTime((Date)model.getValueAt(row,col));
c.setEnabled(calendar.get(Calendar.DAY_OF_MONTH)%2 == 0);
}
返回c;
}
};


Hello I have a JTable And i want to grey out all the disabled checkbox cells i tried with a custom renderer checking isEnabled() and then changing the background color but still not workin.Any suggestions?thanks!!!

解决方案

As noted in Concepts: Editors and Renderers, "a single cell renderer is generally used to draw all of the cells that contain the same type of data." You'll need to maintain the enabled state in your table model.

Addendum: As a concrete example, the data model in this example is a simple array of Date instances. Overriding getTableCellRendererComponent() as shown below causes odd days to be disabled. In this case, being odd is a property inherent to the Date value itself, but the model could be queried for any related property at all.

@Override
public Component getTableCellRendererComponent(JTable table,
    Object value, boolean isSelected, boolean hasFocus, int row, int col) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime((Date) value);
    Component c = super.getTableCellRendererComponent(
        table, value, isSelected, hasFocus, row, col);
    c.setEnabled(calendar.get(Calendar.DAY_OF_MONTH) % 2 == 0);
    return c;
}

Addendum: In the example above, the DateRenderer is evoked because the TableModel returns the type token Date.class, for which it has been made the default.

table.setDefaultRenderer(Date.class, new DateRenderer());

An identical appearance can be obtained by overriding prepareRenderer() as shown below, but the method is invoked for all cells, irrespective of class. As a result, prepareRenderer() is ideal for affecting entire rows, as shown in Table Row Rendering.

private final JTable table = new JTable(model) {

    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
        Component c = super.prepareRenderer(renderer, row, col);
        if (col == DATE_COL) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime((Date) model.getValueAt(row, col));
            c.setEnabled(calendar.get(Calendar.DAY_OF_MONTH) % 2 == 0);
        }
        return c;
    }
};

这篇关于JTable禁用单元格中的Checkbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 16:12