【Processing】ファイル選択ダイアログ

Processingでファイル選択ダイアログを使用する方法をメモっておきます。 入力はselectInput()、出力はselectOutput()でダイアログを表示。ファイル選択後はコールバック関数が呼び出されます。

構文

selectInput(prompt, callback)
selectOutput(prompt, callback)

※ 詳しい設定はselectInputのリファレンスselectOutputのリファレンスを参照

パラメータ

prompt
プロンプト(windowsではダイアログのタイトルバーに表示される文字列)
callback
ファイル選択後に呼び出されるコールバック関数名

戻り値

ありません

コールバック関数構文

callback(File selection)

ダイアログをキャンセルしたり閉じたりした場合にはselectionはnull、選択された場合にはselection.getAbsolutePath()でフルパスが取得できます

ウィンドウをクリックするとファイル出力ダイアログが表示されます。

void setup()
{
  size(400,400); 
}

void draw()
{
}

void mousePressed()
{
  selectOutput("Select a file to process:", "fileSelected");
}

void fileSelected(File selection) 
{
  if (selection == null) 
  {
    println("Window was closed or the user hit cancel.");
  } 
  else
  {
    println("User selected " + selection.getAbsolutePath());
  }
}