ProgressMonitorを表示する進捗ダイアログのタイトルを変更する
Total: 628
, Today: 2
, Yesterday: 1
Posted by aterai at
Last-modified:
概要
ProgressMonitor
を表示する進捗ダイアログのタイトル文字列をUIManager
を使用して変更します。
Screenshot
Advertisement
サンプルコード
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")
は現在のLocale
のResourceBundle
から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) {
// ...
参考リンク
- ProgressMonitorで処理の進捗を表示
- jdk8u-dev/jdk/src/share/classes/javax/swing/UIDefaults.java ・ openjdk/jdk8u-dev ・ GitHub