Swing Components and application events

Discussion in 'Java' started by JavaEnquirer, Jan 27, 2006.

  1. JavaEnquirer

    JavaEnquirer Guest

    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.
    JavaEnquirer, Jan 27, 2006
    #1
    1. Advertising

  2. On 2006-01-27, JavaEnquirer penned:
    > 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.


    --
    monique

    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    Monique Y. Mudama, Jan 27, 2006
    #2
    1. Advertising

  3. JavaEnquirer

    JavaEnquirer Guest

    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.
    JavaEnquirer, Jan 27, 2006
    #3
  4. On 2006-01-27, JavaEnquirer penned:
    > 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.

    --
    monique

    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    Monique Y. Mudama, Jan 27, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mickey Segal
    Replies:
    0
    Views:
    867
    Mickey Segal
    Feb 2, 2004
  2. T E Schmitz
    Replies:
    3
    Views:
    414
  3. Bluetears76
    Replies:
    1
    Views:
    1,011
    Larry Barowski
    Jul 1, 2004
  4. mkrause
    Replies:
    0
    Views:
    669
    mkrause
    May 6, 2005
  5. Marcos
    Replies:
    0
    Views:
    669
    Marcos
    Jan 12, 2006
Loading...

Share This Page