Using getSelectedValuesList() instead of the deprecated getSelectedValues()

C

clusardi2k

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]);
}
 
J

Jeff Higgins

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.
 
K

Knute Johnson

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!]
 
L

Lew

(unknown) said:
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()'?
 
D

Daniel Pitts

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];
 
M

markspace

On 07/16/2012 05:55 PM, Daniel Pitts wrote:
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.
 
L

Lew

&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.
 
J

Jeff Higgins

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 <[email protected]> that I
gave you terrible advice. Please accept my apology.
 
J

Jeff Higgins

Daniel said:
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.
 
S

Steven Simpson

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
'
Mon, 9 Jul 2012 16:26:42 -0700 (PDT)
http://groups.google.com
&gt; '
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.
 
J

Jeff Higgins

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
(e-mail address removed)'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.
 
R

Roedy Green

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)
 
L

Lars Enderin

2012-07-17 14:45, Steven Simpson skrev:
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.
 
D

Daniele Futtorovic

2012-07-17 14:45, Steven Simpson skrev:

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.
 
L

Lew

Daniel said:
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.
 
J

Joerg Meier

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
 
M

Martin Gregorie

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.
 
D

Daniele Futtorovic

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top