Swing Components and application events

J

JavaEnquirer

I'm building an application whose Swing components should change in
response to application generated events e.g. someone clicks change
font size. This can be done quite easily by registering each component
with an EventManager ( my class ). When the application event triggers,
the EventManager iterates through it's list of Swing components and
modifies them. This works fine. However, is there a better technique?
For one, I'm worried about garbage collection i.e. if the EventManager
maintains a reference to a Swing component then it will never get
garbage collected. Is there anyway of knowing/trapping when a JButton
say is about to get collected?

many thanks in advance.
 
M

Monique Y. Mudama

I'm building an application whose Swing components should change in
response to application generated events e.g. someone clicks change
font size. This can be done quite easily by registering each
component with an EventManager ( my class ). When the application
event triggers, the EventManager iterates through it's list of Swing
components and modifies them. This works fine. However, is there a
better technique? For one, I'm worried about garbage collection
i.e. if the EventManager maintains a reference to a Swing component
then it will never get garbage collected. Is there anyway of
knowing/trapping when a JButton say is about to get collected?

many thanks in advance.

Have you looked into creating a custom look and feel using properties
you've defined?

IE

public class MyLookAndFeel extends MetalLookAndFeel
{
protected void initComponentDefaults(UIDefaults table)
{
super.initComponentDefaults();
Object [] uiDefaults =
{
"my.big.font", new Font ("Arial, Font.PLAIN, 20))
//etc
}
table.putDefaults(uiDefaults);
}
}

Then when the user clicks to change font:

UIManager.setLookAndFeel (new MyLookAndFeel());

The trick is that all of your JComponents then need to override
updateUI(). In updateUI() you can load the fonts using something
like:

myJLabel.setFont(UIManager.getFont("my.big.font"));

Btw, for anyone reading this, I would like to know if this approach is
considered a big no-no, or if it's pretty reasonable. Obviously it's
a bit of a pain in the butt needing to override updateUI() all over
the place.
 
J

JavaEnquirer

Sounds like an interesting idea, thanks! I'm also interested in other
people's opinions on your suggestion. However, I will also need to
change the text on JLabels, JButtons etc if the user decides to change
the language. Another requirement is for dynamic language changing.
 
M

Monique Y. Mudama

Sounds like an interesting idea, thanks! I'm also interested in
other people's opinions on your suggestion. However, I will also
need to change the text on JLabels, JButtons etc if the user decides
to change the language. Another requirement is for dynamic language
changing.

You can actually put any arbitrary Object in a LnF, so in theory you
could also store Strings. But I don't think you would want a LnF
object for each permutation of language/font size/etc. I can think of
a way to do this by keeping track of a static language flag and using
a different LnF object for each language, but it feels like the wrong
approach.

By the way, I do know that the approach to changing "skins" works -- I
just don't know if it's considered an ugly hack.
 

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

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top