swing component?

Discussion in 'Java' started by Peter Cheung, Apr 22, 2012.

  1. Peter Cheung

    Peter Cheung Guest

    Hi All, does swing component provide something like:

    component.setProperty("whatever", "value"); ??

    thanks
    from Peter ()
     
    Peter Cheung, Apr 22, 2012
    #1
    1. Advertising

  2. Peter Cheung

    markspace Guest

    On 4/22/2012 7:19 AM, Peter Cheung wrote:
    > Hi All, does swing component provide something like:
    >
    > component.setProperty("whatever", "value"); ??



    Not as far as I know. Components do support bound properties and
    property change listeners, but those are only for "internal" properties.
    You can't change the supported properties, those are fixed by design.

    What are you trying to do that you feel requires properties?
     
    markspace, Apr 22, 2012
    #2
    1. Advertising

  3. Peter Cheung

    Jan Burse Guest

    Peter Cheung schrieb:
    > Hi All, does swing component provide something like:
    >
    > component.setProperty("whatever", "value"); ??
    >
    > thanks
    > from Peter ()


    Hi,

    You can attach client data to a Swing component.
    The methods are:

    public class JComponent {

    /**
    * Adds an arbitrary key/value "client property" to this component.
    */
    public void putClientProperty(Object key, Object value);

    /**
    * Returns the value of the property with the specified key.
    */
    public Object getClientProperty(Object key);

    }


    But you must assure that the key does not clash
    with a key already used internally by Swing.
    Easiest is to use a key that resembles in the prefix
    one of your package names.

    For example:

    myTable.putClientProperty("com.mycompany.mypackage.mykey", "value");

    Bye
     
    Jan Burse, Apr 22, 2012
    #3
  4. Peter Cheung

    Jan Burse Guest

    Jan Burse schrieb:
    > Peter Cheung schrieb:
    >> Hi All, does swing component provide something like:
    >>
    >> component.setProperty("whatever", "value"); ??
    >>
    >> thanks
    >> from Peter ()

    >
    > Hi,
    >
    > You can attach client data to a Swing component.
    > The methods are:
    >
    > public class JComponent {
    >
    > /**
    > * Adds an arbitrary key/value "client property" to this component.
    > */
    > public void putClientProperty(Object key, Object value);
    >
    > /**
    > * Returns the value of the property with the specified key.
    > */
    > public Object getClientProperty(Object key);
    >
    > }
    >
    >
    > But you must assure that the key does not clash
    > with a key already used internally by Swing.
    > Easiest is to use a key that resembles in the prefix
    > one of your package names.
    >
    > For example:
    >
    > myTable.putClientProperty("com.mycompany.mypackage.mykey", "value");
    >
    > Bye
    >


    The javadoc says:

    "The clientProperty dictionary is not intended to support large scale
    extensions to JComponent nor should be it considered an alternative to
    subclassing when designing a new component."

    So an alternative route would be to subclass:

    class myTable extends JTable {
    myType myKey;
    }

    Advantage is a little bit more type safety. Since the new field
    has now a type. So the compiler can do his work when you assign
    values, and spot errors or do box/unboxing etc..

    But subclassing might not always be possible, for example when
    foreign code is used, and some static factory method
    irrevocable creates the GUI component. So client data is then
    a valid resort.

    Bye
     
    Jan Burse, Apr 22, 2012
    #4
  5. Peter Cheung

    markspace Guest

    On 4/22/2012 8:18 AM, Jan Burse wrote:

    >> myTable.putClientProperty("com.mycompany.mypackage.mykey", "value");
    >>

    > "The clientProperty dictionary is not intended to support large scale
    > extensions to JComponent nor should be it considered an alternative to
    > subclassing when designing a new component."



    Thanks to you and Quoll for pointing that out, I wasn't aware of that
    method. However, sub-classing is the last thing I'd want to do for the
    Swing API. The docs do say "large scale extensions," so as long as the
    OP only wants to store a few properties, it should be fine.

    OTOH, I've never needed to use this facility, so I really do wonder what
    Peter means to do. There's probably better ways of accomplishing his goal.
     
    markspace, Apr 22, 2012
    #5
  6. Peter Cheung

    Peter Guest

    On Apr 23, 1:10 am, markspace <-@.> wrote:
    > On 4/22/2012 8:18 AM, Jan Burse wrote:
    >
    > >> myTable.putClientProperty("com.mycompany.mypackage.mykey", "value");

    >
    > > "The clientProperty dictionary is not intended to support large scale
    > > extensions to JComponent nor should be it considered an alternative to
    > > subclassing when designing a new component."

    >
    > Thanks to you and Quoll for pointing that out, I wasn't aware of that
    > method.  However, sub-classing is the last thing I'd want to do for the
    > Swing API.  The docs do say "large scale extensions," so as long as the
    > OP only wants to store a few properties, it should be fine.
    >
    > OTOH, I've never needed to use this facility, so I really do wonder what
    > Peter means to do.  There's probably better ways of accomplishing his goal.


    Thank you all gentlemen :)
     
    Peter, Apr 24, 2012
    #6
  7. Peter Cheung

    Roedy Green Guest

    On Sun, 22 Apr 2012 07:19:19 -0700 (PDT), Peter Cheung
    <> wrote, quoted or indirectly quoted someone who said
    :

    >component.setProperty("whatever", "value"); ??


    there in one place you can hide information is setname
    You can also of course extend Swing compents with such a method.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    When you were a child, if you did your own experiment
    to see if it was better to put to cocoa into your cup first
    or the hot milk first, then you likely have the programmer gene..
     
    Roedy Green, Apr 24, 2012
    #7
  8. Peter Cheung

    Roedy Green Guest

    On Sun, 22 Apr 2012 17:18:26 +0200, Jan Burse <>
    wrote, quoted or indirectly quoted someone who said :

    >So an alternative route would be to subclass:
    >
    > class myTable extends JTable {
    > myType myKey;
    > }


    The problem with subclassing is you really want to insert the methods
    in JComponent. You have to add them independently to every Component
    you want to customise. About the only help Java gives you is the
    interface which can be used to make sure you don't forget some of the
    methods, and to let you get at the methods abstractly. Seems to me I
    remember reading back in the 90s it was possible in SmallTalk to
    insert extra methods in a base class.

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The population is aging, especially in Japan and
    there are not enough young people to take care of them. This will
    stimulate the evolution of caretaker robots, at first
    supervised and largely controlled remotely by human nurses.
    Over time they will work more and more independently
    doing tasks like bathing, feeding, cleaning up, dispensing
    medications and acting as companions by playing games
    and conversing. The technology can then be loosed on
    mankind's most vexing problem -- being sexually
    attracted to people who have no interest back.
    ..
     
    Roedy Green, Apr 27, 2012
    #8
    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. Big Daddy

    Java 1.2 Swing vs. Java 1.5 Swing

    Big Daddy, Apr 15, 2005, in forum: Java
    Replies:
    2
    Views:
    505
    Pete Barrett
    Apr 16, 2005
  2. mkrause
    Replies:
    0
    Views:
    676
    mkrause
    May 6, 2005
  3. lizard
    Replies:
    0
    Views:
    1,775
    lizard
    Jan 30, 2006
  4. S.T
    Replies:
    2
    Views:
    587
  5. Knute Johnson

    Swing is dead! Long live Swing.

    Knute Johnson, Feb 16, 2012, in forum: Java
    Replies:
    32
    Views:
    3,785
    Daniel Pitts
    Feb 29, 2012
Loading...

Share This Page