Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Swing Components and application events

Thread view: 
JavaEnquirer - 27 Jan 2006 17:26 GMT
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.
Monique Y. Mudama - 27 Jan 2006 17:43 GMT
> I'm building an application whose Swing components should change in
> response to application generated events e.g. someone clicks change
[quoted text clipped - 8 lines]
>
> 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.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

JavaEnquirer - 27 Jan 2006 17:58 GMT
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.
Monique Y. Mudama - 27 Jan 2006 19:08 GMT
> 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.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.