Results 1 to 11 of 11
  1. #1
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default JTable column is greyed out

    I have a jTable, in which the 4th column is composed of JCheckBoxes. I set a cellRenderer for that column to be able to display a checkedbox(for true), and an unchecked box(for false). The table displays fine, but for some reason that whole column is grayed out, and when I click on the checkbox nothing happens.

    If I double click inside the cell, I see the 'true' or 'false' text, that it allows me to edit, and then when I click off of the cell, if I changed it from true to false, the checkbox unchecks.

    Any idea why I cant check/uncheck the box manually? The whole column is greyed out.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    A renderer isn't an editor.

    Return Boolean.class for the column from getColumnClass(...) and you won't need to muck around with custom renderers and editors.

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. Don't forget to wrap the code in code tags so it retains its formatting.

    db

  3. #3
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default

    here is an example of what I am seeing

    Java Code:
    package exampleprogram;
    
    import java.awt.Component;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.TableCellRenderer;
    
    public class ExampleProgram {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
        String[] colHeaders = {"First Name","Last Name","Available"};
        String[][] myData = new String[2][3];
        myData[0][0] = "John";
        myData[0][1] = "Jackson";
        myData[0][2] = "true";
        myData[1][0] = "James";
        myData[1][1] = "Smith";
        myData[1][2] = "false";
        
        JTable myTable = new JTable(myData, colHeaders);
        myTable.getColumnModel().getColumn(2).setCellRenderer(
                new TableCellRenderer(){
                
                @Override
                public Component getTableCellRendererComponent(JTable table,
                                                               Object value,
                                                               boolean isScheduled,
                                                               boolean isFocused,
                                                               int row,
                                                               int col){
                boolean marked = Boolean.parseBoolean(value.toString());
                JCheckBox rendererComponent = new JCheckBox();
                
                if(marked){
                    rendererComponent.setSelected(true);
                }
                return rendererComponent;
                }
                });
        
        JScrollPane jsp = new JScrollPane(myTable);
         JFrame jf = new JFrame("Example");
         jf.add(jsp);
         jf.pack();
         jf.show();  
        }
    }
    Notice the last column won't allow the checkbox to change, but, if you double click the cell, you can change the true or false value, and then the cell will check/uncheck the JCheckBox once you press enter or take focus off the cell.

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

    Default

    Where do you override the getColumnClass() method to return Boolean.class?

    Why are you still using a custom renderer?

    Read the JTable API and follow the link to the Swing tutorial on "HOw to Use Tables" for a working example. I believe the example uses a custom TableModel. There is no need to do this you can still use the DefaultTableModel. Just override the getColumnClass() method of the JTable.

  5. #5
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default

    So do I apply the model to the whole table? Where would the JComboBox come into play since I removed the custom renderer

    Java Code:
        
    class MyTableModel extends AbstractTableModel {
    
        public int getColumnCount() {
            return colHeaders.length;
        }
    
        public int getRowCount() {
            return myData.length;
        }
    
        public String getColumnName(int col) {
            return colHeaders[col];
        }
    
        public Object getValueAt(int row, int col) {
            return myData[row][col];
        }
    
        public Class getColumnClass(int c) {
            return Boolean.class;
        }
    
        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if (col < 2) {
                return false;
            } else {
                return true;
            }
        }
    
        /*
         * Don't need to implement this method unless your table's
         * data can change.
         */
        public void setValueAt(Object value, int row, int col) {
            myData[row][col] = value.toString();
            fireTableCellUpdated(row, col);
        }
    }

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

    Default

    I give up. You didn't listen to what Darrly suggested and you haven't listened to what I suggested. I specifically stated you don't need a custom TableModel.

  7. #7
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default

    i did override with the getColumnClass() method to return Boolean.class, but I wasn't sure how to override that method for the Default table model.
    Last edited by fam2315; 08-30-2011 at 09:39 PM.

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

    Default

    That is not what my answer suggested.

  9. #9
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default

    I thought it said override the getColumnClass() method to return Boolean.class, I must have misunderstood.

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

    Default

    I did not suggest you override the DefaultTableModel. Read my whole suggestion.

  11. #11
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default

    does the class that I am creating the JTable inside of have to extend a jtable? not sure how to override the getColumnClass() method of the JTable.

Similar Threads

  1. JTable Column Flow
    By anilkumar_vist in forum Advanced Java
    Replies: 3
    Last Post: 10-26-2010, 06:57 AM
  2. JCheckbox in a Jtable Column
    By kedia_rohit in forum AWT / Swing
    Replies: 1
    Last Post: 04-25-2010, 12:45 PM
  3. split a jtable column
    By anilkumar_vist in forum Advanced Java
    Replies: 0
    Last Post: 12-14-2009, 10:29 AM
  4. split jtable column
    By anilkumar_vist in forum Advanced Java
    Replies: 3
    Last Post: 11-30-2009, 02:14 PM
  5. How to sort column in JTable
    By johnt in forum AWT / Swing
    Replies: 3
    Last Post: 06-14-2008, 05:48 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
  •