- PR -

SWT-ボタンのキャプション色の変更

1
投稿者投稿内容
MOMO
会議室デビュー日: 2004/04/09
投稿数: 5
投稿日時: 2004-05-27 12:02
現在、SWTのGUI設計をやっているのですが、
ボタンの文字色って変更できないのでしょうか?
今までは、SWINGを使ってできていたので、SWT
でも変更可能かと思っていました。

もし色の変更ができない場合、みなさんはどう
対処されてますか?
Wata
ぬし
会議室デビュー日: 2003/05/17
投稿数: 279
投稿日時: 2004-05-28 10:57
もちろんできますよ。ControlクラスのsetForeground(Color)です。
MOMO
会議室デビュー日: 2004/04/09
投稿数: 5
投稿日時: 2004-05-28 11:33
ご回答ありがとうございます。
私の書き方が悪かったのですが、ボタンのスタイルをPUSHにしたときの場合です。

setForeground(Color)は指定したのですが、CHECKスタイルやRADIOスタイルの
時は、色の指定が適用されるのですが、PUSHスタイルの時は、色が変わってくれ
ません。

PUSHスタイルの時は、設定の方法が違うのでしょうか?
もう少し、リファレンスをあさってみようと思います。
前川
常連さん
会議室デビュー日: 2004/04/27
投稿数: 38
お住まい・勤務地: 1DK
投稿日時: 2004-05-28 11:35
引用:

Wataさんの書き込み (2004-05-28 10:57) より:
もちろんできますよ。ControlクラスのsetForeground(Color)です。


そのメソッドって、使用するときに注意点か何か有りますか?私もそのメソッドの事だろうと思って回答前に下記のコードで試したのですが、チェックボックスの前景色だけは変わり、トグルボタンや普通のボタンは変化がありませんでした。
コード:
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ButtonColor {
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());

		Button button1 = new Button(shell, SWT.PUSH);
		button1.setText("Button1");
		Button button2 = new Button(shell, SWT.TOGGLE);
		button2.setText("Button2");
		Button button3 = new Button(shell, SWT.CHECK);
		button3.setText("Button3");

		button3.setForeground(new Color(display, 0, 30, 190));
		button2.setForeground(new Color(display, 00, 30, 190));
		button1.setForeground(new Color(display, 0, 30, 100));

		shell.pack();
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}
}

Wata
ぬし
会議室デビュー日: 2003/05/17
投稿数: 279
投稿日時: 2004-05-28 13:40
引用:

MOMOさんの書き込み (2004-05-28 11:33) より:
setForeground(Color)は指定したのですが、CHECKスタイルやRADIOスタイルの
時は、色の指定が適用されるのですが、PUSHスタイルの時は、色が変わってくれ
ません。


うあ!思い込みでした。ごめんなさい。
でも、そんなのバグじゃないか!
と思って、Bugzillaをあったってみたら、
https://bugs.eclipse.org/bugs/show_bug.cgi?id=23837
がありました。

Windowsはダメなんだそうです…。 orz

無理やりやるなら、setImageで色付きテキストのイメージを張るとかしかないかも。
Wata
ぬし
会議室デビュー日: 2003/05/17
投稿数: 279
投稿日時: 2004-05-28 15:13
Wataです。嘘をついたお詫びに、setImageで色付きテキストのイメージを
Buttonに貼り付けるサンプルを載せます。
コード:
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.*;

public class ColorButtonText {
   private final Shell shell;

   public static void main(String[] args) {
      Display display = new Display();
      ColorButtonText main = new ColorButtonText(display);
      main.open();
      display.dispose();
   }

   public ColorButtonText(Display display) {
      shell = new Shell(display, SWT.TITLE | SWT.CLOSE);
      shell.setText("ColorButtonText");
      shell.setLayout(new RowLayout(SWT.HORIZONTAL));

       Button button1 = new Button(shell, SWT.PUSH | SWT.BOLD);
      setColorTextImage(button1,"Color &Text",
          new Color(shell.getDisplay(), 255, 0, 0));
      Button button2 = new Button(shell, SWT.PUSH);
      setColorTextImage(
         button2, "&Button2",
         new Color(shell.getDisplay(), 0, 0, 255));
      shell.pack();
   }

   private void open() {
      shell.open();
      while (!shell.isDisposed()) {
         if (!shell.getDisplay().readAndDispatch()) {
            shell.getDisplay().sleep();
         }
      }
   }

   public static void setColorTextImage(
      Button button, String text, Color color) {

      button.setForeground(color);
      final Image img = createTextImage(button, text);

      button.setText(text);
      button.setImage(img);
      button.addDisposeListener(new DisposeListener() {
         public void widgetDisposed(DisposeEvent e) {
            if (!img.isDisposed()) {
               img.dispose();
            }
         }
      });
   }

   private static Image createTextImage(Button button, String text) {
      GC buttonGC = new GC(button);
      Point size = buttonGC.textExtent(text, SWT.DRAW_MNEMONIC);
      Font font = buttonGC.getFont();
      buttonGC.dispose();

      Image img = new Image(button.getDisplay(), size.x, size.y);

      GC imgGC = new GC(img);
      imgGC.setFont(font);
      imgGC.setBackground(button.getBackground());
      imgGC.setForeground(button.getForeground());
      imgGC.drawText(text, 0, 0, SWT.DRAW_MNEMONIC);
      imgGC.dispose();

      return img;
   }
}


でも、これトグルボタンだと押された状態の背景色が合わないのでいまいちです。
MOMO
会議室デビュー日: 2004/04/09
投稿数: 5
投稿日時: 2004-05-28 15:22
引用:

Wataさんの書き込み (2004-05-28 13:40) より:
うあ!思い込みでした。ごめんなさい。
でも、そんなのバグじゃないか!
と思って、Bugzillaをあったってみたら、
https://bugs.eclipse.org/bugs/show_bug.cgi?id=23837
がありました。

Windowsはダメなんだそうです…。 orz

無理やりやるなら、setImageで色付きテキストのイメージを張るとかしかないかも。



BUG情報まで調べてもらい、ありがとうございます。
BUGなんですね・・、でもBUGということがはっきりしたので、助かりました。
setImageでやろうかな・・・いや、ユーザーを説得します。

初心者なので、本当に助かりました。
今後とも、よろしくお願いします。
MOMO
会議室デビュー日: 2004/04/09
投稿数: 5
投稿日時: 2004-05-28 15:28
引用:

Wataさんの書き込み (2004-05-28 15:13) より:
Wataです。嘘をついたお詫びに、setImageで色付きテキストのイメージを
Buttonに貼り付けるサンプルを載せます。


うわー助かります。
ユーザー説得挫折時に向け、勉強させてもらいます。

ほんとに、ありがとうございます!。
1

スキルアップ/キャリアアップ(JOB@IT)