groovy
  1. groovy
  2. GROOVY-4474

Enhanced SwingBuilder to add a columnModel into a table, added cellEditor support to tables and trees, implemented optional DefaultRenderer-behaviour for cellRenderers

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.7.5, 1.8-beta-2
    • Fix Version/s: 1.7.6, 1.8-beta-3
    • Component/s: Swing
    • Labels:
      None
    • Testcase included:
      yes
    • Patch Submitted:
      Yes
    • Number of attachments :
      2

      Description

      Features in this patch:

      • Definition of a columnModel in a table (important when using custom tableModel or glazedLists)
      • Value of a column is used as identifier
      • property width does not set the width, but minWidth, preferredWidth and maxWidth
        if with is a Number:
        set all three to this width to define it as fixed width
        if with is a Collection with ...:
        ... 3 values: set min, preferred, max
        ... 2 values: set min, preferred
        ... 1 value : set preferred
      • columns may contain a cellRenderer, a headerRenderer and a cellEditor element
      • cellRenderer, headerRenderer and cellEditor deliver the Component created by the default behaviour as rendercomponent if not specified
      • cellRenderer and cellEditor now work for JTree aswell

      Example:

          new SwingBuilder.edt {
            frame(id: 'test', title: 'Frame', size: [300, 300], show: true) {
              borderLayout()
              def data = [ ['Chang', 'Peng', 30], ['John', 'Doe', 17], ['Hans', 'Meyer', 25]] as Object[][]
              scrollPane {
                table(new JTable(data, ['name', 'prename', 'age'] as Object[])) {
                  columnModel {
                    column('name', modelIndex: 0, headerValue: 'Lastname', width: [100, 150, 250]) {
                      headerRenderer {
                        onRender { children[0].text = value.toUpperCase() }
                      }
                    }
                    column('prename', modelIndex: 1, headerValue: 'Prename', width: [100, 150]) {
                      cellRenderer {
                        label(foreground: java.awt.Color.RED)
                        onRender { children[0].text = value }
                      }
                    }
                    column('age', modelIndex: 2, headerValue: 'Age', width: 70) {
                      cellEditor {
                        prepareEditor { children[0].text = value*2 }
                        getEditorValue { children[0].text.toInteger() }
                      }
                    }
                  }
                }
              }
            }
          }
      

        Activity

        Hide
        Alexander Klein added a comment -

        I applied all the comments from Andres.
        I addition I added the last two comments to RendererFactory / RendererUpdateFactory aswell,
        because this Factorys had been implemented the same way.

        Show
        Alexander Klein added a comment - I applied all the comments from Andres. I addition I added the last two comments to RendererFactory / RendererUpdateFactory aswell, because this Factorys had been implemented the same way.
        Hide
        Hamlet D'Arcy added a comment -

        Alexander,

        I am ready to commit this work, thank you.

        However, I cannot get a working example running. Can you please update your code example in the JIRA description so that it runs in GroovyConsole?
        The example from the unit test runs but is not a good test because it doesn't show any data or header info, as the sample here does.

        THanks,

        Show
        Hamlet D'Arcy added a comment - Alexander, I am ready to commit this work, thank you. However, I cannot get a working example running. Can you please update your code example in the JIRA description so that it runs in GroovyConsole? The example from the unit test runs but is not a good test because it doesn't show any data or header info, as the sample here does. THanks,
        Hide
        Alexander Klein added a comment - - edited

        Hamlet,

        here are two examples, runnable in a 1.7.6 GroovyConsole, one for a JTable, one for a JTree

        JTable-Example

        import groovy.swing.*
        import javax.swing.*
        
        new SwingBuilder().edt {
          frame(id: 'test', title: 'Frame', size: [300, 300], show: true) {
            borderLayout()
            def data = [ ['Chang', 'Peng', 30], ['John', 'Doe', 17], ['Hans', 'Meyer', 25]] as Object[][]
            scrollPane {
              table(new JTable(data, ['name', 'prename', 'age'] as Object[])) {
                columnModel {
                  column('name', modelIndex: 0, headerValue: 'Lastname', width: [100, 150, 250]) {
                    headerRenderer {
                      onRender { children[0].text = value.toUpperCase() }
                    }
                  }
                  column('prename', modelIndex: 1, headerValue: 'Prename', width: [100, 150]) {
                    cellRenderer {
                      label(foreground: java.awt.Color.RED)
                      onRender { children[0].text = value }
                    }
                  }
                  column('age', modelIndex: 2, headerValue: 'Age', width: 70) {
                    cellEditor {
                      prepareEditor { children[0].text = value*2 }
                      editorValue { children[0].text.toInteger() }
                    }
                  }
                }
              }
            }
          }
        }
        

        JTree-Example

        import groovy.swing.*
        import javax.swing.*
        
        new SwingBuilder().edt {
          frame(id: 'test', title: 'Frame', size: [300, 300], show: true) {
            borderLayout()
            scrollPane {
                tree(rootVisible: false, editable: true) {
                  cellRenderer {
                    onRender {
                      children[0].text = value.userObject.toUpperCase()
                    }
                  }
                  cellEditor {
                    textField()
                    prepareEditor {
                        children[0].text = value.userObject
                    }
                    editorValue {
                        value.userObject = children[0].text
                    }
                  }
                }
              }
           }  
        }
        
        Show
        Alexander Klein added a comment - - edited Hamlet, here are two examples, runnable in a 1.7.6 GroovyConsole, one for a JTable, one for a JTree JTable-Example import groovy.swing.* import javax.swing.* new SwingBuilder().edt { frame(id: 'test', title: 'Frame', size: [300, 300], show: true ) { borderLayout() def data = [ ['Chang', 'Peng', 30], ['John', 'Doe', 17], ['Hans', 'Meyer', 25]] as Object [][] scrollPane { table( new JTable(data, ['name', 'prename', 'age'] as Object [])) { columnModel { column('name', modelIndex: 0, headerValue: 'Lastname', width: [100, 150, 250]) { headerRenderer { onRender { children[0].text = value.toUpperCase() } } } column('prename', modelIndex: 1, headerValue: 'Prename', width: [100, 150]) { cellRenderer { label(foreground: java.awt.Color.RED) onRender { children[0].text = value } } } column('age', modelIndex: 2, headerValue: 'Age', width: 70) { cellEditor { prepareEditor { children[0].text = value*2 } editorValue { children[0].text.toInteger() } } } } } } } } JTree-Example import groovy.swing.* import javax.swing.* new SwingBuilder().edt { frame(id: 'test', title: 'Frame', size: [300, 300], show: true ) { borderLayout() scrollPane { tree(rootVisible: false , editable: true ) { cellRenderer { onRender { children[0].text = value.userObject.toUpperCase() } } cellEditor { textField() prepareEditor { children[0].text = value.userObject } editorValue { value.userObject = children[0].text } } } } } }
        Hide
        Hamlet D'Arcy added a comment -

        fixed in head, rolling back to 1.7.6

        Show
        Hamlet D'Arcy added a comment - fixed in head, rolling back to 1.7.6
        Hide
        Hamlet D'Arcy added a comment -

        rolled back to branch

        Show
        Hamlet D'Arcy added a comment - rolled back to branch

          People

          • Assignee:
            Hamlet D'Arcy
            Reporter:
            Alexander Klein
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: