Using getSelectedValuesList() instead of the deprecated getSelectedValues()

  • Follow


Hello, can anyone give me a simple/complete example to replace jLst.getSelectedValues() below using jLst.getSelectedValuesList().

        Object str_array [];

        if ( !jLst.isSelectionEmpty() )
        {        
            str_array = jLst.getSelectedValues();

            System.out.println("Selected: " + str_array[0]);
        }
0
Reply clusardi2k (487) 7/16/2012 7:53:57 PM

On 07/16/2012 03:53 PM, clusardi2k@aol.com wrote:
> Hello, can anyone give me a simple/complete example to replace jLst.getSelectedValues() below using jLst.getSelectedValuesList().
>
>          Object str_array [];
>
>          if ( !jLst.isSelectionEmpty() )
>          {
>              str_array = jLst.getSelectedValues();
>
>              System.out.println("Selected: " + str_array[0]);
>          }
I think the java.util.List<E> interface has a method to return an array.

0
Reply jeff8956 (451) 7/16/2012 8:11:39 PM


On 7/16/2012 12:53 PM, clusardi2k@aol.com wrote:
> Hello, can anyone give me a simple/complete example to replace jLst.getSelectedValues() below using jLst.getSelectedValuesList().
>
>          Object str_array [];
>
>          if ( !jLst.isSelectionEmpty() )
>          {
>              str_array = jLst.getSelectedValues();
>
>              System.out.println("Selected: " + str_array[0]);
>          }
>

import javax.swing.*;
import java.util.*;

public class test {
     public static void main(String[] args) {
         String[] data = {"Hello","World!"};
         JList<String> list = new JList<String>(data);

         // select some
         list.setSelectedIndices(new int[] {0,1});

         // print the selected list
         System.out.println(list.getSelectedValuesList());
     }
}

C:\Documents and Settings\Knute Johnson>java test
[Hello, World!]


0
Reply nospam8071 (917) 7/16/2012 9:12:00 PM

(unknown) wrote:
> Hello, can anyone give me a simple/complete example to replace jLst.getSelectedValues() below using jLst.getSelectedValuesList().
> 
>         Object str_array [];
> 
>         if ( !jLst.isSelectionEmpty() )
>         {        
>             str_array = jLst.getSelectedValues();
> 
>             System.out.println("Selected: " + str_array[0]);
>         }

http://sscce.org/

FIrst, please follow the Java Coding Conventions, particularly with 
respect to naming conventions. (Your opening-brace placement is 
a commonly-accepted variation.)

Second, use names that are more indicative of purpose rather 
than type, and are somewhat more verbose.

Third, show the types of your variables in code samples, at a 
minimum. What is the type of 'jLst'?

I assume that 'getSelectedValues()' returns a collection of some kind.
For some bizarre reason you want to copy that collection into an 
array simply to obtain the element in the first position. Is that correct?

Why not use 'get(0)' or 'iterator().next()'?

If you truly need an array, Jeff Higgins's advice is good.

What is the base type of the collection returned by 'getSelectedValues()'?

-- 
Lew

0
Reply lewbloch (1333) 7/16/2012 9:12:35 PM

On 7/16/12 1:13 PM, Jeff Higgins wrote:
> On 07/16/2012 03:53 PM, clusardi2k@aol.com wrote:
>> Hello, can anyone give me a simple/complete example to replace
>> jLst.getSelectedValues() below using jLst.getSelectedValuesList().
>>
>>          Object str_array [];
>>
>>          if ( !jLst.isSelectionEmpty() )
>>          {
>>              str_array = jLst.getSelectedValues();
>>
>>              System.out.println("Selected: " + str_array[0]);
>>          }
> I think the java.util.List<E> interface has a method to return an array.
>
Indeed it does, but that is terrible advice.

jList.getSelectedValuesList().get(0) is equivalent to 
jList.getSelectedValues()[0];

0
Reply newsgroup.nospam (534) 7/16/2012 9:55:05 PM

On 07/16/2012 05:55 PM, Daniel Pitts wrote:
> On 7/16/12 1:13 PM, Jeff Higgins wrote:
>> On 07/16/2012 03:53 PM, clusardi2k@aol.com wrote:
>>> Hello, can anyone give me a simple/complete example to replace
>>> jLst.getSelectedValues() below using jLst.getSelectedValuesList().
>>>
>>> Object str_array [];
>>>
>>> if ( !jLst.isSelectionEmpty() )
>>> {
>>> str_array = jLst.getSelectedValues();
>>>
>>> System.out.println("Selected: " + str_array[0]);
>>> }
>> I think the java.util.List<E> interface has a method to return an array.
>>
> Indeed it does, but that is terrible advice.

str_array = jLst.getSelectedValuesList().toArray();

Is that much better?

>
> jList.getSelectedValuesList().get(0) is equivalent to
> jList.getSelectedValues()[0];
>

0
Reply jeff8956 (451) 7/16/2012 11:45:41 PM

On 7/16/2012 4:47 PM, Jeff Higgins wrote:

> On 07/16/2012 05:55 PM, Daniel Pitts wrote:

>> On 7/16/12 1:13 PM, Jeff Higgins wrote:
>>> I think the java.util.List<E> interface has a method to return an array.
>>>
>> Indeed it does, but that is terrible advice.

> str_array = jLst.getSelectedValuesList().toArray();
>
> Is that much better?


I think Daniel is saying "prefer Lists to arrays", but then we don't 
know the OP's complete requirements.  If a List will do, then the List 
form is better.  If the OP must have an array, then I'd say one call 
that returns an array is better than doing the same thing with two calls.

0
Reply markspace 7/17/2012 1:05:53 AM

On Monday, July 16, 2012 4:47:53 PM UTC-7, Jeff Higgins wrote:
> Daniel Pitts wrote:
>> Higgins wrote:
>>> clusardi2k... wrote:
> &gt;&gt;&gt; Hello, can anyone give me a simple/complete example to replace
> &gt;&gt;&gt; jLst.getSelectedValues() below using jLst.getSelectedValuesList().
> &gt;&gt;&gt;
> &gt;&gt;&gt; Object str_array [];
> &gt;&gt;&gt;
> &gt;&gt;&gt; if ( !jLst.isSelectionEmpty() )
> &gt;&gt;&gt; {
> &gt;&gt;&gt; str_array = jLst.getSelectedValues();
> &gt;&gt;&gt;
> &gt;&gt;&gt; System.out.println(&quot;Selected: &quot; + str_array[0]);
> &gt;&gt;&gt; }
> &gt;&gt; I think the java.util.List&lt;E&gt; interface has a method to return an array.
> &gt;&gt;
> &gt; Indeed it does, but that is terrible advice.
> 
> str_array = jLst.getSelectedValuesList().toArray();
> 
> Is that much better?
> 
> &gt;
> &gt; jList.getSelectedValuesList().get(0) is equivalent to
> &gt; jList.getSelectedValues()[0];
> &gt;

What's better is

System.out.println("Selected: " + jLst.getSelectedValuesList().get(0));

with appropriate guards against NPE.

Unless, as markspace points out, the OP actually needs an array for 
reasons not in the original post.

-- 
Lew
0
Reply lewbloch (1333) 7/17/2012 2:46:58 AM

On 07/16/2012 03:53 PM, clusardi2k@aol.com wrote:
> Hello, can anyone give me a simple/complete example to replace jLst.getSelectedValues() below using jLst.getSelectedValuesList().
>
>          Object str_array [];
>
>          if ( !jLst.isSelectionEmpty() )
>          {
>              str_array = jLst.getSelectedValues();
>
>              System.out.println("Selected: " + str_array[0]);
>          }

No.[1]

[1] It was pointed out in <_40Nr.24747$cE7.24028@newsfe13.iad> that I 
gave you terrible advice. Please accept my apology.

0
Reply jeff8956 (451) 7/17/2012 11:27:41 AM

On 07/16/2012 10:46 PM, Lew wrote:
> On Monday, July 16, 2012 4:47:53 PM UTC-7, Jeff Higgins wrote:
>> Daniel Pitts wrote:
>>> Higgins wrote:
>>>> clusardi2k... wrote:
>> &gt;&gt;&gt; Hello, can anyone give me a simple/complete example to replace
>> &gt;&gt;&gt; jLst.getSelectedValues() below using jLst.getSelectedValuesList().
>> &gt;&gt;&gt;
>> &gt;&gt;&gt; Object str_array [];
>> &gt;&gt;&gt;
>> &gt;&gt;&gt; if ( !jLst.isSelectionEmpty() )
>> &gt;&gt;&gt; {
>> &gt;&gt;&gt; str_array = jLst.getSelectedValues();
>> &gt;&gt;&gt;
>> &gt;&gt;&gt; System.out.println(&quot;Selected:&quot; + str_array[0]);
>> &gt;&gt;&gt; }
>> &gt;&gt; I think the java.util.List&lt;E&gt; interface has a method to return an array.
>> &gt;&gt;

The XML escapes are distracting. I wish I knew how to get my newsreader 
how to restore them to characters.

>> &gt; Indeed it does, but that is terrible advice.
>>
>> str_array = jLst.getSelectedValuesList().toArray();
>>
>> Is that much better?
>>
>> &gt;
>> &gt; jList.getSelectedValuesList().get(0) is equivalent to
>> &gt; jList.getSelectedValues()[0];
>> &gt;
>
> What's better is
>
> System.out.println("Selected: " + jLst.getSelectedValuesList().get(0));
>
> with appropriate guards against NPE.
>
> Unless, as markspace points out, the OP actually needs an array for
> reasons not in the original post.
>

0
Reply jeff8956 (451) 7/17/2012 11:36:10 AM

On 17/07/12 12:38, Jeff Higgins wrote:
> On 07/16/2012 10:46 PM, Lew wrote:
>> On Monday, July 16, 2012 4:47:53 PM UTC-7, Jeff Higgins wrote:
>>> &gt;&gt;&gt; Hello, can anyone give me a simple/complete example to 
>>> replace
> The XML escapes are distracting. I wish I knew how to get my 
> newsreader how to restore them to characters.

There's no reason it should; it's not declared as XML or similar.

I've been watching this problem for a while, and I'm fairly sure it's 
Lew's news client that is at fault for escaping something that did not 
merit escaping.  Or perhaps it escaped the characters to enable editing 
as HTML, then failed to unescape them when converting back to plain 
text.  I wouldn't be surprised if the client is also mistakenly 
unescaping the text when rendering it back, so Lew doesn't see it.

Here's a selection of Lew's posts, from around when it started:

Date
	Organization
	Problems
Mon, 9 Jul 2012 12:56:02 -0700 (PDT)
	http://groups.google.com
	None
Mon, 9 Jul 2012 16:14:36 -0700 (PDT)
	http://groups.google.com
	&#39;
Mon, 9 Jul 2012 16:26:42 -0700 (PDT)
	http://groups.google.com
	&gt; &#39;
Mon, 09 Jul 2012 23:41:26 -0700
	albasani.net
	None
Mon, 09 Jul 2012 23:47:36 -0700
	albasani.net
	None
Tue, 10 Jul 2012 11:26:33 -0700 (PDT)
	http://groups.google.com
	None, but no challenge
Tue, 10 Jul 2012 11:56:48 -0700 (PDT)
	http://groups.google.com
	&gt;
Wed, 11 Jul 2012 17:03:11 -0700 (PDT)
	http://groups.google.com
	&quot;
Sat, 14 Jul 2012 13:01:44 -0700
	albasani.net
	None


I didn't spot any errors in a quick scan of posts from before the second 
row.

-- 
ss at comp dot lancs dot ac dot uk

0
Reply ss221 (205) 7/17/2012 12:45:32 PM

On 07/16/2012 05:55 PM, Daniel Pitts wrote:
> On 7/16/12 1:13 PM, Jeff Higgins wrote:
>> On 07/16/2012 03:53 PM, clusardi2k@aol.com wrote:
>>> Hello, can anyone give me a simple/complete example to replace
>>> jLst.getSelectedValues() below using jLst.getSelectedValuesList().
>>>
>>> Object str_array [];
>>>
>>> if ( !jLst.isSelectionEmpty() )
>>> {
>>> str_array = jLst.getSelectedValues();
>>>
>>> System.out.println("Selected: " + str_array[0]);
>>> }
>> I think the java.util.List<E> interface has a method to return an array.
>>
> Indeed it does, but that is terrible advice.
>
> jList.getSelectedValuesList().get(0) is equivalent to
> jList.getSelectedValues()[0];
>
Is a jList equivalent to a jLst?
Are either of them equivalent to a javax.swing.JList?

Upon further reflection, it seems to me that the only correct answer to 
clusardi2k@aol.com's question is "no" and that any /advice/s given under 
the assumption that a jLst is meant to indicate a javax.swing.JList are 
equally terrible. One is free to comment on the apparent absurdity of 
the code provided, but those comments are better addressed to the 
original poster of the code rather than to another respondent whose 
/advice/ is as terrible as one's own.

0
Reply jeff8956 (451) 7/17/2012 3:29:49 PM

On Mon, 16 Jul 2012 12:53:57 -0700 (PDT), clusardi2k@aol.com wrote,
quoted or indirectly quoted someone who said :

>Hello, can anyone give me a simple/complete example to replace jLst.getSelectedValues() below using jLst.getSelectedValuesList().

this is a general tip for that sort of problem.  Just google for
"getSelectedValuesList"

There is so much Java code spidered, you will likely find something
well-commented. Make sure it is the right class and package though.
-- 
Roedy Green Canadian Mind Products
http://mindprod.com
The greatest shortcoming of the human race is our inability to understand the exponential function. 
 ~ Dr. Albert A. Bartlett (born: 1923-03-21 age: 89)
http://www.youtube.com/watch?v=F-QA2rkpBSY


0
Reply see_website (4862) 7/17/2012 4:30:26 PM

2012-07-17 14:45, Steven Simpson skrev:
> On 17/07/12 12:38, Jeff Higgins wrote:
>> On 07/16/2012 10:46 PM, Lew wrote:
>>> On Monday, July 16, 2012 4:47:53 PM UTC-7, Jeff Higgins wrote:
>>>> &gt;&gt;&gt; Hello, can anyone give me a simple/complete example to
>>>> replace
>> The XML escapes are distracting. I wish I knew how to get my
>> newsreader how to restore them to characters.
> 
> There's no reason it should; it's not declared as XML or similar.
> 
> I've been watching this problem for a while, and I'm fairly sure it's
> Lew's news client that is at fault for escaping something that did not
> merit escaping.  Or perhaps it escaped the characters to enable editing
> as HTML, then failed to unescape them when converting back to plain
> text.  I wouldn't be surprised if the client is also mistakenly
> unescaping the text when rendering it back, so Lew doesn't see it.

It's not XML, it's HTML, and the guilty party is Google (Groups). It
seems to get even worse than it used to be.

-- 
Lars Enderin


0
Reply lars.enderin1 (162) 7/17/2012 5:46:36 PM

On 17/07/2012 19:46, Lars Enderin allegedly wrote:
> 2012-07-17 14:45, Steven Simpson skrev:
>> On 17/07/12 12:38, Jeff Higgins wrote:
>>> On 07/16/2012 10:46 PM, Lew wrote:
>>>> On Monday, July 16, 2012 4:47:53 PM UTC-7, Jeff Higgins wrote:
>>>>> &gt;&gt;&gt; Hello, can anyone give me a simple/complete example to
>>>>> replace
>>> The XML escapes are distracting. I wish I knew how to get my
>>> newsreader how to restore them to characters.
>>
>> There's no reason it should; it's not declared as XML or similar.
>>
>> I've been watching this problem for a while, and I'm fairly sure it's
>> Lew's news client that is at fault for escaping something that did not
>> merit escaping.  Or perhaps it escaped the characters to enable editing
>> as HTML, then failed to unescape them when converting back to plain
>> text.  I wouldn't be surprised if the client is also mistakenly
>> unescaping the text when rendering it back, so Lew doesn't see it.
> 
> It's not XML, it's HTML, and the guilty party is Google (Groups). It
> seems to get even worse than it used to be.
> 

Amen to that.

-- 
DF.
0
Reply da.futt.news (226) 7/17/2012 6:47:55 PM

