Using getSelectedValuesList() instead of the deprecated getSelectedValues()

Discussion in 'Java' started by clusardi2k@aol.com, Jul 16, 2012.

  1. Guest

    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]);
    }
     
    , Jul 16, 2012
    #1
    1. Advertising

  2. Jeff Higgins Guest

    On 07/16/2012 03:53 PM, 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.
     
    Jeff Higgins, Jul 16, 2012
    #2
    1. Advertising

  3. On 7/16/2012 12:53 PM, 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!]
     
    Knute Johnson, Jul 16, 2012
    #3
  4. Lew Guest

    (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
     
    Lew, Jul 16, 2012
    #4
  5. Daniel Pitts Guest

    On 7/16/12 1:13 PM, Jeff Higgins wrote:
    > On 07/16/2012 03:53 PM, 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];
     
    Daniel Pitts, Jul 16, 2012
    #5
  6. markspace Guest

    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.
     
    markspace, Jul 17, 2012
    #6
  7. Lew Guest

    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
     
    Lew, Jul 17, 2012
    #7
  8. Jeff Higgins Guest

    On 07/16/2012 03:53 PM, 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$> that I
    gave you terrible advice. Please accept my apology.
     
    Jeff Higgins, Jul 17, 2012
    #8
  9. Jeff Higgins Guest

    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.
    >
     
    Jeff Higgins, Jul 17, 2012
    #9
  10. 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
    '
    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.

    --
    ss at comp dot lancs dot ac dot uk
     
    Steven Simpson, Jul 17, 2012
    #10
  11. Jeff Higgins Guest

    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, 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
    '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.
     
    Jeff Higgins, Jul 17, 2012
    #11
  12. Roedy Green Guest

    On Mon, 16 Jul 2012 12:53:57 -0700 (PDT), 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
     
    Roedy Green, Jul 17, 2012
    #12
  13. Lars Enderin Guest

    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
     
    Lars Enderin, Jul 17, 2012
    #13
  14. 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.
     
    Daniele Futtorovic, Jul 17, 2012
    #14
  15. Daniel Pitts Guest

    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.
     
    Daniel Pitts, Jul 17, 2012
    #15
  16. Lew Guest

    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
     
    Lew, Jul 17, 2012
    #16
  17. Joerg Meier Guest

    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.
     
    Joerg Meier, Jul 18, 2012
    #17
  18. Re: Using getSelectedValuesList() instead of the deprecatedgetSelectedValues()

    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 |
     
    Martin Gregorie, Jul 18, 2012
    #18
  19. 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.
     
    Daniele Futtorovic, Jul 18, 2012
    #19
  20. Re: Using getSelectedValuesList() instead of the deprecatedgetSelectedValues()

    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 |
     
    Martin Gregorie, Jul 18, 2012
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Barney Barumba
    Replies:
    0
    Views:
    565
    Barney Barumba
    Jul 23, 2003
  2. puzzlecracker
    Replies:
    1
    Views:
    338
    Thomas Hawtin
    Jan 30, 2006
  3. SenthilVel
    Replies:
    0
    Views:
    954
    SenthilVel
    Jun 7, 2006
  4. Replies:
    0
    Views:
    439
  5. Brian Takita
    Replies:
    1
    Views:
    142
    Charles Oliver Nutter
    Nov 19, 2010
Loading...

Share This Page