CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: Table cell editor not working

  1. #1
    Join Date
    Mar 2003

    Angry Table cell editor not working

    Hello there,
    I am new to Java programming,
    I am trying to create a check box in a table cell which the user should able manipulate when the JTable is showing, the renderer is working fine but the editor is not , the overriden method getTableCellEditorComponent never gets a call, getTableCellRendererComponent does get a call and renderer works fine.
    any ideas whats wrong here ?? or what could be wrong which makes getTableCellEditorComponent not get called ?

    my renderere and editor code is like this

    package com.itrsgroup.swing.componentmanager.dockablemanager;

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Font;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.util.EventObject;

    import javax.swing.AbstractCellEditor;
    import javax.swing.JCheckBox;
    import javax.swing.JTable;
    import javax.swing.SwingConstants;
    import javax.swing.UIManager;
    import javax.swing.table.TableCellEditor;
    import javax.swing.table.TableCellRenderer;

    * A cell renderer and a cell editor for rendering and editing the filters active state.
    public class ActiveCheckBoxRendererEditor extends AbstractCellEditor
    implements TableCellEditor, TableCellRenderer, ItemListener
    private Font font = new Font("Verdana", Font.BOLD, 11 );
    private JCheckBox editor = new JCheckBox("Inactive");
    private JCheckBox renderer = new JCheckBox("Inactive");

    private String strSelectedText;
    private String strNotSelectedText;

    private Color colourActive = new Color(51,153,51);
    private Color colourInActive = Color.RED;

    /** Constructor. */
    public ActiveCheckBoxRendererEditor()
    this( "Selected", "Not Selected" );

    * Constructor.
    * @param strSelText - the text to render when selected.
    * @param strNotSelText - the text to render when not selected.
    public ActiveCheckBoxRendererEditor(String strSelText, String strNotSelText)

    strSelectedText = strSelText;
    strNotSelectedText = strNotSelText;

    renderer = new JCheckBox( strNotSelectedText );
    renderer.setHorizontalTextPosition( SwingConstants.CENTER );
    renderer.setVerticalTextPosition( SwingConstants.TOP);
    renderer.setHorizontalAlignment( SwingConstants.CENTER );
    renderer.setVerticalAlignment( SwingConstants.CENTER );
    renderer.setFont( font );

    editor = new JCheckBox( strNotSelectedText );
    editor.setBackground( UIManager.getColor("Table.selectionBackground") );
    editor.setHorizontalTextPosition( SwingConstants.CENTER );
    editor.setVerticalTextPosition( SwingConstants.TOP );
    editor.setHorizontalAlignment( SwingConstants.CENTER );
    editor.setVerticalAlignment( SwingConstants.CENTER );
    editor.setFont( font );
    editor.addItemListener( this );

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
    return editor;

    public boolean isCellEditable(EventObject arg0)
    // TODO Auto-generated method stub
    return true;

    public Object getCellEditorValue()
    return editor.isSelected();
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    Boolean bool = (Boolean) value;

    renderer.setSelected( bool );
    renderer.setText( bool ? strSelectedText : strNotSelectedText);
    renderer.setForeground( bool ? colourActive : colourInActive );

    if( isSelected )
    renderer.setBackground( table.getSelectionBackground() );
    renderer.setBackground( table.getBackground() );

    return renderer;

    public void itemStateChanged(ItemEvent e)
    boolean isSelected = e.getStateChange() == ItemEvent.SELECTED;

    editor.setText( isSelected ? strSelectedText : strNotSelectedText);

    and this is how I install it on my JTable

    ActiveCheckBoxRendererEditor cbc = new ActiveCheckBoxRendererEditor("Active", "Inactive");

    activeColumn = this.getColumnModel().getColumn( 3 );
    activeColumn.setCellEditor( cbc);
    activeColumn.setCellRenderer( cbc );
    activeColumn.setPreferredWidth( 80 );
    activeColumn.setMaxWidth( 200 );
    activeColumn.setMinWidth( 80 );

    He is not strong and powerful, who throws people down, but he is strong who withholds himself from anger

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999

    Re: Table cell editor not working

    I doubt many will read a large chunk of unformatted code. Please use CODE tags when posting code.

    The Java Tutorial explains how to use custom table editors, and you'll see there that you don't need to provide a custom editor for check boxes, because they are automatically provided if you use Boolean for your column type. But if you still want to do it yourself, the explanation is there.

    Whenever there is a hard job to be done I assign it to a lazy man; he is sure to find an easy way of doing it...
    W. Chrysler
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Jan 2009

    Re: Table cell editor not working

    I really would like to help you but as dlorde suggested, use code tag to format it. I have gone through your code and found that it will not compile as you made many compile time mistake such as in the following code the return type is Object and you are returing boolean value .

    public Object getCellEditorValue()
    return editor.isSelected();

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)