Approveボタンに表示される文字列を設定する
広告
Approveボタンに表示される文字列を設定する方法を確認します。Approveボタンとは「ファイルを開く」ダイアログの場合は「開く」と書かれたボタンであり「ファイルを保存する」ダイアログの場合であれば「保存」と書かれたボタンです。
設定するにはJFileChooserクラスで用意されている「setApproveButtonText」メソッドを使います。
public void setApproveButtonText(String approveButtonText)
FileChooserUI の ApproveButton で使用されるテキストを設定します。 パラメータ: approveButtonText - ApproveButton で使用されるテキスト
引数にはApproveボタンに表示したい文字列を表すString型の値を設定します。
実際の使い方は次のようになります。
JFileChooser filechooser = new JFileChooser();
filechooser.setApproveButtonText("Open");
int selected = filechooser.showOpenDialog(this);
サンプルプログラム
では簡単なサンプルを作成して試してみます。
import javax.swing.*;
import java.io.File;
import java.awt.BorderLayout;
import java.awt.event.*;
public class JFileChooserTest15 extends JFrame implements ActionListener{
JLabel label;
public static void main(String[] args){
JFileChooserTest15 frame = new JFileChooserTest15();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(10, 10, 300, 200);
frame.setTitle("タイトル");
frame.setVisible(true);
}
JFileChooserTest15(){
JButton button = new JButton("file select");
button.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(button);
label = new JLabel();
JPanel labelPanel = new JPanel();
labelPanel.add(label);
getContentPane().add(labelPanel, BorderLayout.CENTER);
getContentPane().add(buttonPanel, BorderLayout.PAGE_END);
}
public void actionPerformed(ActionEvent e){
JFileChooser filechooser = new JFileChooser("c:\\temp");
filechooser.setApproveButtonText("ブラウザで開く");
int selected = filechooser.showOpenDialog(this);
if (selected == JFileChooser.APPROVE_OPTION){
File file = filechooser.getSelectedFile();
label.setText(file.getName());
}
}
}
上記をコンパイルした後で実行すると次のように表示されます。
ボタンをクリックするとダイアログが表示されます。今回は左側のボタンに表示される文字列を設定してあります。
( Written by Tatsuo Ikura )
著者 / TATSUO IKURA
これから IT 関連の知識を学ばれる方を対象に、色々な言語でのプログラミング方法や関連する技術、開発環境構築などに関する解説サイトを運営しています。