TITLE:JMenuにアイテムを追加

JMenuにアイテムを追加

編集者:Terai Atsuhiro~

作成日:2003-10-07
更新日:2024-02-02 (金) 11:46:37
  • 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

概要

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

概要

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

サンプルコード

#spanend
#spanadd
private int MAXHISTORY = 3;
#spanend
#spanadd
private void updateHistory(String str) {
#spanend
  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);
  }
#spanadd
}
#spanend

#spandel
//#screenshot
#spanend
#spanadd
class HistoryAction extends AbstractAction {
#spanend
  private final String fileName;
  public HistoryAction(String fileName) {
    super();
    this.fileName = fileName;
  }

#spandel
**サンプルコード [#u0f87931]
#spanend
 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();
   }
 }
  @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);
  }
#spanadd
}
#spanend
#spanadd
View in GitHub: Java, Kotlin

解説

上記のサンプルでは「ファイル、開く」のJMenuItemをクリックするとファイルのオープン履歴を「最近使ったファイル(F)」のJMenu中に追加しています。

解説

アプリケーションを再起動しても、これらのファイル履歴を残しておきたい場合は、Preferencesなどを使用する方法があります。
  • 履歴を表示するJMenuItem3件までに制限
  • 履歴用のJMenuItemをメニューから選択するとそのファイルを履歴の先頭に移動(ファイルを開く処理などはない)
    • 実際に使用する場合はファイルを使用している箇所を修正したり、アプリケーションを終了する際に履歴を保存したりするコードを追加する必要がある

参考リンク

参考リンク

コメント

コメント