Java Mailing List Archive

http://www.gg3721.com/

Home » user.groovy »

[groovy-user] @Bindable and JList/ListModels

Erik Pragt

2008-08-06

Replies: Find Java Web Hosting

Author LoginPost Reply

Hi all,

I'm currently playing around with Groovy 1.6 + Bindables, and I'm having
some trouble getting it to work the way I want. For trivial code snipplets,
there's not a problem, but for anything more advanced, I'm a little stuck
(well, I have a workaround, but it's far from elegant).

The problem is that I have two JList's, of which one should be aware of the
contents of the other one. For example, I've made a sample which has two
JList's: one with some numbers in it, and the other should display a word as
many times as the number in the selected List. I've gone into the sources of
SwingBuilder and ended up at BindFactory, which has a lot of commented
sourceCode (including the code for JLists...), and refers to JSR-295, which
is currently on hold. So, does anyone have an idea how to make the Swing
components aware of each other?

This is the example code (note the workaround code...I'd like to change that
to something like: model:bind(source:listModel,
sourceProperty:sourceProperty), and since I need to do a conversion from
numbers to elements, maybe something like:

model:bind(source:listModel, sourceProperty:sourceProperty, converter: {
listModel.addElement(it) } )

Code:

import groovy.swing.SwingBuilder
import java.awt.BorderLayout
import groovy.beans.Bindable
import javax.swing.event.ListSelectionListener
import javax.swing.event.ListSelectionEvent
import javax.swing.JList
import javax.swing.DefaultListModel
import javax.swing.JFrame
import java.beans.PropertyChangeListener
import java.beans.PropertyChangeEvent

counter = new Counter()

class Counter {
  @Bindable count = 0
}

class CounterSelectionListener implements ListSelectionListener {
  def counter

  public void valueChanged(ListSelectionEvent e) {
    boolean adjust = e.valueIsAdjusting;

    if (!adjust) {
       JList list = e.source
       println list.selectedValues

       counter.count = list.selectedValue
    }
  }
}


class ListModel extends DefaultListModel implements PropertyChangeListener {
  public void propertyChange(PropertyChangeEvent evt) {
    evt.newValue.times {
       addElement('erik')
    }
  }
}

listModel = new ListModel()
// counter.addPropertyChangeListener(listModel) <-- this is my workaround


def swing = new SwingBuilder()
swing.frame(title: 'test list', pack:true, visible: true,
defaultCloseOperation: JFrame.EXIT_ON_CLOSE) {
  panel {
    borderLayout()
    scrollPane(constraints: BorderLayout.WEST) {
       listData = []
       10.times {
          listData << it
       }

       list(listData: listData).addListSelectionListener(new
CounterSelectionListener(counter:counter))
    }
    label(text:bind(source:counter, sourceProperty:'count'),
constraints:BorderLayout.NORTH)
    scrollPane(constraints: BorderLayout.EAST) {
       list(model:listModel)
    }
  }
}


Thanks for any help, if someone has a solution or a suggestion!
--
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


©2008 gg3721.com - Jax Systems, LLC, U.S.A.