Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #13789 > unrolled thread

swing component?

Started byPeter Cheung <cmk128@gmail.com>
First post2012-04-22 07:19 -0700
Last post2012-04-24 08:52 -0700
Articles 9 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  swing component? Peter Cheung <cmk128@gmail.com> - 2012-04-22 07:19 -0700
    Re: swing component? markspace <-@.> - 2012-04-22 07:47 -0700
    Re: swing component? "Qu0ll" <Qu0llSixFour@gmail.com> - 2012-04-23 01:05 +1000
    Re: swing component? Jan Burse <janburse@fastmail.fm> - 2012-04-22 17:12 +0200
      Re: swing component? Jan Burse <janburse@fastmail.fm> - 2012-04-22 17:18 +0200
        Re: swing component? markspace <-@.> - 2012-04-22 10:10 -0700
          Re: swing component? Peter <mcheung63@gmail.com> - 2012-04-24 06:44 -0700
        Re: swing component? Roedy Green <see_website@mindprod.com.invalid> - 2012-04-27 05:06 -0700
    Re: swing component? Roedy Green <see_website@mindprod.com.invalid> - 2012-04-24 08:52 -0700

#13789 — swing component?

FromPeter Cheung <cmk128@gmail.com>
Date2012-04-22 07:19 -0700
Subjectswing component?
Message-ID<24010092.561.1335104359317.JavaMail.geo-discussion-forums@pbckz3>
Hi All, does swing component provide something like:

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

thanks
from Peter (cmk128@hotmail.com)

[toc] | [next] | [standalone]


#13790

Frommarkspace <-@.>
Date2012-04-22 07:47 -0700
Message-ID<jn15mj$i7m$1@dont-email.me>
In reply to#13789
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?

[toc] | [prev] | [next] | [standalone]


#13791

From"Qu0ll" <Qu0llSixFour@gmail.com>
Date2012-04-23 01:05 +1000
Message-ID<JeKdndW82aSsgwnSnZ2dnUVZ_v2dnZ2d@westnet.com.au>
In reply to#13789
"Peter Cheung"  wrote in message 
news:24010092.561.1335104359317.JavaMail.geo-discussion-forums@pbckz3...

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

Not exactly however JComponent has putClientProperty() and 
getClientProperty() which may be of interest.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
Qu0llSixFour@gmail.com
[Replace the "SixFour" with numbers to email me] 

[toc] | [prev] | [next] | [standalone]


#13792

FromJan Burse <janburse@fastmail.fm>
Date2012-04-22 17:12 +0200
Message-ID<jn175o$9u8$1@news.albasani.net>
In reply to#13789
Peter Cheung schrieb:
> Hi All, does swing component provide something like:
>
> component.setProperty("whatever", "value");  ??
>
> thanks
> from Peter (cmk128@hotmail.com)

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

[toc] | [prev] | [next] | [standalone]


#13794

FromJan Burse <janburse@fastmail.fm>
Date2012-04-22 17:18 +0200
Message-ID<jn17g2$agj$1@news.albasani.net>
In reply to#13792
Jan Burse schrieb:
> Peter Cheung schrieb:
>> Hi All, does swing component provide something like:
>>
>> component.setProperty("whatever", "value"); ??
>>
>> thanks
>> from Peter (cmk128@hotmail.com)
>
> 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

[toc] | [prev] | [next] | [standalone]


#13800

Frommarkspace <-@.>
Date2012-04-22 10:10 -0700
Message-ID<jn1e2d$7mn$1@dont-email.me>
In reply to#13794
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.

[toc] | [prev] | [next] | [standalone]


#13848

FromPeter <mcheung63@gmail.com>
Date2012-04-24 06:44 -0700
Message-ID<4b99164c-346d-4e76-a086-64fb69a3bde1@to5g2000pbc.googlegroups.com>
In reply to#13800
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 :-)

[toc] | [prev] | [next] | [standalone]


#13940

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-04-27 05:06 -0700
Message-ID<rm2lp7pqm55gvjepqtodirnb4pms3jbpm7@4ax.com>
In reply to#13794
On Sun, 22 Apr 2012 17:18:26 +0200, Jan Burse <janburse@fastmail.fm>
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.
.

[toc] | [prev] | [next] | [standalone]


#13853

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-04-24 08:52 -0700
Message-ID<j0jdp7hskie2ciopq7fc0ribh80ohkfsk1@4ax.com>
In reply to#13789
On Sun, 22 Apr 2012 07:19:19 -0700 (PDT), Peter Cheung
<cmk128@gmail.com> 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..

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web