How to make a specific jTable boolean column editable

二次信任 提交于 2019-12-24 15:05:15

问题


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;
      }       
}

I can override isCellEditable method also.

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

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.


回答1:


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);
    }
}


来源:https://stackoverflow.com/questions/21242268/how-to-make-a-specific-jtable-boolean-column-editable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!