Results 1 to 5 of 5
  1. #1
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default add a check box to only one cell in a JTable

    hi,i'm trying to add a check box to one cell in my table.i know it s possible to add check boxes to a column(every row in that column will be boolean).
    i just want to add check box to only one cell in a selected column.

    i was wondering if i can overide
    public Class getColumnClass(int columnIndex) to do this?

    i was able to make some selected cells unedtable by overiding isCellEditable method;but it has input parameters for both rows and columns.therefore it was easy to dealt.

    so can somone tell me how to solve my question?
    thanks

  2. #2
    SaSho's Avatar
    SaSho is offline Member
    Join Date
    Dec 2008
    Location
    Ukraine, Dnepropetrovsk
    Posts
    5
    Rep Power
    0

    Default

    Hello
    I think the best way is custom implementation of TableCellRenderer and, if it's necessary, TableCellEditor interfaces.
    I could write a little exapmle

  3. #3
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default

    I'm not familiar with these renderers and editors in tables.i'm completely new to java.I'm glad f you can write example (in simple context)
    thank you.

  4. #4
    SaSho's Avatar
    SaSho is offline Member
    Join Date
    Dec 2008
    Location
    Ukraine, Dnepropetrovsk
    Posts
    5
    Rep Power
    0

    Default

    I wrote a little example without editors

    TableModel contains two columns for each row - boolean and string:
    Java Code:
    package bai.test.swing;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Vector;
    import javax.swing.table.DefaultTableModel;
    
    /**
     * TableModel with two columns: Boolean (CheckBox) and String
     *
     * @author Aleksandr
     */
    class BooleanTableModel extends DefaultTableModel {
    
        private static final int LOGICAL_COLUMN = 0;
        private static final int DESCRIPTION_COLUMN = 1;
        private static final int COLUMNS_COUNT = 2;
    
        /**
         * Model data.
         */
        private List<RowItem> data = new Vector<RowItem>();
    
        public BooleanTableModel(RowItem ... data){
            if ( data != null )
                this.data.addAll(Arrays.asList(data));
        }
    
        /**
         * Returns number of lines
         *
         * @return number of rows
         */
        @Override
        public int getRowCount() {
            return data != null? data.size() : 0;
        }
    
        /**
         * Returns number of columns
         *
         * @return number of columns
         */
        @Override
        public int getColumnCount() {
            return COLUMNS_COUNT;
        }
    
        /**
         * Forbids editing og cells
         *
         * @param row Row index
         * @param column Column index
         * @return <tt>false</tt> always
         */
        @Override
        public boolean isCellEditable(int row, int column){
            return false;
        }
    
        /**
         * Returns class of values in column <tt>columnIndex</tt>
         * 
         * @param columnIndex Number of column
         * @return Class of values in column <tt>columnIndex</tt>
         */
        @Override
        public Class<?> getColumnClass(int columnIndex) {
            switch (columnIndex) {
                case LOGICAL_COLUMN:
                    return Boolean.class;
                case DESCRIPTION_COLUMN:
                    return String.class;
    
            }
            return null;
        }
    
        /**
         * Returns value in row <tt>rowIndex</tt> and column <tt>columnIndex</tt>
         *
         * @param rowIndex
         * @param columnIndex
         * @return
         */
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            RowItem item = data.get(rowIndex);
            if ( item == null )
                return null;
            switch (columnIndex) {
                case LOGICAL_COLUMN:
                    return item.getLogical();
                case DESCRIPTION_COLUMN:
                    return item.getDescription();
    
            }
            return null;
        }
    
    
        /**
         * Data class for incapsulation data
         */
        protected static class RowItem {
    
            private Boolean logical;
            private String description;
    
            /**
             * Constructs the class with the given values
             *
             * @param logical Logical value
             * @param description Description value
             */
            RowItem(Boolean logical, String description){
                this.logical = logical;
                this.description = description;
            }
    
            public String getDescription() {
                return description;
            }
    
            public void setDescription(String description) {
                this.description = description;
            }
    
            public Boolean getLogical() {
                return logical;
            }
    
            public void setLogical(Boolean logical) {
                this.logical = logical;
            }
    
        }
    
    }
    The TableCellRenderer implementation.
    The general idea is: If value of cell is not-null Boolean object - method getTableCellRendererComponent() returns renderer based on CheckBox, else - returns DefaultTableCellRenderer
    Java Code:
    package bai.test.swing;
    
    import java.awt.Component;
    import javax.swing.JCheckBox;
    import javax.swing.JLabel;
    import javax.swing.JTable;
    import javax.swing.UIManager;
    import javax.swing.border.Border;
    import javax.swing.border.EmptyBorder;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.TableCellRenderer;
    
    /**
     * Imlementation of TableCellRenderer.
     * <br> Alone method returns renderer for boolean if <tt>value</tt> is not-null
     * instance of Boolean class, or default table cell renderer in the another case
     *
     * @author Aleksandr
     */
    public class BooleanCellRenderer extends DefaultTableCellRenderer {
    
        /**
         * Renderer for true not-null Boolean
         */
        private static final BooleanRenderer booleanRenderer = new BooleanRenderer();
    
        /**
         * Returns the table cell renderer. If <tt>value</tt> is not-null instance of
         * Boolean class, returns renderer for boolean. Else returns default table
         * cell renderer
         *
         * @param table  the <code>JTable</code>
         * @param value  the value to assign to the cell at
         *			<code>[row, column]</code>
         * @param isSelected true if cell is selected
         * @param hasFocus true if cell has focus
         * @param row  the row of the cell to render
         * @param column the column of the cell to render
         * @return the default table cell renderer
         * @see javax.swing.JComponent#isPaintingForPrint()
         */
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                              boolean isSelected, boolean hasFocus, int row, int column) {
    
            // if value is null - cell mustn't contain any CheckBoxes
            if ( value == null )
                return super.getTableCellRendererComponent(table,
                        "", isSelected, hasFocus, row, column);
    
            return booleanRenderer.getTableCellRendererComponent(table,
                value, isSelected, hasFocus, row, column);
        }
    
    
        /**
         * True Boolean renderer
         */
        private static class BooleanRenderer extends JCheckBox implements
                TableCellRenderer
        {
            private static final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
    
    	public BooleanRenderer() {
    	    super();
    	    setHorizontalAlignment(JLabel.CENTER);
                setBorderPainted(true);
    	}
    
            public Component getTableCellRendererComponent(JTable table, Object value,
    						       boolean isSelected, boolean hasFocus, int row, int column) {
    	    if (isSelected) {
    	        setForeground(table.getSelectionForeground());
    	        super.setBackground(table.getSelectionBackground());
    	    }
    	    else {
    	        setForeground(table.getForeground());
    	        setBackground(table.getBackground());
    	    }
                setSelected((value != null && ((Boolean)value).booleanValue()));
    
                if (hasFocus) {
                    setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
                } else {
                    setBorder(noFocusBorder);
                }
    
                return this;
            }
        }
    
    }
    And finally, example dialog:
    Java Code:
    package bai.test.swing;
    
    import javax.swing.JDialog;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    
    /**
     * Example dialog
     *
     * @author Aleksandr
     */
    public class TestTableDialog extends JDialog {
    
    
        public TestTableDialog(){
    
            BooleanTableModel tableModel = new BooleanTableModel(
                    new BooleanTableModel.RowItem(true, "The first true"),
                    new BooleanTableModel.RowItem(null, "<< must be empty"),
                    new BooleanTableModel.RowItem(true, "The second true"),
                    new BooleanTableModel.RowItem(null, "<< the other empty"),
                    new BooleanTableModel.RowItem(null, "<< the last empty"),
                    new BooleanTableModel.RowItem(false, "Finally false")
            );
    
            JTable table = new JTable();
            table.setDefaultRenderer(Boolean.class, new BooleanCellRenderer());
            table.setModel(tableModel);
    
            JScrollPane tableScroll = new JScrollPane();
            tableScroll.getViewport().setView(table);
    
            this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            this.getContentPane().add(tableScroll);
            this.add(tableScroll);
            this.setSize(320, 240);
            this.setLocationRelativeTo(null); // for center location
            this.setVisible(true);
    
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            new TestTableDialog();
        }
    
    }
    Pay attention to line:
    Java Code:
    table.setDefaultRenderer(Boolean.class, new BooleanCellRenderer());
    It sets our BooleanCellRenderer to be used if column class is Boolean.

    I hope this example will help you. :)

    P.S. I'm sorry for my bad English

  5. #5
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

Similar Threads

  1. Color cell in JTable
    By ippacciani in forum AWT / Swing
    Replies: 3
    Last Post: 03-25-2009, 12:53 PM
  2. Replies: 0
    Last Post: 11-14-2008, 05:14 AM
  3. set different font for each cell in JTable
    By success21061985 in forum AWT / Swing
    Replies: 3
    Last Post: 09-10-2008, 03:06 PM
  4. jTable Cell editing
    By mahaling_m in forum AWT / Swing
    Replies: 0
    Last Post: 06-23-2008, 09:17 AM
  5. Jframe In Jtable cell
    By Clarion in forum AWT / Swing
    Replies: 4
    Last Post: 06-23-2008, 05:42 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
  •