Results 1 to 6 of 6
  1. #1
    MGeorge's Avatar
    MGeorge is offline Aging Hacker
    Join Date
    Sep 2014
    Location
    East (San Francisco) Bay, California, USA
    Posts
    20
    Rep Power
    0

    Default Seeking ClientProperty Keys

    I'm using JComponent.putClientProperty() and JComponent.getClientProperty() to handle information related to menu items.
    I'm assuming that these values are stored in some kind of Map. Does anyone know of a way to get the Set of key values from that map?

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,221
    Rep Power
    12

    Default Re: Seeking ClientProperty Keys

    Not that I know of. The internal data structure is declared private. What exactly are you trying to do? There might be a different way.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    MGeorge's Avatar
    MGeorge is offline Aging Hacker
    Join Date
    Sep 2014
    Location
    East (San Francisco) Bay, California, USA
    Posts
    20
    Rep Power
    0

    Default Re: Seeking ClientProperty Keys

    My thanks to jim829 for the reply (even if it wasn't the answer I was hoping for). I solved my problem by attaching yet another property, a TreeSet to the component. That has introduced another "opportunity". I now have
    Java Code:
    TreeSet<String> list = new TreeSet<>();
    list.add(...);
    menuItem.putClientProperty("_list", list);
    In my event handler I retrieve the set using
    Java Code:
    TreeSet<String> list = (TreeSet<String>) menuItem.getClientProperty("_list");
    whiich results in
    Java Code:
    warning: [unchecked] unchecked cast
                            (TreeSet<String>) item.getClientProperty("_cp_keys");
      required: TreeSet<String>
      found:    Object
    I have also tried casting to "(TreeSet)" which changes the warning to "found: TreeSet".
    I would really like to make this warning go away (without suppressing it). Any help
    will be appreciated.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,221
    Rep Power
    12

    Default Re: Seeking ClientProperty Keys

    Try this.

    Java Code:
         JMenuItem m = new JMenuItem();
          Set<String> set = new HashSet<>();
          set.addAll(Arrays.asList("Item1", "Item2", "Item3"));
          m.putClientProperty("foo", set);
          Set<?> a = (Set<?>)m.getClientProperty("foo");
          System.out.println(a);
    A couple of points.

    1. Unless you need to maintain a sorted order, use HashSet instead of TreeSet. In your case it probably won't make a difference but the latter can incur a performance hit for large sets.
    2. Usually it's considered best practice to assign to the interface type rather than the implementation type. The exception would be if you need to use additional methods in the implementation that aren't in the interface.

    Regards,
    Jim
    Last edited by jim829; 08-02-2018 at 09:43 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    MGeorge's Avatar
    MGeorge is offline Aging Hacker
    Join Date
    Sep 2014
    Location
    East (San Francisco) Bay, California, USA
    Posts
    20
    Rep Power
    0

    Default Re: Seeking ClientProperty Keys

    Quote Originally Posted by jim829 View Post
    Try this.

    Java Code:
         JMenuItem m = new JMenuItem();
          Set<String> set = new HashSet<>();
          set.addAll(Arrays.asList("Item1", "Item2", "Item3"));
          m.putClientProperty("foo", set);
          Set<?> a = (Set<?>)m.getClientProperty("foo");
          System.out.println(a);
    A couple of points.

    1. Unless you need to maintain a sorted order, use HashSet instead of TreeSet. In your case it probably won't make a difference but the latter can incur a performance hit for large sets.
    2. Usually it's considered best practice to assign to the interface type rather than the implementation type. The exception would be if you need to use additional methods in the implementation that aren't in the interface.

    Regards,
    Jim
    Thanks for the comments. I used TreeSet because it's sorted. My test configuration has about fifteen entries in the set, not a huge number so I doubt the performance hit would be great. I suspect there shouldn't be more than maybe twice that number in a production configuration. It just makes it easier for humans who need to read the output. :-)

    TreeSet implements the Collection interface which permits the use of for-each. If I change to Set I would have to rework my code to use an Iterator instead.

    Those might not be "golden" reasons but they are the reason I did it the way I did.

    - Mike

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,221
    Rep Power
    12

    Default Re: Seeking ClientProperty Keys

    You may do it as you wish but just so you know, the Set interface extends Collection<E>. So the following works fine.

    Java Code:
         JMenuItem m = new JMenuItem();
          Set<String> set = new HashSet<>();
          set.addAll(Arrays.asList("Item1", "Item2", "Item3"));
          m.putClientProperty("foo", set);
          Set<?> a = (Set<?>)m.getClientProperty("foo");
          
          a.forEach(System.out::println);
    And on a different note: I also recommend you check the forum periodically after posting. Quite often someone may respond to your last statement with new info or to correct a misconception. Or they might have made a mistake and want to correct it. Plus it's part of forum etiquette. I recently answered a question or asked a follow-up of someone a while back and have yet to see a response. That can get frustrating.

    Regards,
    Jim
    Last edited by jim829; 08-03-2018 at 09:55 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Allow seeking through streaming mp3 file
    By nicocannon in forum Java Servlet
    Replies: 0
    Last Post: 08-30-2015, 07:17 AM
  2. Seeking Basic Java Tutor
    By jlorenzo in forum Jobs Offered
    Replies: 0
    Last Post: 06-05-2013, 08:33 PM
  3. Replies: 0
    Last Post: 05-12-2013, 10:51 AM
  4. Seeking Lucene Consultant
    By evedk in forum Jobs Offered
    Replies: 0
    Last Post: 07-28-2012, 07:21 PM
  5. seeking advice on using paintImmediately()
    By gib65 in forum AWT / Swing
    Replies: 1
    Last Post: 06-28-2010, 05:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •