Ian Dickinson wrote:
> Hi Muhammad,
> On 07/03/2009 01:24 PM, Muhammad Javed wrote:
>> Can anyone help me how
>> one can cast/convert a String object to Resource.
>>
>> String list = (String) JComboBox.getSelectedItem();
>> DatatypeProperty.addRange ( (Resource) list); <<<<<<<<<<<<<
>> Inconvertible Types.
> There's a simple answer and a more complicated answer to your question.
> First of all, if the combo box contains the actual URI's of the values
> you want your users to select, you can just convert the URI to a Resource:
>
> String uri = .. get the selected item string .. ;
> Resource r = yourModel.createResource( uri );
>
> However, that's unlikely to be an acceptable solution for your users,
> because URI's are not very readable things to have in a UI. What you
> probably want is some table or other datastructure (like a HashMap) that
> associates resource URI's with a readable label, such as the value of
> the rdfs:label property (there are a number of other alternatives to
> rdfs:label, including SKOS).
I remember facing a similar issue a couple of years back; my solution
was to put the Resources as elements in the combobox, and specify a
Renderer (? I might remember the name wrong) which is basically a class
that knows how to display a specific kind of objects. That way, the URIs
were nicely embellished (namespace abbreviations, localnames, what you
like the most) while I had no problem with conversions since I was
retrieving Resources with getSelectedItem. I don't have that code around
but I hope this generally nudges you in the right direction.
HTH,
I.
>
> How you set up that mapping is up to you: it's not something that Jena
> cares about or has anything to offer. If it was me, I would probably
> consider creating a custom ComboBoxModel which was populated initially
> with a SPARQL query into your model. Another possibility would be to
> populate the ComboBoxModel with actual resources, but use a custom
> ListCellRenderer to display the rdfs:label instead of the resource's
> URI. Using the renderer approach, there would be no need to convert the
> selected value to a resource, since the user would be selecting the
> resource directly.
>
> However, without knowing your business goals or user requirements, it's
> really not something that we can advise on on this list.
>
> Ian
>
> ____________________________________________________________
> Ian Dickinson http://www.hpl.hp.com/people/ian_dickinson
> HP Laboratories Bristol mailto:ian.dickinson@...
> Hewlett-Packard Limited Registered No: 690597 England
> Registered Office: Cain Road, Bracknell, Berks RG12 1HN
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
>