Swing/FileHistory のバックアップソース(No.16)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - 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)
--- title: JMenuに最近使ったファイルを追加 tags: [JMenu, File] author: aterai pubdate: 2003-11-10 description: JMenuに、「最近使ったファイル(F)」を履歴として追加していきます。 --- * 概要 [#a3e2a135] `JMenu`に、「最近使ったファイル(`F`)」を履歴として追加していきます。 #download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTMffElRbI/AAAAAAAAAZQ/1d47Mop5D-0/s800/FileHistory.png) * サンプルコード [#u0f87931] #code(link){{ private int MAXHISTORY = 3; private void updateHistory(String str) { fileHistory.removeAll(); fh.removeElement(str); fh.insertElementAt(str, 0); if(fh.size()>MAXHISTORY) fh.remove(fh.size()-1); for(int i=0;i<fh.size();i++) { String name = (String)fh.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(); this.file = file; } @Override public void actionPerformed(ActionEvent e) { historyActionPerformed(file); } } private void historyActionPerformed(File file) { Object[] obj = {"本来はファイルを開いたりする。\n", "このサンプルではなにもせずに\n", "履歴の先頭にファイルを移動する。"}; JOptionPane.showMessageDialog(this, obj, APP_NAME, JOptionPane.INFORMATION_MESSAGE); repaint(); updateHistory(file.getAbsolutePath()); } }} * 解説 [#p7efb749] 上記のサンプルでは、「ファイル->開く」で、ダミーファイルの履歴が残ります。履歴は`3`件まで残り、履歴をメニューから選択すると、そのファイルが履歴の先頭に移動します。 実際に使用する場合は、ダミーファイルを使用している箇所を修正したり、アプリケーションを終了する際に履歴を保存したりするコードを追加する必要があります。 * 参考リンク [#k3eb0d01] - [[Resourceファイルからメニューバーを生成>Swing/ResourceMenuBar]] - [[JFrameの位置・サイズを記憶する>Swing/Preferences]] * コメント [#j906ef87] #comment #comment