[groovy-user] SwingBuilder list

118 views
Skip to first unread message

David Rosenstark

unread,
Oct 26, 2009, 9:37:35 AM10/26/09
to us...@groovy.codehaus.org

Hi,

I am trying to use a SwingBuilder list.

I have a simple window that should have a select all and deselect all button.

However, I don’t see a property that allows me to access all items in the list to have a closure mark them all as selected or deselected.

The only thing I see is that I can do myList.getModel.getElementAt() – but then I cannot do it in a closure.

What are my options here?

 

Thanks,

David

Tim Yates

unread,
Oct 26, 2009, 10:05:11 AM10/26/09
to us...@groovy.codehaus.org
This works in the Griffon swingPad:

all  = action( name:'Select All',  closure:{
  listId.selectedIndices = (0..listId.model.size()) as int[]
} )
none = action( name:'Select None', closure:{
  listId.selectedIndices = [] as int[]
} )

panel( preferredSize:[ 320, 500 ] ) {
  borderLayout()
  scrollPane( constraints:CENTER ) {
    theList = list( id:'listId' )
   
    theList.listData = (1..50).collect { "Line $it" }
  }
  panel( constraints:SOUTH ) {
    button( all )
    button( none )
  }
}

Hope it helps...

TIm

David Rosenstark

unread,
Oct 26, 2009, 10:18:16 AM10/26/09
to us...@groovy.codehaus.org

Wow, that came just in time as I was real close and was having just missing the cast (newbie issue!)

 

For deselect, there is also this:

list1.selectionModel.clearSelection()

 

Side question: Does it pay for me to use griffon, if there is no db for my small app? Is there any notable advantage?

 


Danno Ferrin

unread,
Oct 26, 2009, 3:58:31 PM10/26/09
to us...@groovy.codehaus.org
Griffon adds a richer MVC infrastructure with framework support for the APIs.

It also provides easier packaging support.  For the smaller apps the ability to roll out your code as an applet or webstart application with a minimum of work would be a big gain.

 You can also bring in other widget sets more easily if there is a plugin for it already (such as MacWidgets, SwingX, or Jide).

--Danno
--
------------------------------------------------------
"But you didn't." - Jim Halpert, The Office S05E23

David Rosenstark

unread,
Oct 27, 2009, 1:53:38 AM10/27/09
to us...@groovy.codehaus.org

Thanks, I will give it a try. It actually is a web start app which took a while until I got the packaging together manually

 

David

 


David Rosenstark

unread,
Nov 4, 2009, 7:01:53 AM11/4/09
to us...@groovy.codehaus.org

Hi,

I have a simple window created using swingbuilder

I have a few buttons and would like to have the focus on the last button so that the user can hit enter and it closes the window with all the defaults as set.

I have tried defaultButton and selected but neither of these seems to work. Any ideas?

 

  button(action: action(name: 'Select All', closure: {

            list1.selectedIndices = (0 .. list1.model.size - 1) as int[]

          }))

          button(action: action(name: 'DeSelect All', closure:{

                              list1.selectedIndices = [] as int[]

             }))

           button(action: action(name: 'OK', defaultButton: true, selected: true,

                  closure:{

                          println("button clicked")

                          list1.selectedValues.each {

                            println(it)

                          dispose()

                          } }))

Tim Yates

unread,
Nov 4, 2009, 7:56:59 AM11/4/09
to us...@groovy.codehaus.org
Does it work if you change it to:

      button(action: action(name: 'Select All', closure: {
        list1.selectedIndices = (0 .. list1.model.size - 1) as int[]
      }))
      button(action: action(name: 'DeSelect All', closure:{
        list1.selectedIndices = [] as int[]
      }))
      button( id:'defBtn', defaultCapable:true, action: action(name: 'OK', selected: true, closure:{

        println("button clicked")
        list1.selectedValues.each {
        println(it)
        dispose()
      } }))
      defBtn.rootPane.defaultButton = defBtn

Tim

Tim Yates

unread,
Nov 4, 2009, 8:42:23 AM11/4/09
to us...@groovy.codehaus.org
Just to be extra safe, that last line should probably be:

      defBtn.rootPane?.defaultButton = defBtn

I'd rather have no default button than a crash ;-)

Tim

David Rosenstark

unread,
Nov 4, 2009, 9:04:59 AM11/4/09
to us...@groovy.codehaus.org

I am not sure I understand what you are saying.

