• category: swing folder: FileHistory title: JMenuに最近使ったファイルを追加 tags: [JMenu, File] author: aterai pubdate: 2003-11-10 description: JMenuに、「最近使ったファイル(F)」を履歴として追加していきます。 image: https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTMffElRbI/AAAAAAAAAZQ/1d47Mop5D-0/s800/FileHistory.png

概要

JMenuに、「最近使ったファイル(F)」を履歴として追加していきます。

サンプルコード

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);
  }
}
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);
  }
}
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、「ファイル→開く」で、ダミーファイルの履歴が残ります。履歴は3件まで残り、履歴をメニューから選択すると、そのファイルが履歴の先頭に移動します。

実際に使用する場合は、ダミーファイルを使用している箇所を修正したり、アプリケーションを終了する際に履歴を保存したりするコードを追加する必要があります。

参考リンク

コメント