Swing Style Aspect
Aspects are great way to to remove redundant code. I started tinkering with another aspect over the holiday because I was getting very tired of having manually style multiple JLabels with the same code over an over again. So instead of having to do something like this:
Font customFont = new Font("verdana",Font.BOLD,10);
Color mediumGray = new Color(172,172,172);
JLabel name1 = new JLabel("Name 1");
JLabel name2 = new JLabel("Name 2");
name1.setFont(font);
name1.setForeground(meduimGray);
name2.setFont(font);
name2.setForeground(meduimGray);
You could do something like this:
@Style(name="mediumGray")
JLabel name1 = new JLabel("Name 1");
@Style(name="mediumGray")
JLabel name2 = new JLabel("Name 2");
This ensures that the lables get styled with the same style when the lable values are set. This also ensures consistency accorss multiple forms too. While not the best example in ther world, I think it does get the point accros. There’s stil a lot more thought that needs to go into it such as:
- What type of format do you externalize the Styles? I’m not convinced CSS will work well with Swing.
- Loading and configuration of the externalized style definitions needs to be figured out
- Expand this far beyond JLabels
- The aspect depends on the JComonents to be a field of another class, they can’t be local variables
- See if AspectJ is better than JBoss AOP
I’ll be tweaking the code a bit more over the winter holidays and it will all become available in new Open Source project on Java.net. If you have any suggestions on the Style Aspect, please leave a comment or two!
December 3rd, 2005 at 1:49 am
Why do you need aspects for that?
JLabel createLabel(String text)
{
JLabel label = new JLabel(text);
label.setFont(new Font(”Verdana”, 10));
label.setBackground(new Color(172, 172, 172));
return label;
}
Seems easier.
December 3rd, 2005 at 2:51 am
You could use a factory pattern to do this:
JLabel name1 = FactoryJLabel.create(”Name 1″,”meduimGray”);
Aspects are not the answers to everything. Use it only when you cannot find an elegant solution in plain Java.
December 5th, 2005 at 2:46 am
I can’t see the benefit. What you do with aspect is what you usually do in a look and feel. Hence for me this is the wrong way. What is your motivation to solve this problem with aspects?
December 7th, 2005 at 12:08 am
Don’t see the benefit? This is one of the reasons why web interfaces are a lot quicker and easier to style than Swing ones.
December 7th, 2005 at 8:45 am
[...] Seems a few folks don’t get what my previous post about the Swing Style Aspect was all about, so I decied to answer a few of the comments in a post. First and foremost, this is merely a proof-of-concept and means of playing with AOP. Second, aspects provide another way of doing things and the results can certainly be achieved through other means. You don’t have to like or use AOP, but I do and I’m simply playing with idea here. [...]
December 13th, 2005 at 2:01 am
This is, at least to me, more annotation-like programming style, but not very Aspect :).