Is rootPane reserved or the name of my rootPane.

When I took it as is (assuming it was reserved) I get an error:

 

No such property: rootPane for class: groovy.swing.impl.DefaultAction

 


From: Tim Yates [mailto:tim....@gmail.com]

Sent: Wednesday, November 04, 2009 3:42 PM
To: us...@groovy.codehaus.org

Tim Yates

unread,
Nov 4, 2009, 9:20:30 AM11/4/09
to us...@groovy.codehaus.org
the rootPane property gets you the rootPane of the button by calling "getRootPane()"

It's a method in swing

To set the default button, you set it so it is "default enabled", then pass the button to the rootpane's "setDefaultButton" method

Have you set the id on the button as I showed?

Tim

Danno Ferrin

unread,
Nov 4, 2009, 12:59:32 PM11/4/09
to us...@groovy.codehaus.org
The "default button" only works when the focus is already on the window but not on any other button, for example:

new groovy.swing.SwingBuilder().edt {
 frame(pack:true, show:true, defaultCloseOperation:javax.swing.JFrame.DISPOSE_ON_CLOSE) {
  flowLayout()
  textField("Yo ho ho!")
  button("one")
  button("two", defaultButton:true, actionPerformed: {e->println 'yo!'})
  button("Three")
 }
}

if you place the cursor in the text component and press enter, 'yo' will fire.  But if it is on any other button or not in the frame, nothing happens.

What you probably want is keyboard focus and window focus.  To do this you need to track the button and possibly the dialog/frame.  The swing requestFocusInWindow and requestFocus methods can help there:


frame {
   //....
   myButton = button(action: okAction, defaultButton) ...
}

myButton.requestFocusInWindow()
// Or...
myButton.requestFocus()


The difference is that requestFocusInWindow() will not attempt to cause the root level component to gain focus, whill requestFocus() will attempt.  And by attempt, some OS window managers may suppress that call and simply cause the doc icon to bounce or the taskbar to flash for that task, I don't rememeber the specific rules and specific cases.  If your java app already has focus, it should shift to the reqesting window.

David Rosenstark

unread,
Nov 4, 2009, 3:28:32 PM11/4/09
to us...@groovy.codehaus.org

I did exactly as you showed me and got the error below

David Rosenstark

unread,
Nov 4, 2009, 3:35:43 PM11/4/09
to us...@groovy.codehaus.org

Thanks. I am still not clear – I want to open a dialog on top of main window and set focus to this button. So, when I hit show, I need to have already requested focus for it?

 

 


From: Danno Ferrin [mailto:danno....@shemnon.com]
Sent: Wednesday, November 04, 2009 8:00 PM
To: us...@groovy.codehaus.org
Subject: Re: [groovy-user] default button in swingbuilder

 

The "default button" only works when the focus is already on the window but not on any other button, for example:

Danno Ferrin

unread,
Nov 5, 2009, 2:56:32 PM11/5/09
to us...@groovy.codehaus.org
I would do the request after the window deceleration...

dialog( ..., show:true ...) {
  myButton = button( ... defaultButton:true ...)
}

button.requestFocus()

David Rosenstark

unread,
Nov 9, 2009, 9:14:41 AM11/9/09
to us...@groovy.codehaus.org

Sorry, but I am still not clear on this.

If I have a modal dialog (as is my case) then I will not be able to run additional code to set focus. Is there on onFocus of the window?

How do I catch such events in groovy?

 

Thanks,

David

Danno Ferrin

unread,
Nov 9, 2009, 2:06:03 PM11/9/09
to us...@groovy.codehaus.org
In that case move the code to set the focus inside the dialog as a do later, and do a requestFocusInWindow(), and use the doLater closure...


dialog( ..., show:true, modal:true, ...) {
  //...

  myButton = button( ... defaultButton:true ...)
  //...
  doLater {
button.requestFocusInWindow()}
}

The show handler runs after the complete execution of the window.  So assuming you are running the builder in the EDT the doLater will result in a event being fired on the EDT.  I haven't tried this yet so there may be some wierdness with the way modal dialogs intercept the EDT.

Another option is the the listeners.  windowActivated and windowGainedFocus are the two you would want to try.

dialog ( ..., show:true, windowGainedFocus {myButton.requestFocusInWindow}, ...) {
  myButton = button( ... defaultButton:true ...)
}

as long as myButton is unbound the above should work.
Reply all
Reply to author
Forward
0 new messages