Groovy SwingBuilder : using a scrollpanel to show a list of panels

瘦欲@ 提交于 2019-12-11 02:04:49

问题


I would like to show a list of panels containing components, i.e a checkbox, labels, buttons, all on the same horizontal line; each panel represents one set of components to display information for one item. I need to put the list of panels (number undetermined) inside a scrollpanel to fit within the main panel height.

I can't seem to find a solution for mixing scrollpanel and panels with components.

I'd like to get this result :

scrollpanel {

  • checkbox | item1 | button1 | button1 | label1 | label1
  • checkbox | item2 | button2 | button2 | label2 | label2
  • checkbox | item3 | button3 | button3 | label3 | label3

    [ ... ]

}

There is a working example of what I have currently shown here : Groovy SwingBuilder : button to change the color of a panel

There, you can see there are 6 items, each one with their respective components relating to it. Now if I wanted to display 60 items instead of 6, the frame would expand to fit them but exceed the screen size.

I seems so obvious to me that kind of a "scrollpanel" would do the job, but I can't get it working, although I checked all examples on the Java tutorials and the related questions here.

tia. Michel


回答1:


You can put the panels inside a vbox, which in-turn you put inside a scrollPane.

Taking the code from the previous question, you end up with something like this:

import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.JScrollPane
import javax.swing.BoxLayout as BXL

int numPanels = 20

swing = new SwingBuilder()
frame = swing.frame(title:'test', pack:true, visible:true, defaultCloseOperation:WC.HIDE_ON_CLOSE) {
  panel(id:'mainPanel'){
    scrollPane( verticalScrollBarPolicy:JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ) {
      vbox {
        (1..numPanels).each { num ->
          def panelID = "panel$num"
          def pane = panel( alignmentX:0f, id:panelID, background:java.awt.Color.GREEN ) {
            label('description') 
            textField( id: "description$num", text:panelID, columns: 70 )
            button( id: "buttonpanel$num", text:panelID, actionPerformed:{
              swing."$panelID".background = java.awt.Color.RED
            } )
          }
        }
      }
    }

    boxLayout(axis: BXL.Y_AXIS)
    panel(id:'secondPanel' , alignmentX: 0f){                       
      button('Quit', actionPerformed:{
        frame.visible = false
      })
    }
  }       
}
frame.size = [ frame.width, 600 ]


来源:https://stackoverflow.com/questions/6444068/groovy-swingbuilder-using-a-scrollpanel-to-show-a-list-of-panels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!