概要

ProgressMonitorを表示する進捗ダイアログのタイトル文字列をUIManagerを使用して変更します。

サンプルコード

String key = "ProgressMonitor.progressText";
JTextArea area = new JTextArea();
area.setEditable(false);
area.append(String.format("%s: %s%n", key, UIManager.getString(key)));

JTextField textField = new JTextField("Title Progress...");
JCheckBox check = new JCheckBox("Default");
check.addActionListener(e -> textField.setEnabled(
    !((JCheckBox) e.getSource()).isSelected()));

ProgressMonitor monitor = new ProgressMonitor(this, "message", "note", 0, 100);
JButton runButton = new JButton("run");
runButton.addActionListener(e -> {
  runButton.setEnabled(false);
  String title = check.isSelected() ? null : textField.getText();
  UIManager.put(key, title);
  monitor.setProgress(0);
  // ...
View in GitHub: Java, Kotlin

解説

  • ProgressMonitor#setProgress(...)が実行されて内部でJOptionPane#createDialog(...)で進捗ダイアログが作成されるときにUIManager.getString("ProgressMonitor.progressText")が実行されるので、その直前にJTextFieldから文字列を取得してUIManager.put("ProgressMonitor.progressText", title)で設定
  • UIManager.put("ProgressMonitor.progressText", null)が設定されている場合、UIManager.getString("ProgressMonitor.progressText")は現在のLocaleResourceBundleからProgressMonitor.progress.textAndMnemonicの値を参照して文字列を返す
    • JOptionPaneのタイトルバーにMnemonicは設定不可のためTextのみ適用される
// javax/swing/UIDefaults.java
public Object get(Object key, Locale l) {
  Object value = getFromHashtable(key);
  return value != null ? value : getFromResourceBundle(key, l);
}
// ...

/**
 * <code>TextAndMnemonicHashMap</code> stores swing resource strings. Many of strings
 * can have a mnemonic. For example:
 *   FileChooser.saveButton.textAndMnemonic=&Save
 * For this case method get returns "Save" for the key "FileChooser.saveButtonText" and
 * mnemonic "S" for the key "FileChooser.saveButtonMnemonic"
 *
 * There are several patterns for the text and mnemonic suffixes which are checked by the
 * <code>TextAndMnemonicHashMap</code> class.
 * Patterns which are converted to the xxx.textAndMnemonic key:
 * (xxxNameText, xxxNameMnemonic)
 * (xxxNameText, xxxMnemonic)
 * (xxx.nameText, xxx.mnemonic)
 * (xxxText, xxxMnemonic)
 *
 * These patterns can have a mnemonic index in format
 * (xxxDisplayedMnemonicIndex)
 *
 * Pattern which is converted to the xxx.titleAndMnemonic key:
 * (xxxTitle, xxxMnemonic)
 *
 */
private static class TextAndMnemonicHashMap extends HashMap<String, Object> {
  static final String AND_MNEMONIC = "AndMnemonic";
  static final String TITLE_SUFFIX = ".titleAndMnemonic";
  static final String TEXT_SUFFIX = ".textAndMnemonic";

  @Override public Object get(Object key) {
    // ...

参考リンク

コメント