Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By camickr

Thread: Checkbox editor for JTable column

  1. #1
    ShamPhadtale is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Checkbox editor for JTable column

    I have written checkbox editor for JTable column. I am able to see checkbox and can select or deselect it. But if I select row 1 checkbox and then if click on row 2 checkbox, the selected checkbox of row 1 disappers, it becomes false again. That is I can select only one checkbox. I am not getting what i written wrong in editor. Or what is my propable mistake. Please someone can help me.
    Java Code:
    import java.awt.Color;   
    import java.awt.Component;   
    import java.awt.event.ItemEvent;   
    import java.awt.event.ItemListener;   
    import java.util.EventObject;   
      
    import javax.swing.DefaultCellEditor;   
    import javax.swing.JCheckBox;   
    import javax.swing.JTable;   
    import javax.swing.SwingConstants;   
    import javax.swing.event.CellEditorListener;   
      
    /*  
     * can extends AbstractCellEditor  
     */  
      
    public class GenTableCellEditor extends DefaultCellEditor implements ItemListener {   
           
        protected static JCheckBox checkBox = new JCheckBox();   
      
        public GenTableCellEditor() {   
            super(checkBox);   
            checkBox.setHorizontalAlignment(SwingConstants.CENTER);   
            checkBox.setBackground(Color.white);   
        }   
      
        public Component getTableCellEditorComponent(JTable table, Object value,   
                boolean isSelected, int row, int column) {   
            if (value == null)   
                return checkBox;   
            checkBox.addItemListener(this);   
            if (((Boolean) value).booleanValue())   
                checkBox.setSelected(true);   
            else  
                checkBox.setSelected(false);   
      
            return checkBox;   
        }   
      
        public Object getCellEditorValue() {   
            if(checkBox.isSelected() == true)   
                return new Boolean(true);   
            else    
                return new Boolean(false);   
        }   
      
        @Override  
        public void addCellEditorListener(CellEditorListener l) {   
            // TODO Auto-generated method stub   
        }   
      
        @Override  
        public void cancelCellEditing() {   
            // TODO Auto-generated method stub   
      
        }   
      
        @Override  
        public boolean isCellEditable(EventObject anEvent) {   
            // TODO Auto-generated method stub   
            return true;   
        }   
      
        @Override  
        public void removeCellEditorListener(CellEditorListener l) {   
            // TODO Auto-generated method stub   
      
        }   
      
        @Override  
        public boolean shouldSelectCell(EventObject anEvent) {   
            // TODO Auto-generated method stub   
            return true;   
        }   
      
        @Override  
        public boolean stopCellEditing() {   
            // TODO Auto-generated method stub   
            return true;   
        }   
      
        @Override  
        public void itemStateChanged(ItemEvent e) {   
            // TODO Auto-generated method stub   
            System.out.println("Firing!");   
        }   
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Checkbox editor for JTable column

    Why are you even trying to create a checkbox editor? Why not just override the model and have it's getColumnClass() return Boolean.class for the column that needs checkboxes?

  3. #3
    ShamPhadtale is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Checkbox editor for JTable column

    I tried it. But it shows only checkboxes not allowing seletion to them. I am not able to check it. I also override isCellEditable() method but still not able to check the checkbox.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Checkbox editor for JTable column

    Quote Originally Posted by ShamPhadtale View Post
    I tried it. But it shows only checkboxes not allowing seletion to them. I am not able to check it. I also override isCellEditable() method but still not able to check the checkbox.
    They should be selectable unless the cell is not editable. I think you should post a small compilable program showing this attempt above, an SSCCE.

  5. #5
    ShamPhadtale is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Checkbox editor for JTable column

    Ok. actualy I have lot of legacy code. So I will try to write a sample code within an hour.

  6. #6
    ShamPhadtale is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Checkbox editor for JTable column

    Sorry to say but writting sample program will take too much time. I am just pasting table model which I am using. Please give me hint if possible from below model. I will definitely paste whole sample program SSCCE tomorrow.

    Java Code:
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Vector;
    
    import javax.swing.table.AbstractTableModel;
    
    public class TableModel1 extends AbstractTableModel {
    	
    	private ColumnProperties[] colprops;    // it contain data type column class name and some more colum properties
    	private String columnNames[];
    	private HashMap _data = new HashMap();
    	protected int columnsCount = 0;
    	
    	public TableModel1(ColumnProperties[] colProps) {
    		this.colprops = colProps;
    		this.columnsCount = colProps.length;
    		setColumnProperties();
    	}
    
    	public void setColumnProperties() {
    		columnNames = new String[colprops.length];
    		for (int i = 0; i < columnNames.length; i++) {
    			columnNames[i] = colprops[i].getColumnName();
    		}
    	}
    	
    	public boolean isCellEditable(int rowIndex, int columnIndex) {
    		if(getColumnName(columnIndex).equalsIgnoreCase("Mask")){
    			return true;
    		}
    		return false;
    	}
    	
    	public void clear() {
    		_data.clear();
    		fireTableDataChanged();
    	}
    
    	public int getRowCount() {
    		if (_data.size() == 0)
    			return 0;
    		else
    			return  _data.size();
    	}
    	public int getColumnCount() {
    		return colprops.length;
    	}
    
    	public Object getValueAt(int row, int col) {
    		List myList = new ArrayList(_data.keySet());
    		Vector finRec = (Vector) _data.get(myList.get(row));
    		return finRec.elementAt(col);
    	}
    	
    	public String getColumnName(int col) {
    		return colprops[col].getColumnName();
    	}
    	@Override
    	public Class getColumnClass(int column) {
    		if (colprops[column] != null) {
    			return colprops[column].getClassPARM();
    		}
    		return String.class;
    	}
    
    	@SuppressWarnings("unchecked")
    	public void setValueAt(Object value, int row, int col) {
    		try {
    			Vector<Object> a = (Vector) _data.get(row);
    			a.setElementAt(value, col);
    			// _data[row][col] = value;
    			fireTableCellUpdated(row, col);
    		} catch (Exception e) {
    		}
    	}
    
    	public ColumnProperties[] getColprops() {
    		return colprops;
    	}
    
    	public void setColprops(ColumnProperties[] colprops) {
    		this.colprops = colprops;
    	}
    
    	public HashMap getData() {
    		return _data;
    	}
    
    	public void remove(Object key) {
    		_data.remove(key);
    		fireTableDataChanged();
    	}
    
    	public void setData(Object key, Vector data) {
    		this._data.put(key, data);
    		fireTableDataChanged();
    	}
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Checkbox editor for JTable column

    Quote Originally Posted by ShamPhadtale View Post
    Sorry to say but writting sample program will take too much time. I am just pasting table model which I am using. Please give me hint if possible from below model. I will definitely paste whole sample program SSCCE tomorrow.
    If you still need our help tomorrow, I'll wait for your SSCCE. Otherwise and again, fix your getColumnClass and isCellEditable methods above.

    Best of luck!

  8. #8
    ShamPhadtale is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Checkbox editor for JTable column

    I am still facing problem. One thing I noticed that itemStateChanged method was getting call again and again. So I moved checkBox.addItemListener(this); line from getTableCellEditorComponent() to Constructor. Now itemStateChanged method gets call only twice. Still not able to select multiple checkbox, only one checkbox is get allowed

    I checked by debugging getColumnClass and isCellEditable methods are working correctly, they are returning correct values on cell selection. So i think I need to add some code to other methods like shouldSelectCell, stopCellEditing etc. But I am not aware what exactly they do. I will find out what exactly i need to do.
    I also tried of using -
    Java Code:
    					columnModel.getColumn(i).setCellEditor(genTable.getDefaultEditor(Boolean.class));
    					columnModel.getColumn(i).setCellRenderer(genTable.getDefaultRenderer(Boolean.class));
    but it also not worked.

  9. #9
    ShamPhadtale is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Checkbox editor for JTable column

    I am not getting why itemStateChanged() and stopCellEditing() methods are getting call twice?

  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Checkbox editor for JTable column

    Java Code:
    columnModel.getColumn(i).setCellEditor(genTable.getDefaultEditor(Boolean.class)); 
    columnModel.getColumn(i).setCellRenderer(genTable.getDefaultRenderer(Boolean.class));
    There is no need to create a custom editor. JTable provides a default checkbox editor.

    I am not getting why you haven't posted a SSCCE yet? We can't help you otherwise. I have no idea why you are even using an ItemListener.
    Fubarable likes this.

  11. #11
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Checkbox editor for JTable column


  12. #12
    ShamPhadtale is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Checkbox editor for JTable column

    To get help early I had posted same question on Javranch as well. From next time I will take care of letting know members that I have posted questions on different forums. I am appologize for my mistake.
    I am trying to use ItemListener, since after selecting checkbox I need to do some changes to server side and I was planning to call these changes from ItemStateChange method of ItemListener. Is it incorrect to do such thing? What should be right way to capture state change event of CheckBox in JTable column?

  13. #13
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Checkbox editor for JTable column

    You would use a TableModelListener.

Similar Threads

  1. Place a working checkbox in a JTable header.
    By Vorrin in forum AWT / Swing
    Replies: 1
    Last Post: 03-28-2011, 01:39 PM
  2. Disable a checkbox in jtable
    By pink123 in forum AWT / Swing
    Replies: 8
    Last Post: 03-24-2011, 06:29 PM
  3. Adding Checkbox to Jtable through vectors
    By indra00 in forum AWT / Swing
    Replies: 2
    Last Post: 12-08-2010, 08:45 AM
  4. CheckBox in JTable
    By nikosa in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2009, 05:01 AM
  5. checkBox in Jtable renderining problem
    By pothraj in forum AWT / Swing
    Replies: 1
    Last Post: 12-18-2008, 11:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •