Swing/FileHistory のバックアップの現在との差分(No.10)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/FileHistory へ行く。
- 1 (2004-02-26 (木) 02:48:54)
- 2 (2004-06-02 (水) 09:53:39)
- 3 (2004-06-17 (木) 05:49:40)
- 4 (2004-08-31 (火) 12:49:35)
- 5 (2004-10-08 (金) 06:19:57)
- 6 (2004-11-04 (木) 10:05:38)
- 7 (2005-04-28 (木) 04:32:28)
- 8 (2005-11-03 (木) 16:28:44)
- 9 (2006-02-16 (木) 22:24:08)
- 10 (2006-02-27 (月) 15:53:41)
- 11 (2006-06-01 (木) 11:15:46)
- 12 (2006-06-01 (木) 12:56:59)
- 13 (2007-08-05 (日) 21:38:57)
- 14 (2013-02-26 (火) 14:49:11)
- 15 (2013-10-12 (土) 20:07:44)
- 16 (2015-01-20 (火) 15:41:32)
- 17 (2016-06-01 (水) 20:48:26)
- 18 (2017-09-09 (土) 21:17:05)
- 19 (2019-03-08 (金) 19:29:21)
- 20 (2020-12-22 (火) 15:44:59)
- 21 (2023-06-01 (木) 14:49:18)
- 22 (2024-02-02 (金) 11:46:37)
- 追加された行はこの色です。
- 削除された行はこの色です。
#navi(../) *JMenuにアイテムを追加 [#rfdbc890] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2003-10-07~ 更新日:&lastmod; --- category: swing folder: FileHistory title: JMenuに最近使ったファイルを追加 tags: [JMenu, JMenuBar, File] author: aterai pubdate: 2003-11-10 description: JMenuに「最近使ったファイル(F)」を履歴として追加していきます。 image: https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTMffElRbI/AAAAAAAAAZQ/1d47Mop5D-0/s800/FileHistory.png --- * 概要 [#summary] `JMenu`に「最近使ったファイル(`F`)」を履歴として追加していきます。 #contents #download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTMffElRbI/AAAAAAAAAZQ/1d47Mop5D-0/s800/FileHistory.png) **概要 [#z0413747] JMenuに、「最近使ったファイル(F)」を履歴として追加していきます。 * サンプルコード [#sourcecode] #code(link){{ private int MAXHISTORY = 3; private void updateHistory(String str) { fileHistoryMenu.removeAll(); fileHistoryCache.remove(str); fileHistoryCache.add(0, str); if (fileHistoryCache.size() > MAX_HISTORY) { fileHistoryCache.remove(fileHistoryCache.size() - 1); } for (int i = 0; i < fileHistoryCache.size(); i++) { String name = fileHistoryCache.get(i); String num = Integer.toString(i + 1); JMenuItem mi = new JMenuItem(new HistoryAction(name)); mi.setText(num + ": " + name); mi.setMnemonic((int) num.charAt(0)); fileHistoryMenu.add(mi, i); } } **サンプルコード [#u0f87931] private FileHistory fh; private JMenuItem noFile = new JMenuItem("なし"); private JMenu fileHistory; private void initHistory() { int hmax = 0; if(prefs.getBoolean("flag_history", true)) { hmax = prefs.getInt("history_size", 6); } fh = new FileHistory(prefs, "style", hmax); JMenu fm = getMenu("file"); if(fileHistory==null) { fileHistory = new JMenu("最近使ったファイル(F)"); fileHistory.setMnemonic('F'); JMenuItem exit = getMenuItem("exit"); fm.remove(exit); fm.add(fileHistory); fm.addSeparator(); fm.add(exit); }else{ fileHistory.removeAll(); } Vector vl = fh.getModel(); if(vl.size()<=0) { noFile.setEnabled(false); fileHistory.add(noFile); }else{ fm.remove(noFile); for(int i=0;i<vl.size();i++) { String name = (String)vl.elementAt(i); String num = Integer.toString(i+1); JMenuItem mi = new JMenuItem(new HistoryAction(new File(name))); mi.setText(num + ": "+ name); byte[] bt = num.getBytes(); mi.setMnemonic((int) bt[0]); fileHistory.add(mi); } } } private void updateHistory(String str) { fileHistory.removeAll(); Vector vl = fh.getModel(); for(int i=0;i<vl.size();i++) { String name = (String)vl.elementAt(i); String num = Integer.toString(i+1); JMenuItem mi = new JMenuItem(new HistoryAction(new File(name))); mi.setText(num + ": "+ name); byte[] bt = num.getBytes(); mi.setMnemonic((int) bt[0]); fileHistory.add(mi, i); } } class HistoryAction extends AbstractAction{ final private File file; public HistoryAction(File file_) { super(); file = file_; } public void actionPerformed(ActionEvent evt) { //現在編集しているファイルをセーブするかどうか //if(!isSaved()) { // Object[] obj = {"「"+getTitlePath()+"」は変更されています。", // "別のスタイルシートを開く前に保存しますか?"}; // java.awt.Toolkit.getDefaultToolkit().beep(); // int retValue = JOptionPane.showConfirmDialog((JPanel)null, // obj, "APP_NAME", JOptionPane.YES_NO_CANCEL_OPTION); // if(retValue!=JOptionPane.YES_OPTION) { // return; // } //} //以下、ファイルをロードするためのコード //Thread loader = new FileLoader(file); //loader.start(); } } class HistoryAction extends AbstractAction { private final String fileName; public HistoryAction(String fileName) { super(); this.fileName = fileName; } @Override public void actionPerformed(ActionEvent e) { Object[] obj = {"本来はファイルを開いたりする。\n", "このサンプルではなにもせずに\n", "履歴の先頭にファイルを移動する。"}; JComponent c = (JComponent) e.getSource(); JOptionPane.showMessageDialog(c.getRootPane(), obj, VersionAction.APP_NAME, JOptionPane.INFORMATION_MESSAGE); updateHistory(fileName); } } }} **解説 [#p7efb749] アプリケーションを再起動しても、これらのファイル履歴を残しておきたい場合は、Preferencesなどを使用する方法があります。 * 解説 [#explanation] 上記のサンプルでは「ファイル、開く」の`JMenuItem`をクリックするとファイルのオープン履歴を「最近使ったファイル(`F`)」の`JMenu`中に追加しています。 **参考リンク [#k3eb0d01] -[[JFrameの位置・サイズを記憶する>Swing/Preferences]] - 履歴を表示する`JMenuItem`は`3`件までに制限 - 履歴用の`JMenuItem`をメニューから選択するとそのファイルを履歴の先頭に移動(ファイルを開く処理などはない) -- 実際に使用する場合はファイルを使用している箇所を修正したり、アプリケーションを終了する際に履歴を保存したりするコードを追加する必要がある **コメント [#j906ef87] * 参考リンク [#reference] - [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JMenu.html JMenu (Java Platform SE 8)] - [[Resourceファイルからメニューバーを生成>Swing/ResourceMenuBar]] - [[JFrameの位置・サイズを記憶する>Swing/Preferences]] * コメント [#comment] #comment #comment