進捗度合いとして表示される文字列を設定する

広告

進捗バーで「setStringPainted」メソッドを使って進捗度合いを表示させた時、はデフォルトでは進捗度合いを示す値をパーセンテージで表示します。ここではパーセンテージ表示の代わりに別の文字列を表示する方法を確認します。設定するにはJProgressBarクラスで用意されている「setString」メソッドを使います。

進捗文字列の値を設定します。デフォルトでは、この文字列は null で、単純な
パーセンテージを表す文字列が返されます (組み込みの動作)。独自の進捗文字
列を指定したあとで、組み込みの動作に戻すには、文字列を null に設定し直し
ます。

進捗文字列は、isStringPainted が true を返す場合にだけペイントされます。

パラメータ:
  s - 進捗文字列の値

引数に進捗度合いとして表示される文字列をString型の値で指定します。

実際の使い方は次のようになります。

JProgressBar bar = new JProgressBar();
bar.setStringPainted(true);
bar.setString("処理中です");

サンプルプログラム

では簡単なサンプルを作成して試してみます。

JProgressBarTest7.java

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;

public class JProgressBarTest7 extends JFrame implements ActionListener{

  Timer timer;
  JButton startButton;
  JButton stopButton;
  JProgressBar bar;
  JLabel label;
  int count;

  public static void main(String[] args){
    JProgressBarTest7 frame = new JProgressBarTest7();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(10, 10, 300, 200);
    frame.setTitle("タイトル");
    frame.setVisible(true);
  }

  JProgressBarTest7(){
    count = 0;
    label = new JLabel("Not Start");

    JPanel labelPanel = new JPanel();
    labelPanel.add(label);

    startButton = new JButton("start");
    startButton.addActionListener(this);
    startButton.setActionCommand("start");

    stopButton = new JButton("stop");
    stopButton.addActionListener(this);
    stopButton.setActionCommand("stop");
    stopButton.setEnabled(false);

    JPanel buttonPanel = new JPanel();
    buttonPanel.add(startButton);
    buttonPanel.add(stopButton);

    bar = new JProgressBar(0, 200);
    bar.setStringPainted(true);
    bar.setString("");
    bar.setValue(0);

    JPanel barPanel = new JPanel();
    barPanel.add(bar);

    timer = new Timer(100 , this);
    timer.setActionCommand("timer");

    getContentPane().add(labelPanel, BorderLayout.PAGE_START);
    getContentPane().add(buttonPanel, BorderLayout.PAGE_END);
    getContentPane().add(barPanel, BorderLayout.CENTER);
  }

  public void actionPerformed(ActionEvent e){
    String cmd = e.getActionCommand();

    if (cmd.equals("start")){
      startButton.setEnabled(false);
      stopButton.setEnabled(true);

      timer.start();
    }else if (cmd.equals("stop")){
      startButton.setEnabled(true);
      stopButton.setEnabled(false);

      timer.stop();
    }else if (cmd.equals("timer")){
      label.setText(count + " count");
      bar.setString("処理中です");

      if (count >= 200){
        startButton.setEnabled(true);
        stopButton.setEnabled(false);

        timer.stop();

        bar.setValue(count);
        bar.setString("完了しました");

        count = 0;
      }else{
        count++;
        bar.setValue(count);
      }
    }
  }
}

上記をコンパイルした後で実行すると次のように表示されます。

JProgressBarで進捗度合いを表す文字列を設定する

今回は実行前、実行中、終了後で表示する文字列を設定しています。

JProgressBarで進捗度合いを表す文字列を設定する

JProgressBarで進捗度合いを表す文字列を設定する

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。