Results 1 to 7 of 7
  1. #1
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    34
    Rep Power
    0

    Default jTable DefaultCellRenderer not working

    Hello community,
    I have a jTable with two columns and in the second Column I would like to have 2 different cell-renderer. One is String and the other one is boolean. The data is always from type Object[][].

    I have written an own renderer:
    Java Code:
    @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if(column == 2 && row > 2){
                return super.getTableCellRendererComponent(table, (boolean)value, isSelected, hasFocus, row, column);
            }
    
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); //To change body of generated methods, choose Tools | Templates.
        }
    I set the default renderer in my form:
    Java Code:
    jTable.setDefaultRenderer(Object.class, myRenderer);
    So the problem is, that my JTable will always renderer to String. I thought when i write "(boolean)" before "value" (in my IF-Statement), he returns the boolean cell renderer. But he isn`t doing that.
    How can I return a boolean-renderer in my column 2 with all rows higher than 2 ?

    Thank you for your time and effort

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: jTable DefaultCellRenderer not working

    Are you extending DefaultTableCellRenderer here? (An MCVE would help clear up any uncertainty.)

    If so, that always returns a JLabel from the getTableCellRendererComponent() method.

    You either have to create your own renderer that returns a JCheckBox, or you can try returning a JTable.BooleanRenderer instead.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    34
    Rep Power
    0

    Default Re: jTable DefaultCellRenderer not working

    Are you extending DefaultTableCellRenderer here?
    I set the default renderer to my personal class "myRenderer"
    Java Code:
    jTable.setDefaultRenderer(Object.class, myRenderer);
    If so, that always returns a JLabel from the getTableCellRendererComponent() method.
    Yes thats the problem

    or you can try returning a JTable.BooleanRenderer instead.
    And how can i accomplish this?

    Thanks

    //EDIT
    So I tested some things but it looks a little bit strange. My new code:
    Java Code:
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if(column == 2 && row > 1){
                setHorizontalAlignment(SwingConstants.CENTER);
                if(isSelected){
                    setBackground(table.getSelectionBackground());
                }else{
                    setBackground(table.getBackground());
                }
                JCheckBox c = new JCheckBox();
                Object trueFalse = table.getValueAt(row, column);
                if(trueFalse.toString().equals("true")){
                    c.setSelected(true);
                }else{
                    c.setSelected(false);
                }           
                return c;
            }
    
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); //To change body of generated methods, choose Tools | Templates.
        }
    Now I have the problem, that the cell with the jCheckboxes has a grey color, that should be white and it is not centered!
    How can I make this better ?

    Thanks
    Last edited by Paul279; 07-30-2014 at 02:54 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: jTable DefaultCellRenderer not working

    Quote Originally Posted by Paul279 View Post
    And how can i accomplish this?
    What have you tried? I gave you two solutions to attempt. I suggest you try one out, create an MCVE, and we'll go from there.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    34
    Rep Power
    0

    Default Re: jTable DefaultCellRenderer not working

    Look at my EDIT above please
    and here is the picture to my situation jTable DefaultCellRenderer not working-pic.jpg

  6. #6
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    34
    Rep Power
    0

    Default Re: jTable DefaultCellRenderer not working

    So endlich geschafft, ist sicherlich nicht die schönste Lösung aber es ist eine Lösung:

    Java Code:
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            setHorizontalAlignment(SwingConstants.CENTER);
            if(isSelected){
                setBackground(table.getSelectionBackground());
            }else{
                setBackground(table.getBackground());
            }        
            if(column == anzahlColumns && row > 1){
                JCheckBox c = new JCheckBox();
                c.setBackground(Color.WHITE);  
                c.setHorizontalAlignment(SwingConstants.CENTER);              
                
                Object trueFalse = table.getValueAt(row, column);
                if(trueFalse.toString().equals("true")){
                    c.setSelected(true);
                }else{
                    c.setSelected(false);
                }           
                return c;          
            }
    
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); //To change body of generated methods, choose Tools | Templates.
        }
    Danke für die Nerven und die Richtungsweisung

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,050
    Rep Power
    6

    Default Re: jTable DefaultCellRenderer not working

    Now this is a -serious- case of the Friday's; just utterly forgetting how to speak English!

    It happens to the best of us.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. link not working in Jtable
    By ppreeti in forum New To Java
    Replies: 7
    Last Post: 05-27-2014, 08:35 AM
  2. jTable not working
    By sathya11491 in forum NetBeans
    Replies: 0
    Last Post: 03-10-2012, 08:14 AM
  3. Cell selection not working in JTable
    By pahiker in forum AWT / Swing
    Replies: 9
    Last Post: 07-10-2010, 06:01 PM
  4. dynamic JTable not working..
    By soumajit23 in forum AWT / Swing
    Replies: 1
    Last Post: 06-25-2010, 12:23 PM
  5. Working with Jtable
    By blu_fox88 in forum AWT / Swing
    Replies: 3
    Last Post: 02-20-2010, 04:49 PM

Posting Permissions

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