开发者

Problem with Component Resource Injection using Swing Application Framework

开发者 https://www.devze.com 2022-12-31 06:25 出处:网络
I\'m having a problem using the Swing Application Framework\'s Component Resource Injection, I read the tutorial provided by Sun and it kinda works.

I'm having a problem using the Swing Application Framework's Component Resource Injection, I read the tutorial provided by Sun and it kinda works.

The case is that I have a class Program that extends from the SingleFrameApplication provided by de SAF, now I want to get the text of my components (buttons and labels, etc.) from a properties file and it works as expected for that Program class, (I created a subdirectory called resources and put the Program.properties file in there). But, I have a MainFrame within a subpackage named mainpkg.gui (the Program is wi开发者_Python百科thin the mainpkg package) and I created the corresponding resources subsubpackage for placing the MainFrame.properties file, and it does not work! I cannot get the bundle (automatically, I guess if I use the ResourceMap.getXXX() method I'd get the stuff.

What am I doing wrong?

Thanks in advance!


I know this is an old question, but here's what I think is the problem:

First, if you use SingleFrameApplication, you shouldn't create your own JFrame. Instead, you should have a startup() method like this:

@Override
protected void startup() {
    final FrameView view = getMainView();
    view.setMenuBar(createMenuBar());
    view.setComponent(createMainComponent());
    show(view);
}

Now, the show(view) method takes care of injecting all those resources into the components. But it processes only the components that are in the component hierarchy of the view at the moment you call show(). If you add something later, you will have to inject the resources yourself. Here's an example of how you could do that:

public void injectResources(final Component root) {
    final ResourceMap resourceMap = applicationContext.getResourceMap(root
            .getClass(), Object.class);
    resourceMap.injectComponents(root);
    resourceMap.injectFields(root);
}

I hope this helps you or someone else with the same problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消