swing component?

M

markspace

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?
 
J

Jan Burse

Peter said:
Hi All, does swing component provide something like:

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

thanks
from Peter ([email protected])

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
 
J

Jan Burse

Jan said:
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
 
M

markspace

"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.
 
P

Peter

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 :)
 
R

Roedy Green

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..
 
R

Roedy Green

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.
..
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

peter-swing 28
java swing engine 7
famous software that use swing? 5
how to bypass this swing limit 6
my swing theme 10
miglayout doesn't grow for jpanel 1
thread vs fork? 8
why maven auto import commons-lang 5

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top