代表的なSwingコモンダイアログの使い方メモ
ファイル・ダイアログ (JFileChooser)
ファイルやディレクトリを開く/保存するコモンダイアログ
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("コモンダイアログのテスト");
// 親コンテナ
Container container = frame.getContentPane();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
frame.setContentPane(container);
// 1行目(テキストボックス)
JTextField text = new JTextField("テスト文字列", 20);
container.add(text);
// 2行目のボタンを表示する
JPanel panel_2 = new JPanel();
panel_2.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 5));
JButton button = new JButton("ファイルダイアログ");
panel_2.add(button);
container.add(panel_2);
// ボタンの動作設定
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser dialog = new JFileChooser();
dialog.setFileFilter(new FileNameExtensionFilter(
"HTMLファイル (*.html)", "html", "htm"));
dialog.setFileFilter(new FileNameExtensionFilter(
"テキストファイル (*.txt)", "txt"));
// dialog.setAcceptAllFileFilterUsed(false); // 「全てのファイル」を無効化
dialog.setDialogTitle("開くダイアログのテスト");
if (dialog.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
text.setText(dialog.getSelectedFile().getPath());
}
}
});
// 実行環境OSにあったUIを適用する
try {
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// SwingUtilities.updateComponentTreeUI(panel);
SwingUtilities.updateComponentTreeUI(frame);
} catch (Exception e) {
}
// メイン ダイアログの表示
frame.pack();
frame.setVisible(true);
}
「保存」ダイアログを表示するには
dialog.showOpenDialog(frame)
↓
dialog.showSaveDialog(frame)
ファイル/ディレクトリ の選択を制限するには、setFileSelectionModeメソッドの引数に、次の値をセットする。
JFileChooser.FILES_ONLY : ファイルのみ
JFileChooser.DIRECTORIES_ONLY : ディレクトリのみ
JFileChooser.FILES_AND_DIRECTORIES : ファイルとディレクトリの双方
dialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
隠しファイルを表示するには
dialog.setFileHidingEnabled(false);
ダイアログのタイトル文字列の変更は
dialog.setDialogTitle("開くダイアログのテスト");
ディレクトリのみ選択可能にして、保存ダイアログを表示すれば次のようになる。
色選択ダイアログ (JColorChooser)
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("基本コンポーネントのテスト");
// 親コンテナ
Container container = frame.getContentPane();
container.setLayout(new GridLayout(0, 1));
frame.setContentPane(container);
// ボタンを配置する
JPanel panel_2 = new JPanel();
panel_2.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 5));
JButton button = new JButton("色選択ダイアログ");
panel_2.add(button);
container.add(panel_2);
// ボタンの動作設定
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自動生成されたメソッド・スタブ
JColorChooser dialog = new JColorChooser();
Color color = new Color(220, 128, 128);
color = dialog.showDialog(frame, "色の選択", color);
if (color != null)
JOptionPane.showMessageDialog(
frame,
String.format("rgb=%d:%d:%d", color.getRed(),
color.getGreen(), color.getBlue()));
}
});
// 実行環境OSにあったUIを適用する
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(frame);
} catch (Exception e) {
}
// メイン ダイアログの表示
frame.pack();
frame.setVisible(true);
}


