TITLE:PersistenceServiceを使ってJFrameの位置・サイズを記憶

Posted by aterai at 2012-03-26

PersistenceServiceを使ってJFrameの位置・サイズを記憶

ServiceManagerからPersistenceServiceを取得し、JFrameなどの位置・サイズの保存、呼び出しを行います。

  • &jnlp;
  • &jar;
  • &zip;
PersistenceService.png

サンプルコード

final WindowState ws = new WindowState();
SwingWorker<WindowAdapter,Void> worker = new SwingWorker<WindowAdapter,Void>() {
  @Override public WindowAdapter doInBackground() {
    PersistenceService ps;
    BasicService bs;
    try{
      bs = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService");
      ps = (PersistenceService)ServiceManager.lookup("javax.jnlp.PersistenceService");
    }catch(UnavailableServiceException use) {
      use.printStackTrace();
      ps = null;
      bs = null;
    }
    if(ps != null && bs != null) {
      final PersistenceService persistenceService = ps;
      final URL codebase = bs.getCodeBase();
      loadWindowState(persistenceService, codebase, ws);
      return new WindowAdapter() {
        @Override public void windowClosing(WindowEvent e) {
          JFrame f = (JFrame)e.getComponent();
          if(f.getExtendedState()==JFrame.NORMAL) {
            ws.setSize(f.getSize());
            ws.setLocation(f.getLocationOnScreen());
          }
          saveWindowState(persistenceService, codebase, ws);
        }
      };
    }else{
      return null;
    }
  }
  @Override public void done() {
    WindowAdapter wa = null;
    try{
      wa = get();
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch(Exception e) {
      e.printStackTrace();
    }
    JFrame frame = new JFrame();
    if(wa!=null) frame.addWindowListener(wa);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel());
    frame.setSize(ws.getSize());
    frame.setLocation(ws.getLocation());
    frame.setVisible(true);
  }
};
worker.execute();

解説

上記のサンプルでは、PersistenceServiceで確保したファイルに、HashMap<String, Serializable>に格納したJFrameの位置、サイズをXMLEncoder、XMLDecoderを使って読み書きしています。


  • Windows 7, JDK 1.7.0_03 では、C:\Users\(user)\AppData\LocalLow\Sun\Java\Deployment\cache\6.0\muffin 以下に mufファイルなどが作成されるようです。
<?xml version="1.0" encoding="UTF-8"?> 
<java version="1.7.0_03" class="java.beans.XMLDecoder"> 
 <object class="java.util.HashMap"> 
  <void method="put"> 
   <string>location</string> 
   <object idref="Point0"/> 
  </void> 
  <void method="put"> 
   <string>size</string> 
   <object idref="Dimension0"/> 
  </void> 
 </object> 
</java> 

  • XMLEncoder で直接 JFrame を書きだす場合は、removeWindowListener で、WindowListener を取り除いて置かないと、以下のような IllegalAccessException が発生する
    java.lang.IllegalAccessException: Class sun.reflect.misc.Trampoline can not access a member of class MainPanel$2 with modifiers ""
    Continuing ...
    • または、その WindowListener を public にする?
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import javax.swing.*;

public class MainPanel extends JPanel{
  private static final String PROPERTIES_XML = "properties.xml";
  public MainPanel() {
    super(new BorderLayout());
    JTextArea textArea = new JTextArea();
    try{
      textArea.read(new FileReader(PROPERTIES_XML), "File");
    }catch(Exception ex) {
      //ex.printStackTrace();
    }
    add(new JScrollPane(textArea));
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    try{
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch(Exception e) {
      e.printStackTrace();
    }
    JFrame frame = loadFrame();
    frame.setVisible(true);
  }
  private static JFrame loadFrame() {
    JFrame frame = null;
    try{
      XMLDecoder d = new XMLDecoder(new BufferedInputStream(
          MainPanel.class.getResourceAsStream(PROPERTIES_XML)));
      frame = (JFrame)d.readObject();
      d.close();
    }catch(Exception ex) {
      //ex.printStackTrace();
    }
    if(frame==null) {
      frame = new JFrame("title");
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.getContentPane().add(new MainPanel());
      frame.setSize(320, 240);
      frame.setLocationRelativeTo(null);
    }
    frame.addWindowListener(new WindowAdapter() {
      @Override public void windowClosing(WindowEvent e) {
        JFrame frame = (JFrame)e.getComponent();
        frame.removeWindowListener(this);
        try{
          File file = new File(PROPERTIES_XML);
          XMLEncoder xe = new XMLEncoder(
              new BufferedOutputStream(new FileOutputStream(file)));
          xe.writeObject(frame);
          xe.flush();
          xe.close();
        }catch(Exception ex) {
          //e.printStackTrace();
        }
      }
    });
    return frame;
  }
}

参考リンク

コメント