Tuesday, 28 May 2013

Coming from many years of Java server side programming, having used a lot of java webapp frameworks, I want to refresh my Swing knowledge and also try a framework for Swing. After some research I had to recognize that Swing application frameworks seem to be inactive for quite some years (see http://en.wikipedia.org/wiki/Swing_Application_Framework). As I want to do just simple Swing applications (you could call "accessoires") I tend to not use full blown frameworks like NetBeans Platform or Eclipse RCP.

So I decided to give BSAF (Better Swing Application Framework) a chance.

Specification

Homepage: https://kenai.com/projects/bsaf/pages/Home
Version: 1.9.2
Sourcecode of examples: https://kenai.com/projects/bsaf/sources/main/show/examples/trunk

Usage

  • In NetBeans IDE create a new Maven Java application project:
    File - New Project... - Maven - Java Application
  • Add the BSAF library as dependency:
<dependency>
  <groupId>org.jdesktop.bsaf</groupId>
  <artifactId>bsaf</artifactId>
  <version>1.9.2</version>
</dependency>

Hello World

App.java:

import org.jdesktop.application.SingleFrameApplication;
import java.awt.Font;
import javax.swing.JLabel;

/**
* A trivial (Hello World) example of SingleFrameApplication. For simplicity's
* sake, this version doesn't have a resource file. SingleFrameExample2 is a
* little bit more realistic.
*
* @author Hans Muller (Hans.Muller@Sun.COM)
*/
public class App extends SingleFrameApplication {

  @Override
  protected void startup() {
    JLabel label = new JLabel("Hello World");
    label.setFont(new Font("LucidaSans", Font.PLAIN, 32));
    show(label);
  }

  public static void main(String[] args) {
    launch(App.class, args);
  }
}

Screenshot:

Hello World 2


In the "Hello World 2" example all label properties are externalized into a properties-file.

App.java:

import org.jdesktop.application.SingleFrameApplication;
import javax.swing.JLabel;

/**
* @author Hans Muller (Hans.Muller@Sun.COM)
*/
public class App extends SingleFrameApplication {

  @Override
  protected void startup() {
    JLabel label = new JLabel();
    label.setName("label");
    show(label);
  }

  public static void main(String[] args) {
    launch(App.class, args);
  }
}

App.properties (in src/main/resources/.../resources, see screenshot):

label.opaque = true
label.background = 0, 0, 0
label.foreground = 255, 255, 255
label.text = Hello World
label.font = Lucida-PLAIN-48
label.icon = earth.png

earth.png (besides App.properties):

Screenshot:

Hello World 3

In the "Hello World 3" example
  • all label properties and
  • the application window title are externalized into a properties-file,
  • an Exit-listener and
  • a confirmation dialog are added.

App.java:

import org.jdesktop.application.Application.ExitListener;
import org.jdesktop.application.SingleFrameApplication;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.util.EventObject;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
* A SingleFrameApplication example with an exitListener.
*
* @author Hans Muller (Hans.Muller@Sun.COM)
*/
public class App extends SingleFrameApplication {

  @Override
  protected void startup() {
    ExitListener maybeExit = new ExitListener() {
      public boolean canExit(EventObject e) {
        Object source = (e != null) ? e.getSource() : null;
        Component owner = (source instanceof Component) ? (Component) source : null;
        int option = JOptionPane.showConfirmDialog(owner, "Really Exit?");
        return option == JOptionPane.YES_OPTION;
      }

      public void willExit(EventObject e) {
      }
    };
    addExitListener(maybeExit);
    JButton button = new JButton();
    button.setName("button");
    button.setAction(new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
        exit();
      }
    });
    show(button);
  }

  public static void main(String[] args) {
    launch(App.class, args);
  }
}
App.properties:
mainFrame.title = ExitListener Demo
button.background = 255, 255, 255
button.foreground = 0, 0, 0
button.text = Click to Exit
button.font = Lucida-PLAIN-48
button.icon = exit.png

exit.png:

Screenshot:

...