On 7/17/12 11:47 AM, Daniele Futtorovic wrote:
> On 17/07/2012 19:46, Lars Enderin allegedly wrote:
>> 2012-07-17 14:45, Steven Simpson skrev:
>>> On 17/07/12 12:38, Jeff Higgins wrote:
>>>> On 07/16/2012 10:46 PM, Lew wrote:
>>>>> On Monday, July 16, 2012 4:47:53 PM UTC-7, Jeff Higgins wrote:
>>>>>> &gt;&gt;&gt; Hello, can anyone give me a simple/complete example to
>>>>>> replace
>>>> The XML escapes are distracting. I wish I knew how to get my
>>>> newsreader how to restore them to characters.
>>>
>>> There's no reason it should; it's not declared as XML or similar.
>>>
>>> I've been watching this problem for a while, and I'm fairly sure it's
>>> Lew's news client that is at fault for escaping something that did not
>>> merit escaping.  Or perhaps it escaped the characters to enable editing
>>> as HTML, then failed to unescape them when converting back to plain
>>> text.  I wouldn't be surprised if the client is also mistakenly
>>> unescaping the text when rendering it back, so Lew doesn't see it.
>>
>> It's not XML, it's HTML, and the guilty party is Google (Groups). It
>> seems to get even worse than it used to be.
>>
>
> Amen to that.
>
It's gotten *worse* than intolerable? Didn't know that was possible. 
It's why I switched to a "real" news client.

0
Reply newsgroup.nospam (534) 7/17/2012 7:26:46 PM

Daniel Pitts wrote:
> It's gotten *worse* than intolerable? Didn't know that was possible. 
> It's why I switched to a "real" news client.

I use GG when it's convenient, but this "unescape" difficulty makes that 
less frequent than it used to be.

Yes, one sees the escape sequences in the editor box for a reply. It's 
a lot of work to clear them, so I mostly don't.

When I use Thunderbird as the news client life is much happier.

-- 
Lew

0
Reply lewbloch (1333) 7/17/2012 9:16:59 PM

On Tue, 17 Jul 2012 12:26:46 -0700, Daniel Pitts wrote:

> On 7/17/12 11:47 AM, Daniele Futtorovic wrote:
>> On 17/07/2012 19:46, Lars Enderin allegedly wrote:
>>> It's not XML, it's HTML, and the guilty party is Google (Groups). It
>>> seems to get even worse than it used to be.
>> Amen to that.
> It's gotten *worse* than intolerable? Didn't know that was possible. 
> It's why I switched to a "real" news client.

Google Groups has apparently been given an ... 'underhaul' ? recently. It
stopped respecting Follow-Up's as well, and in fact doesn't even inform the
user (let alone giving them a choice).

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.
0
Reply joergmmeier (151) 7/18/2012 2:08:14 PM

On Wed, 18 Jul 2012 16:08:14 +0200, Joerg Meier wrote:

> 
> Google Groups has apparently been given an ... 'underhaul' ? recently.
> It stopped respecting Follow-Up's as well, and in fact doesn't even
> inform the user (let alone giving them a choice).
>
What do you expect? 

As long as Google persists in describing almost everything it provides as 
a beta release and never seems willing to promote it to a production 
release, I'd say you should expect BAAFNRAA behavior from their code.

BAAFNRAA = Bugs Anywhere, Anytime For No Reason At All.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |
0
Reply martin1645 (530) 7/18/2012 9:33:14 PM

On 18/07/2012 23:33, Martin Gregorie allegedly wrote:
> On Wed, 18 Jul 2012 16:08:14 +0200, Joerg Meier wrote:
> 
>>
>> Google Groups has apparently been given an ... 'underhaul' ? recently.
>> It stopped respecting Follow-Up's as well, and in fact doesn't even
>> inform the user (let alone giving them a choice).
>>
> What do you expect? 
> 
> As long as Google persists in describing almost everything it provides as 
> a beta release and never seems willing to promote it to a production 
> release, I'd say you should expect BAAFNRAA behavior from their code.
> 
> BAAFNRAA = Bugs Anywhere, Anytime For No Reason At All.

Rubbish. There's always a reason for bugs.

