|
View:
New views
5 Messages
—
Rating Filter:
Alert me
|
|
|
binding a dropdown to a List, and adding new values to ListI created a form with a dropdown which displays integer values 1....n. This is bound to two bean properties using beansbinding:
elements: MyBean.choices selectedItem: MyBean.selected MyBean { private List<Integer> choices; private Integer selected; This works fine. I'm wondering how to implement adding new choices though. My setChoices() looks like:
public void setChoices(List<Integer> choices) { List<Integer> old = this. choices; this. choices = choices; firePropertyChange("choices", old, choices);
System.out.println("fired change: " + old + ", " + choices); } and the method which adds a new choice: public void addChoice() {
System.out.println("adding loadchoice"); List<Integer> newChoices = new ArrayList<Integer>(getChoices()); newChoices .add(newChoices .size() + 1);
setLoadNumberChoices(newChoices ); } This works too....I am wondering however if this is the easiest way. It seems like a lot of work to create a new copy of the List each time one item is modified, especially since Lists can contain much more complex objects than Integers. Isn't there a way to just add a new choice to the original List and have beansbinding notice this change?
Tried like this but results is an empty dropdown for some reason: public void addChoice() { System.out.println("adding choice");
choices.add(choices.size() + 1); setChoices(choices); } |
|
|
Re: binding a dropdown to a List, and adding new values to List2009/7/3 janne mattila <jannepostilistat@...>:
> MyBean { > private List<Integer> choices; > private Integer selected; > [...] I am wondering however if this is the easiest way. Try this: change your List<Integer> choices into: private final ObservableList<List> choices = ObservbleCollections.observableList( new ArrayList<Integer>()); As you see, this list is final, you will never change it. Now bind your dropdown to this list. It does not matter if you populate this list before or after binding. You are free to add/remove elements whenever you want and dropdown will be notified/refreshed. > Tried like this but results is an empty dropdown for some reason: > public void addChoice() { > System.out.println("adding choice"); > choices.add(choices.size() + 1); > setChoices(choices); > } This code is just incorrect. a) You get the list object. b) You add element into this list. c) You invoke setChoices with list object which is the same object you got in step (a). I mean this is physically the same object. This step does nothing. It looks like it is setting something, but all it does is nothing :) Try the way with observable list I described at the beginning. I always do it this way. In my program, when I create a form and load new data from server, I always do it like this: List<Something> newData = ...; myListOfSomething.clear(); myListOfSomething.addAll(newData); To be more precise, new data are fetched in #doInBackground method of SwingWorker, then it is passed to #done, and observable lists are reloaded on the Event Dispatch Thread, but this applies to Swing only. Regards, Witold Szczerba |
|
|
Re: binding a dropdown to a List, and adding new values to ListTry this: change your List<Integer> choices into: Cool, that's nice and easy. I didn't even know about ObservableCollections's existence. I can't find much documentation on beansbinding except some 1 page intros and the javadocs. Is there a good document available that describes all features of beansbinding?
Yep.... was just messing around, trying to maybe get some events firing.
That's a nice approach, thanks. |
|
|
Re: binding a dropdown to a List, and adding new values to Listjanne mattila wrote:
> > Cool, that's nice and easy. > > I didn't even know about ObservableCollections's existence. I can't > find much documentation on beansbinding except some 1 page intros and > the javadocs. Is there a good document available that describes all > features of beansbinding? I think that there are many sparse examples if you search through the internetl, blogs, etc. but of course a single, centralized repo would be better. For BBB I've opened this: http://kenai.com/jira/browse/BETTERBEANSBINDING-45 Please add a comment to it with a link to existing stuff that you might know, and eventually attach your own samples. A good set of code examples is already a good value; in any case, once you have got it, it will be simple to write down some documentation. -- Fabrizio Giudici - Java Architect, Project Manager Tidalwave s.a.s. - "We make Java work. Everywhere." weblogs.java.net/blog/fabriziogiudici - www.tidalwave.it/blog Fabrizio.Giudici@... - mobile: +39 348.150.6941 |
|
|
Help Netbeans 6.5.1 not RespondingMy DBA changed the database URL. My main project was last closed with a
page design showing that has tables tied to the database. Now the IDE does not complete it's load -- it appears to the main screen comes up and the projects window comes up, but neither of these allows any operations. The only message is "Opening Main Window". Using Windows XP Professional. What can I do to get the IDE and my project to open? Thanks, |
| Free embeddable forum powered by Nabble | Forum Help |