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


Groups > comp.lang.java.programmer > #13794

Re: swing component?

From Jan Burse <janburse@fastmail.fm>
Newsgroups comp.lang.java.programmer
Subject Re: swing component?
Date 2012-04-22 17:18 +0200
Organization albasani.net
Message-ID <jn17g2$agj$1@news.albasani.net> (permalink)
References <24010092.561.1335104359317.JavaMail.geo-discussion-forums@pbckz3> <jn175o$9u8$1@news.albasani.net>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web