Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree2Likes

Thread: Strange behaviour of CellEditor jCheckbox

  1. #1
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    45
    Rep Power
    0

    Default Strange behaviour of CellEditor jCheckbox

    Hello community,

    I have a JTable with several columns. I have set an Checkbox-Renderer on one column.
    Java Code:
    MyCellRenderer cellRenderer = 
    new MyCellRenderer(9);
    myJTable.setDefaultRenderer(Object.class, cellRenderer);
    I have made a custom Renderer because I wanted to have all rows in one column with the behaviour of a jCheckbox, but the first two rows.
    It is working fine (below you see MyCellRenderer Class.
    Java Code:
    public class MyCellRendererextends DefaultTableCellRenderer{
    
        private final int anzahlColumns;
        
        public MyCellRenderer(int anzahlColumns) {
            //+2 weil plus "Gesamt" und "Einheitlich"
            this.anzahlColumns = anzahlColumns +2;
        }
        
    
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, 
                boolean isSelected, boolean hasFocus, int row, int column) {
            //Ausrichtung der Columns
            if(column > 0){
                setHorizontalAlignment(SwingConstants.CENTER);
            }else{
                setHorizontalAlignment(SwingConstants.LEFT);
            }
            
            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 == null){
                    String TEST = "jetzt ist null";
                }
                if(trueFalse != null && 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 wanted to add an CellEditor to the column.
    Java Code:
    myJTable.getColumn("Einheitlich").setCellEditor(new DefaultCellEditor(new JCheckBox()));
    But now here is that strange behaviour. When I first click on one column, on which i set the celleditor, the celleditor (jCheckbox) is working fine. After the first one, it is not working again anymore. Then I have to double click on the cell which brings me then the value behind the checkbox: false or true as text.

    So does anybody know why the celleditor is first working and then not again anymore?

    Thanks
    Greetings
    Paul

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Strange behaviour of CellEditor jCheckbox

    Well one thing I spot is that your getTableCellRendererComponent() returns a completely new JCheckBox each time it is called. I'd suspect you want to return the exact same component for the same row and column each time it is called.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Strange behaviour of CellEditor jCheckbox

    No need to reinvent the wheel. Populate your JTable with boolean/Boolean values and return Boolean.class from getColumnClass(...).

    Have you gone through How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) ?

    db

    (I'm moving the thread to the appropriate section. In future, please post Swing related topics in the section I'm moving this to.)
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    45
    Rep Power
    0

    Default Re: Strange behaviour of CellEditor jCheckbox

    Thanks for reply,
    how can i change my code, that it returns the same component?
    I am not familiar with Renderer and Editor.

    Additionally i can say, the renderer is not guilty, because I disabled the renderer, so that I only have a normal jTable with an editor set. The same behaviour.
    The first time it has the behaviour of a jCheckbox and than I have to double click

    //EDIT
    @DarrylBurke
    I cannot use the standard Renderer with getColumn Class Boolean. Because then my completely column would be a boolean renderer. But in my renderer the first two rows of this column are normal text renderer. I dont want to have a boolean checkbox in the first two rows
    Last edited by Paul279; 10-03-2014 at 11:35 AM.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Strange behaviour of CellEditor jCheckbox

    I wonder what happens when you do follow Darryl's recommendation, so that you have a checkbox in the entire column. do those checkboxes at least behave as they should?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Strange behaviour of CellEditor jCheckbox

    No it does not work. Because then I get a ClassCastExcetpion because in this first two rows there are null values.
    They should not have a value, they only should be empty

    I think the Problem has to be here:
    Java Code:
        @Override
        public void tableChanged(TableModelEvent e) {
            int row = e.getFirstRow();
            int column = e.getColumn();
            if(row > 1 && column == ANZAHL_COLUMNS-1){
    
                controller.getModel().setRunde_Statistik(this.runde);
                
                if(this.controllerH == null)
                    this.controllerH = controller.getControllerHauptform();
                
                Statistik_Faktoren_TableModel faktorenTableModel = 
                        controller.getModel().getDtmStatistik_Faktoren();
                dataVectorFaktoren = new Object[ANZAHL_ROWS][ANZAHL_COLUMNS];
                int rowCounter = 0;
                for(Object[] temp : dataVectorFaktoren){
                    for(int i=0; i<ANZAHL_COLUMNS-1; i++){
                        temp[i] = faktorenTableModel.getValueAt(rowCounter, i);
                    }
                    if(rowCounter > 1)
                        temp[ANZAHL_COLUMNS-1] = "false";
                    rowCounter++;
                }
                dataVectorFaktoren[row][column] = "true";
                
                controller.getModel().getDtmStatistik_Faktoren().setDataVector(dataVectorFaktoren, 
                    this.COL_IDENTIFIER_STATISTIK);    
                
                controller.getModel().setWelchejRadioButtonAngeklickt(row+1);
                controller.getModel().runStatistikverwaltung("", false);  
            }
        }
    Because the first time it is working and AFTER the table changed it is not working anymore.
    Maybe there is a Problem with saving the boolean values? true and false, because these are saved as text values. I have tested it with (dataVectorFaktoren[row][column] = true;) and false only but this is also not working :-(
    Last edited by Paul279; 10-03-2014 at 12:15 PM.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Strange behaviour of CellEditor jCheckbox

    Well I think you're slowly starting to prove the problem is somewhere you're not actually looking. That is true more often than not: the problem you see is not the actual root of the problem but only a "problem as a result of a problem".

    I think at this point if you want assistance from especially Darryl (who knows a lot more about Swing than me), I'd create a small and complete example program which demonstrates the problem. Something other people can run and play with on their own machine.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Strange behaviour of CellEditor jCheckbox

    Quote Originally Posted by Paul279 View Post
    No it does not work. Because then I get a ClassCastExcetpion because in this first two rows there are null values.
    They should not have a value, they only should be empty.
    Why? Without knowing the details, it appears that you may be using a JTable in a way that it was not intended to be used.

    Quote Originally Posted by Paul279 View Post
    Maybe there is a Problem with saving the boolean values? true and false, because these are saved as text values.
    You seem to have missed this:
    Quote Originally Posted by DarrylBurke View Post
    Populate your JTable with boolean/Boolean values
    Do you have a problem with doing that?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Strange behaviour of CellEditor jCheckbox

    Quote Originally Posted by gimbal2 View Post
    I'd create a small and complete example program which demonstrates the problem. Something other people can run and play with on their own machine.
    Also known as a SSCCE (Short, Self Contained, Correct (Compilable), Example).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Strange behaviour of CellEditor jCheckbox

    It sounds to me like the OP wants a renderer for a specific type of cell. Based on my
    quick perusal, the tutorials tell you know to do that.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Strange behaviour of CellEditor jCheckbox

    Quote Originally Posted by jim829 View Post
    Based on my quick perusal, the tutorials tell you how to do that.
    Yes, entire columns. Not necessarily specific cells in specific rows (and then not in other rows). At least I don't see an example program which demonstrates how to do that.

    Assuming you're talking about the page DarrylBirdman linked to.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    45
    Rep Power
    0

    Default Re: Strange behaviour of CellEditor jCheckbox

    Okay I have found the problem of my problem, but don`t have a solution yet.
    Every time I do
    myTable.setDataVector(...) my celleditor is not working anymore.

    So obviously the jTable looses the editor every time I set a new dataVector. How can i say my jTable that it should not loose the editor, or do I have to add every time new the cell editor?

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Strange behaviour of CellEditor jCheckbox

    Quote Originally Posted by gimbal2 View Post
    Yes, entire columns. Not necessarily specific cells in specific rows
    Well, getTableCellRendererComponent(...) does have parameters for the row and column, so it wouldn't be difficult ...

    Quote Originally Posted by gimbal2 View Post
    I don't see an example program which demonstrates how to do that.
    Because that isn't the way a JTable is expected to be used.

    db
    gimbal2 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Strange behaviour of CellEditor jCheckbox

    They don't give an example of that, they just explain it. Here is a quote from the section.

    "To specify a cell-specific renderer, you need to define a JTable subclass that overrides the getCellRenderer method. For example, the following code makes the first cell in the first column of the table use a custom renderer:"

    Java Code:
    TableCellRenderer weirdRenderer = new WeirdRenderer();
    table = new JTable(...) {
        public TableCellRenderer getCellRenderer(int row, int column) {
            if ((row == 0) && (column == 0)) {
                return weirdRenderer;
            }
            // else...
            return super.getCellRenderer(row, column);
        }
    };
    Regards,
    Jim
    gimbal2 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    45
    Rep Power
    0

    Default Re: Strange behaviour of CellEditor jCheckbox

    Quote Originally Posted by DarrylBurke View Post
    Well, getTableCellRendererComponent(...) does have parameters for the row and column, so it wouldn't be difficult ...
    For you as higher skilled not, but for me already.

    Quote Originally Posted by DarrylBurke View Post
    Because that isn't the way a JTable is expected to be used.
    Who says how I should use a jTable. I can do it like I need it to have.

    So, but the problem is found. See post number 12

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Strange behaviour of CellEditor jCheckbox

    Quote Originally Posted by Paul279 View Post
    Who says how I should use a jTable. I can do it like I need it to have.
    No need to get testy. Darryl's correct in that it is not normally meant to be used that way. Most
    columns represent a particular type of info and would use a standard rendererer appropriate for that column.
    After all, that's why the example refers to it as "weirdRenderer."

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    45
    Rep Power
    0

    Default Re: Strange behaviour of CellEditor jCheckbox

    Quote Originally Posted by jim829 View Post
    No need to get testy.
    I am very sorry, I don`t wanted to be testy, respectless or something in that way. I meant this as a normal sentence.

    So we are out of topic. Can we maybe go back to the origin topic? Thank you

  18. #18
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Strange behaviour of CellEditor jCheckbox

    Quote Originally Posted by DarrylBurke View Post
    Well, getTableCellRendererComponent(...) does have parameters for the row and column, so it wouldn't be difficult ...
    Perhaps combining the code in this article from our other local Swing hero (that sounds dirtier than intended):

    Table Row Rendering Java Tips Weblog

    with the WeirdRenderer code that Jim spotted in the tutorial you can go a long way...

    Me... I'd just use two isolated JTables to be honest. If data has to be represented differently, then that data probably needs to be treated as something entirely different and not thrown together in the same component.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  19. #19
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    45
    Rep Power
    0

    Default Re: Strange behaviour of CellEditor jCheckbox

    My renderer is working fine. The only problem that still exists is that the jTable looses the cell editor when I put new values in the jTable. (setDataVector(...))

    How can I solve this problem?

  20. #20
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Strange behaviour of CellEditor jCheckbox

    Quote Originally Posted by Paul279 View Post
    My renderer is working fine. The only problem that still exists is that the jTable looses the cell editor when I put new values in the jTable. (setDataVector(...))
    First, JTable doesn't have a setDataVector(...) method.

    If you're referring to the method of DefaultTableModel, calling setDataVector(...) results in a call to fireTableStructureChanged() and that resets all custom renderers and editors (and some more).

    Quote Originally Posted by Paul279 View Post
    How can I solve this problem?
    Don't use setTableVector(...). If you're not changing the column structure, you shouldn't be using it anyways.

    Instead, remove and add rows.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Page 1 of 2 12 LastLast

Similar Threads

  1. very Strange behaviour !!
    By selimzz in forum Advanced Java
    Replies: 12
    Last Post: 01-06-2013, 11:35 AM
  2. Strange JFrame behaviour
    By javaExprt in forum New To Java
    Replies: 5
    Last Post: 12-24-2012, 03:20 AM
  3. Strange behaviour
    By imadabh in forum Threads and Synchronization
    Replies: 1
    Last Post: 05-11-2011, 03:31 PM
  4. Strange JVM behaviour
    By pjpr in forum Advanced Java
    Replies: 13
    Last Post: 01-03-2011, 07:39 PM
  5. Strange behaviour in swing
    By cbalu in forum AWT / Swing
    Replies: 1
    Last Post: 05-23-2008, 09:23 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
  •