/readies excuse calendar/ Try me.

-- 
DF.
0
Reply da.futt.news (226) 7/18/2012 9:36:28 PM

On Wed, 18 Jul 2012 23:36:28 +0200, Daniele Futtorovic wrote:

> On 18/07/2012 23:33, Martin Gregorie allegedly wrote:
>> On Wed, 18 Jul 2012 16:08:14 +0200, Joerg Meier wrote:
>> 
>> 
>>> Google Groups has apparently been given an ... 'underhaul' ? recently.
>>> It stopped respecting Follow-Up's as well, and in fact doesn't even
>>> inform the user (let alone giving them a choice).
>>>
>> What do you expect?
>> 
>> As long as Google persists in describing almost everything it provides
>> as a beta release and never seems willing to promote it to a production
>> release, I'd say you should expect BAAFNRAA behavior from their code.
>> 
>> BAAFNRAA = Bugs Anywhere, Anytime For No Reason At All.
> 
> Rubbish. There's always a reason for bugs.
> 
> /readies excuse calendar/ Try me.

Even if the 'reason' is "We don't believe in regression testing"?


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |
0
Reply martin1645 (530) 7/18/2012 9:53:06 PM

On 18/07/2012 23:53, Martin Gregorie allegedly wrote:
> On Wed, 18 Jul 2012 23:36:28 +0200, Daniele Futtorovic wrote:
>> On 18/07/2012 23:33, Martin Gregorie allegedly wrote:
>>> BAAFNRAA = Bugs Anywhere, Anytime For No Reason At All.
>>
>> Rubbish. There's always a reason for bugs.
>>
>> /readies excuse calendar/ Try me.
> 
> Even if the 'reason' is "We don't believe in regression testing"?

Hmm. I was thinking more along the lines of solar flares, alignment of
planets, the bus having a flat tire or the NIC having run out of bits.

I think one should generally avoid faith-based bug-reasoning. Too easy
to attack; not peremptory enough. Amateurish, in a nutshell. /waves
disdainfully/

-- 
DF.
0
Reply da.futt.news (226) 7/18/2012 10:12:10 PM

On 7/18/2012 3:12 PM, Daniele Futtorovic wrote:
>
> ...  or the NIC having run out of bits.


I once lost my token ring, but I found it later caught in the ethernet.



0
Reply markspace 7/18/2012 10:53:06 PM

On 7/18/12 3:53 PM, markspace wrote:
> On 7/18/2012 3:12 PM, Daniele Futtorovic wrote:
>>
>> ...  or the NIC having run out of bits.
>
>
> I once lost my token ring, but I found it later caught in the ethernet.
I once lost my Tolkien ring, but I found it later on some nasty hobbitses.



0
Reply newsgroup.nospam (534) 7/18/2012 11:18:38 PM

On Thu, 19 Jul 2012 00:12:10 +0200, Daniele Futtorovic wrote:

> On 18/07/2012 23:53, Martin Gregorie allegedly wrote:
>> On Wed, 18 Jul 2012 23:36:28 +0200, Daniele Futtorovic wrote:
>>> On 18/07/2012 23:33, Martin Gregorie allegedly wrote:
>>>> BAAFNRAA = Bugs Anywhere, Anytime For No Reason At All.
>>>
>>> Rubbish. There's always a reason for bugs.
>>>
>>> /readies excuse calendar/ Try me.
>> 
>> Even if the 'reason' is "We don't believe in regression testing"?
> 
> Hmm. I was thinking more along the lines of solar flares, alignment of
> planets, the bus having a flat tire or the NIC having run out of bits.
> 
> I think one should generally avoid faith-based bug-reasoning. Too easy
> to attack; not peremptory enough. Amateurish, in a nutshell. /waves
> disdainfully/
>
If you must know, I couldn't resist misquoting some Frank Zappa initial 
slang: AAFNRAA (Anything Anytime For No Reason At All).

This is equivalent, roughly, to the statement that anything is art if you 
put a frame round it.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |
0
Reply martin1645 (530) 7/18/2012 11:44:03 PM

24 Replies
64 Views

(page loaded in 0.317 seconds)


Reply: