Search code examples
groovyjlistswingbuilder

How to limit visible rows in a JList in groovy


I'm building a small dialog. I using Groovy from a gradle build script. The dialog consists of a JList, a JTextField and a JButton.

The list is populated with names of files. There are many files so I only wanna show 5 files together with a scollbar to go thru the list. I have tried to set visibleRowCount but it still shows all rows.

new SwingBuilder().edt {
        dialog(modal: true,             // Otherwise the build will continue running before you closed the dialog
            title: 'Enter program name',// Dialog title
            alwaysOnTop: true,          // pretty much what the name says
            resizable: true,           // Don't allow the user to resize the dialog
            locationRelativeTo: null,   // Place dialog in center of the screen
            pack: true,                 // We need to pack the dialog (so it will take the size of it's children
            show: true                  // Let's show it
            ) {
                vbox { // Put everything below each other
                    label(text: "Program Name:")
                    list(id:"programName", items: progNames, visibleRowCount: 8)
                    label(text: "Start Rule Name:")
                    input = textField(id: 'ruleName', text: startRuleName)

                    button(defaultButton: true, text: 'OK', actionPerformed: {
                        testProgram   = programName.selectedValuesList
                        startRuleName = ruleName.text
                        dispose() // Close dialog
                    })
                }
            }
        }

How can I limit the number of visible rows?


Solution

  • You just need to wrap the call to list in a scrollPane node, ie:

    new groovy.swing.SwingBuilder().edt {
            dialog(modal: true,             // Otherwise the build will continue running before you closed the dialog
                title: 'Enter program name',// Dialog title
                alwaysOnTop: true,          // pretty much what the name says
                resizable: true,           // Don't allow the user to resize the dialog
                locationRelativeTo: null,   // Place dialog in center of the screen
                pack: true,                 // We need to pack the dialog (so it will take the size of it's children
                show: true                  // Let's show it
                ) {
                    vbox { // Put everything below each other
                        label(text: "Program Name:")
                        scrollPane {
                            list(id:"programName", items: progNames, visibleRowCount: 8)
                        }
                        label(text: "Start Rule Name:")
                        input = textField(id: 'ruleName', text: startRuleName)
    
                        button(defaultButton: true, text: 'OK', actionPerformed: {
                            testProgram   = programName.selectedValuesList
                            startRuleName = ruleName.text
                            dispose() // Close dialog
                        })
                    }
                }
            }