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!

11 thoughts on “Swing Style Aspect

  1. Pingback: DamnHandy : Archive » Swing Style Aspect Part 2

  2. 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.

    Like

  3. 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.

    Like

  4. 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?

    Like

  5. 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?

    Like

  6. 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.

    Like

  7. 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.

    Like

  8. 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.

    Like

  9. 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.

    Like

Comments are closed.