如何使特定的jTable布尔列可编辑

问题描述:

我有一个返回数据库填充的DefaultTableModel的方法.我想做的是通过向返回的DefaultTableModel实例添加新的布尔列,为返回的每个记录添加布尔复选框.用户应该只能单击/取消单击这些复选框(应允许多选)以操纵我在GUI中拥有的某些地图对象.其他列应不可编辑.关于如何实现这一目标的任何想法?到目前为止,我已经讲到以下几点,我对TableCellRenderer进行了如下扩展

I have a method that returns a DefaultTableModel populated by a database. What I wanted to do is add boolean check boxes to each records returned by adding a new boolean column to the returned DefaultTableModel instance. The user should be able to only click/unclick these checkboxes (Multiple selection should be allowed) to manipulate some map objects I have in the GUI. Other columns should be un-editable. Any ideas on how to achieve this? So far I have gone up to the following point, I have extended TableCellRenderer as follows

public class UGIS_BooleanTableCellRenderer extends JCheckBox implements TableCellRenderer {

          public UGIS_BooleanTableCellRenderer() {
            setHorizontalAlignment(JLabel.CENTER);
          }

      @Override
      public Component getTableCellRendererComponent(JTable table, Object value,
          boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
          setForeground(table.getSelectionForeground());
          super.setBackground(table.getSelectionBackground());
          setBackground(table.getSelectionBackground());
        } else {
          setForeground(table.getForeground());
          setBackground(table.getBackground());
        }
        setSelected((value != null && ((Boolean) value).booleanValue()));
        return this;
      }       
}

我也可以重写isCellEditable方法.

I can override isCellEditable method also.

DefaultTableModel dm = new DefaultTableModel() {
                @Override
                public boolean isCellEditable(int row, int column) {
                    return column == 3;
                }
            };

但是如何使方法返回的DefaultTableModel与我重写的dm实例兼容?任何帮助,将不胜感激.

But how do I make the DefaultTableModel returned by the method to be compatible with my overrided dm instance? Any help on this would be greatly appreciated.

您可以使用CheckBox列而无需编写自定义渲染器/编辑器,而只需覆盖TableModelgetColumnClass()方法即可.这是带有CheckBox列的简单示例:

You can use CheckBox column without writing custom renderer/editor, just overriding getColumnClass() method of TableModel. Here is simple example for your with CheckBox column:

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Example extends JFrame {

    public static void main(String... s){
        new Example();
    }

    public Example(){
        DefaultTableModel model = new DefaultTableModel(4,4) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return column == 3;
            }

            @Override
            public Class<?> getColumnClass(int columnIndex) {
                if(columnIndex == 3){
                    return Boolean.class;
                }
                return super.getColumnClass(columnIndex);
            }
        };

        JTable t = new JTable(model);
        add(new JScrollPane(t));

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
}