SwingBuilder list

View: New views
16 Messages — Rating Filter:   Alert me  

SwingBuilder list

by David R :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

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


Re: SwingBuilder list

by Tim Yates :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

On Mon, Oct 26, 2009 at 1:37 PM, David Rosenstark <David.Rosenstark@...> wrote:

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



RE: SwingBuilder list

by David R :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

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?

 


From: Tim Yates [mailto:tim.yates@...]
Sent: Monday, October 26, 2009 4:05 PM
To: user@...
Subject: Re: [groovy-user] SwingBuilder list

 

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

On Mon, Oct 26, 2009 at 1:37 PM, David Rosenstark <David.Rosenstark@...> wrote:

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

 


Re: SwingBuilder list

by shemnon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

On Mon, Oct 26, 2009 at 8:18 AM, David Rosenstark <David.Rosenstark@...> wrote:

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?

 


From: Tim Yates [mailto:tim.yates@...]
Sent: Monday, October 26, 2009 4:05 PM
To: user@...
Subject: Re: [groovy-user] SwingBuilder list

 

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

On Mon, Oct 26, 2009 at 1:37 PM, David Rosenstark <David.Rosenstark@...> wrote:

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

 




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

RE: SwingBuilder list

by David R :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

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

 


From: Danno Ferrin [mailto:danno.ferrin@...]
Sent: Monday, October 26, 2009 9:59 PM
To: user@...
Subject: Re: [groovy-user] SwingBuilder list

 

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

On Mon, Oct 26, 2009 at 8:18 AM, David Rosenstark <David.Rosenstark@...> wrote:

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?

 


From: Tim Yates [mailto:tim.yates@...]
Sent: Monday, October 26, 2009 4:05 PM
To: user@...
Subject: Re: [groovy-user] SwingBuilder list

 

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

On Mon, Oct 26, 2009 at 1:37 PM, David Rosenstark <David.Rosenstark@...> wrote:

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

 




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


default button in swingbuilder

by David R :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

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

                          } }))


Re: default button in swingbuilder

by Tim Yates :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

On Wed, Nov 4, 2009 at 12:01 PM, David Rosenstark <David.Rosenstark@...> wrote:

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

                          } }))



Re: default button in swingbuilder

by Tim Yates :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

On Wed, Nov 4, 2009 at 12:56 PM, Tim Yates <tim.yates@...> wrote:
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

On Wed, Nov 4, 2009 at 12:01 PM, David Rosenstark <David.Rosenstark@...> wrote:

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

                          } }))




RE: default button in swingbuilder

by David R :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

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.yates@...]
Sent: Wednesday, November 04, 2009 3:42 PM
To: user@...
Subject: Re: [groovy-user] default button in swingbuilder

 

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

On Wed, Nov 4, 2009 at 12:56 PM, Tim Yates <tim.yates@...> wrote:

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

 

On Wed, Nov 4, 2009 at 12:01 PM, David Rosenstark <David.Rosenstark@...> wrote:

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

                          } }))

 

 


Re: default button in swingbuilder

by Tim Yates :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

On Wed, Nov 4, 2009 at 2:04 PM, David Rosenstark <David.Rosenstark@...> wrote:

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.yates@...]
Sent: Wednesday, November 04, 2009 3:42 PM
To: user@...
Subject: Re: [groovy-user] default button in swingbuilder

 

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

On Wed, Nov 4, 2009 at 12:56 PM, Tim Yates <tim.yates@...> wrote:

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

 

On Wed, Nov 4, 2009 at 12:01 PM, David Rosenstark <David.Rosenstark@...> wrote:

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

                          } }))

 

 



Re: default button in swingbuilder

by shemnon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

On Wed, Nov 4, 2009 at 5:01 AM, David Rosenstark <David.Rosenstark@...> wrote:

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

                          } }))




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

RE: default button in swingbuilder

by David R :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

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

 

 

 

 

From: Tim Yates [mailto:tim.yates@...]
Sent: Wednesday, November 04, 2009 4:21 PM
To: user@...
Subject: Re: [groovy-user] default button in swingbuilder

 

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

On Wed, Nov 4, 2009 at 2:04 PM, David Rosenstark <David.Rosenstark@...> wrote:

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.yates@...]
Sent: Wednesday, November 04, 2009 3:42 PM
To: user@...
Subject: Re: [groovy-user] default button in swingbuilder

 

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

On Wed, Nov 4, 2009 at 12:56 PM, Tim Yates <tim.yates@...> wrote:

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

 

On Wed, Nov 4, 2009 at 12:01 PM, David Rosenstark <David.Rosenstark@...> wrote:

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

                          } }))

 

 

 


RE: default button in swingbuilder

by David R :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

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.ferrin@...]
Sent: Wednesday, November 04, 2009 8:00 PM
To: user@...
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:

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.

On Wed, Nov 4, 2009 at 5:01 AM, David Rosenstark <David.Rosenstark@...> wrote:

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

                          } }))




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


Re: default button in swingbuilder

by shemnon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I would do the request after the window deceleration...

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

button.requestFocus()



On Wed, Nov 4, 2009 at 1:35 PM, David Rosenstark <David.Rosenstark@...> wrote:

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.ferrin@...]
Sent: Wednesday, November 04, 2009 8:00 PM


To: user@...
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:



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.

On Wed, Nov 4, 2009 at 5:01 AM, David Rosenstark <David.Rosenstark@...> wrote:

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

                          } }))




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




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

RE: default button in swingbuilder

by David R :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

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

 


From: Danno Ferrin [mailto:danno.ferrin@...]
Sent: Thursday, November 05, 2009 9:57 PM
To: user@...
Subject: Re: [groovy-user] default button in swingbuilder

 

I would do the request after the window deceleration...

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

button.requestFocus()


On Wed, Nov 4, 2009 at 1:35 PM, David Rosenstark <David.Rosenstark@...> wrote:

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.ferrin@...]
Sent: Wednesday, November 04, 2009 8:00 PM


To: user@...
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:



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.

On Wed, Nov 4, 2009 at 5:01 AM, David Rosenstark <David.Rosenstark@...> wrote:

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

                          } }))




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




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


Re: default button in swingbuilder

by shemnon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.


On Mon, Nov 9, 2009 at 7:14 AM, David Rosenstark <David.Rosenstark@...> wrote:

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

 


From: Danno Ferrin [mailto:danno.ferrin@...]
Sent: Thursday, November 05, 2009 9:57 PM


To: user@...
Subject: Re: [groovy-user] default button in swingbuilder

 

I would do the request after the window deceleration...

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

button.requestFocus()


On Wed, Nov 4, 2009 at 1:35 PM, David Rosenstark <David.Rosenstark@...> wrote:

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.ferrin@...]
Sent: Wednesday, November 04, 2009 8:00 PM


To: user@...
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:



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.

On Wed, Nov 4, 2009 at 5:01 AM, David Rosenstark <David.Rosenstark@...> wrote:

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

                          } }))




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




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




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