Michel Parmentier
Michel Parmentier

Reputation: 141

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

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 {

}

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

Upvotes: 2

Views: 2646

Answers (1)

tim_yates
tim_yates

Reputation: 171114

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 ]

Upvotes: 3

Related Questions