Swing/PersistenceService のバックアップの現在との差分(No.4)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/PersistenceService へ行く。
- 1 (2012-03-26 (月) 15:45:39)
- 2 (2012-04-17 (火) 16:56:49)
- 3 (2012-04-17 (火) 19:53:11)
- 4 (2012-06-15 (金) 21:15:15)
- 5 (2012-06-23 (土) 05:08:04)
- 6 (2012-11-16 (金) 18:50:43)
- 7 (2012-12-13 (木) 15:14:56)
- 8 (2012-12-26 (水) 11:30:04)
- 9 (2013-05-03 (金) 23:45:05)
- 10 (2013-05-07 (火) 05:48:15)
- 11 (2013-11-14 (木) 19:06:49)
- 12 (2014-10-08 (水) 18:35:33)
- 13 (2014-10-13 (月) 06:51:47)
- 14 (2014-10-17 (金) 15:04:11)
- 15 (2014-12-20 (土) 13:22:57)
- 16 (2015-02-25 (水) 17:36:09)
- 17 (2015-06-23 (火) 16:15:22)
- 18 (2015-11-26 (木) 04:34:21)
- 19 (2016-05-26 (木) 15:29:02)
- 20 (2016-05-31 (火) 15:12:52)
- 21 (2017-02-20 (月) 18:24:55)
- 22 (2018-01-02 (火) 21:42:35)
- 23 (2019-12-26 (木) 16:53:52)
- 24 (2021-07-02 (金) 17:55:08)
- 25 (2023-03-09 (木) 19:03:41)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:PersistenceServiceを使ってJFrameの位置・サイズを記憶 #navi(../) RIGHT:Posted by [[aterai]] at 2012-03-26 *PersistenceServiceを使ってJFrameの位置・サイズを記憶 [#da09cfeb] ServiceManagerからPersistenceServiceを取得し、JFrameなどの位置・サイズの保存、呼び出しを行います。 --- category: swing folder: PersistenceService title: PersistenceServiceを使ってJFrameの位置・サイズを記憶 tags: [ServiceManager, PersistenceService, JFrame, SwingWorker, XMLEncoder, XMLDecoder, Preferences] author: aterai pubdate: 2012-03-26T15:45:39+09:00 description: ServiceManagerからPersistenceServiceを取得し、JFrameなどの位置・サイズの保存、呼び出しを行います。 image: https://lh6.googleusercontent.com/-PyOW5DW5kHU/T3APD_Cq_bI/AAAAAAAABKk/i9eivMuQZ0Y/s800/PersistenceService.png --- * 概要 [#summary] `ServiceManager`から`PersistenceService`を取得し、`JFrame`などの位置・サイズの保存、呼び出しを行います。 -&jnlp; -&jar; -&zip; #download(https://lh6.googleusercontent.com/-PyOW5DW5kHU/T3APD_Cq_bI/AAAAAAAABKk/i9eivMuQZ0Y/s800/PersistenceService.png) //#screenshot #ref(https://lh6.googleusercontent.com/-PyOW5DW5kHU/T3APD_Cq_bI/AAAAAAAABKk/i9eivMuQZ0Y/s800/PersistenceService.png) * サンプルコード [#sourcecode] #code(link){{ class LoadSaveTask extends SwingWorker<WindowAdapter, Void> { private final WindowState windowState; **サンプルコード [#xdef909f] #code{{ final WindowState ws = new WindowState(); SwingWorker<WindowAdapter,Void> worker = new SwingWorker<WindowAdapter,Void>() { public LoadSaveTask(WindowState windowState) { super(); this.windowState = windowState; } @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(); try { bs = (BasicService) ServiceManager.lookup("javax.jnlp.BasicService"); ps = (PersistenceService) ServiceManager.lookup("javax.jnlp.PersistenceService"); } catch (UnavailableServiceException use) { ps = null; bs = null; } if(ps != null && bs != null) { if (ps == null || bs == null) { return null; } else { final PersistenceService persistenceService = ps; final URL codebase = bs.getCodeBase(); loadWindowState(persistenceService, codebase, ws); loadWindowState(persistenceService, codebase, windowState); 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()); JFrame f = (JFrame) e.getComponent(); if (f.getExtendedState() == Frame.NORMAL) { windowState.setSize(f.getSize()); windowState.setLocation(f.getLocationOnScreen()); } saveWindowState(persistenceService, codebase, ws); saveWindowState(persistenceService, codebase, windowState); } }; }else{ return null; } } @Override public void done() { WindowAdapter wa = null; try{ wa = get(); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch(Exception e) { e.printStackTrace(); private static void loadWindowState( PersistenceService ps, URL codebase, WindowState windowState) { try { FileContents fc = ps.get(codebase); try (XMLDecoder d = new XMLDecoder( new BufferedInputStream(fc.getInputStream()))) { @SuppressWarnings("unchecked") Map<String, Serializable> map = (Map<String, Serializable>) d.readObject(); windowState.setSize((Dimension) map.get("size")); windowState.setLocation((Point) map.get("location")); } } catch (IOException ex) { try { long size = ps.create(codebase, 64000); System.out.println("Cache created - size: " + size); } catch (IOException ioe) { ioe.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(); private static void saveWindowState( PersistenceService ps, URL codebase, WindowState windowState) { try { FileContents fc = ps.get(codebase); try (XMLEncoder e = new XMLEncoder( new BufferedOutputStream(fc.getOutputStream(true)))) { HashMap<String, Serializable> map = new HashMap<>(); map.put("size", (Serializable) windowState.getSize()); map.put("location", (Serializable) windowState.getLocation()); e.writeObject(map); e.flush(); } } catch (IOException ioe) { ioe.printStackTrace(); } } } }} **解説 [#p124cd93] 上記のサンプルでは、PersistenceServiceで確保したファイルに、HashMap<String, Serializable>に格納したJFrameの位置、サイズをXMLEncoder、XMLDecoderを使って読み書きしています。 * 解説 [#explanation] - `JFrame`の位置とサイズを`PersistenceService`で確保したファイルに`XMLEncoder`で保存、`XMLDecoder`でそのファイルから値を復元 - `Windows`環境では`C:\Users\(user)\AppData\LocalLow\Sun\Java\Deployment\cache\6.0\muffin`以下に次のような`muf`ファイルが作成される ---- - Windows 7, JDK 1.7.0_03 では、C:\Users\(user)\AppData\LocalLow\Sun\Java\Deployment\cache\6.0\muffin 以下に mufファイルなどが作成されるようです。 #code{{ <?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> <?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 が発生する - `XMLEncoder`で直接上記の`JFrame`を書きだす場合、以下のような`IllegalAccessException`が発生する java.lang.IllegalAccessException: Class sun.reflect.misc.Trampoline can not access a member of class MainPanel$2 with modifiers "" Continuing ... -- または、その WindowListener を public class にしておけば問題ない - 回避方法 -- `WindowListener`を実装する匿名内部クラスを`public class`(`Java Beans`)に変更する -- または、`removeWindowListener`で取り除く -- または、`XMLEncoder#setExceptionListener(...)`で、何もしない`ExceptionListener`を設定し、警告を捨ててしまう #code{{ import java.awt.*; import java.awt.event.*; import java.beans.*; import java.io.*; import java.nio.charset.StandardCharsets; import javax.swing.*; public class MainPanel extends JPanel{ 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(); try (Reader fr = new InputStreamReader( new FileInputStream(PROPERTIES_XML), StandardCharsets.UTF_8)) { textArea.read(fr, "File"); } catch (Exception ex) { ex.printStackTrace(); } add(new JScrollPane(textArea)); } public static void main(String[] args) { public static void main(String... args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { createAndShowGUI(); } }); } public static void createAndShowGUI() { try{ try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch(Exception e) { } 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(); try (XMLDecoder d = new XMLDecoder(new BufferedInputStream( new FileInputStream(PROPERTIES_XML)))) { frame = (JFrame) d.readObject(); // d.close(); } catch (Exception ex) { ex.printStackTrace(); } if(frame==null) { 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(); JFrame frame = (JFrame) e.getComponent(); frame.removeWindowListener(this); try{ File file = new File(PROPERTIES_XML); XMLEncoder xe = new XMLEncoder( new BufferedOutputStream(new FileOutputStream(file))); File file = new File(PROPERTIES_XML); try (XMLEncoder xe = new XMLEncoder(new BufferedOutputStream( new FileOutputStream(file)))) { xe.setExceptionListener(new ExceptionListener() { @Override public void exceptionThrown(Exception exception) { // XXX: exception.printStackTrace(); } }); xe.writeObject(frame); xe.flush(); xe.close(); }catch(Exception ex) { //e.printStackTrace(); // xe.flush(); xe.close(); } catch (Exception ex) { ex.printStackTrace(); } } }); return frame; } } }} **参考リンク [#m74e8dfc] * 参考リンク [#reference] - [http://pscode.org/jws/api.html JWS javax.jnlp.* API demos] - [http://docs.oracle.com/javase/jp/6/technotes/guides/javaws/developersguide/examples.html#PersistenceService PersistenceService] - [http://www.finnw.me.uk/persistencetest.html PersistenceService Demo] - [https://docs.oracle.com/javase/jp/8/docs/technotes/guides/javaws/developersguide/examples.html#PersistenceService PersistenceServiceサービスの使用] // - [http://www.finnw.me.uk/persistencetest.html PersistenceService Demo] - [[JFrameの位置・サイズを記憶する>Swing/Preferences]] - [[JTableのモデルをXMLファイルで保存、復元する>Swing/PersistenceDelegate]] **コメント [#x92d06e4] * コメント [#comment] #comment - `JScrollPane` + `JTable` + `DefaultTableModel`を`XMLEncoder`で`XML`書出しする場合のエラーなどについて: [http://wiki.byte-welt.net/wiki/JTable_speichern JTable speichern – Byte-Welt Wiki] -- &user(aterai); &new{2013-11-14 (木) 19:06:49}; java.lang.InstantiationException: javax.swing.plaf.basic.BasicTableHeaderUI$MouseInputHandler Continuing ... java.lang.Exception: XMLEncoder: discarding statement JTableHeader.removeMouseMotionListener(BasicTableHeaderUI$MouseInputHandler); Continuing ... java.lang.InstantiationException: javax.swing.plaf.basic.BasicTableUI$Handler Continuing ... java.lang.Exception: XMLEncoder: discarding statement JTable.removeMouseMotionListener(BasicTableUI$Handler); Continuing ... #comment