JavaFX ReadOnlyListProperty not read only? -


this code throws unsupportedoperationexception, expect since it's read only.

listproperty<string> lp = new simplelistproperty<string>(); readonlylistwrapper<string> rolw = new readonlylistwrapper<string>(lp); readonlylistproperty<string> rolp = rolw.getreadonlyproperty(); rolp.add("element"); 

however, code not.

observablelist<string> ol = fxcollections.observablearraylist(); readonlylistwrapper<string> rolw = new readonlylistwrapper<string>(ol); readonlylistproperty<string> rolp = rolw.getreadonlyproperty(); rolp.add("element"); 

is bug, or not understanding something?

the original expectation wrong - examples provided. unsupportedoperationexception occurs different reason , not because "read-only" list being "written" to. still possible have "read-only" lists. hope answer below helps clarify.

the answer needs considered in 2 parts. one: listproperty exception , two: read-only lists.

1) listproperty example fails because no list has been assigned property.

this, simplified, example throws exception. note "read-only" aspects removed:

listproperty<string> lp = new simplelistproperty<>(); lp.add("element"); 

this can corrected with:

observablelist ol = fxcollections.observablearraylist(); listproperty<string> lp = new simplelistproperty<>(); lp.setvalue(ol); lp.add("element"); 

if change original example in similar manner neither listproperty nor observablelist examples throw exception, wasn't op wanted or expected.

2) second part asks why can element added read-only list. using fxcollections.unmodifiableobservablelist create read-only list throw unsupportedoperationexception expected:

observablelist<string> ol = fxcollections.observablearraylist(); observablelist<string> uol = fxcollections.unmodifiableobservablelist(ol); uol.add("element"); 

but doesn't answer question why doesn't readonlylistwrapper/property this?

let's deal property first. listproperty enable value changed, i.e. allows assign different list property. readonlylistproperty not allow this, i.e. once list assigned remains list object. content of list can still change. example below makes no sense readonlylistproperty:

observablelist<string> ol1 = fxcollections.observablearraylist(); observablelist<string> ol2 = fxcollections.observablearraylist(); listproperty<string> lp = new simplelistproperty<>(ol1); lp.setvalue(ol2); 

so read-only refers property, , not list.

finally - readonlylistwrapper - api documentation states "this class provides convenient class define read-only properties. creates 2 properties synchronized. 1 property read-only , can passed external users. other property read- , writable , should used internally only."


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -