如何在 groovy 中限制 JList 中的可见行

How to limit visible rows in a JList in groovy

我正在构建一个小对话框。 我使用 gradle 构建脚本中的 Groovy。 该对话框由一个 JList、一个 JTextField 和一个 JButton 组成。

列表中填充了文件名。有很多文件,所以我只想显示 5 个文件和一个 scollbar 来浏览列表。 我尝试设置 visibleRowCount 但它仍然显示所有行。

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
                    })
                }
            }
        }

如何限制可见行数?

您只需要将对 list 的调用包装在 scrollPane 节点中,即:

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
                    })
                }
            }
        }