小部件的背景颜色在JFace PopupDialog [英] Background color of widgets in JFace PopupDialog

查看:201
本文介绍了小部件的背景颜色在JFace PopupDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将JFace PopupDialog用作用户输入的轻量级对话框。但是我对文本小部件的背景颜色有一些问题。

I want to use the JFace PopupDialog as lightweight dialog for user input. But I have some problems with the background color of text widgets.

正如您在 1 ,SWT.MULTI文本小部件没有背景和边框,SWT.SINGLE文本小部件没有背景。
我试图用以下方式覆盖背景颜色:

As you can see below in 1, a SWT.MULTI text widget has no background and border, a SWT.SINGLE text widget has no background. I tried to override the background color with:

Text comment = new Text(composite, SWT.MULTI|SWT.BORDER);
comment.setFocus();
comment.setBackground(new Color(Display.getDefault(), new RGB(000, 000, 000)));
// method of PopupDialog
applyBackgroundColor(new Color(Display.getDefault(), new RGB(000, 000, 000)), comment);

有没有人知道如何正确处理这个问题?

Does anybody has any idea how to handle this properly?

提前致谢!

编辑:根据要求,这是弹出窗口的来源。我将PopupDialog子类化,因为我希望在Cursor位置旁边打开弹出窗口:

As requested, here is the source for the popup. I subclassed the PopupDialog, as I wanted the popup to be opened next to the Cursor location:

public class MouseLocationPopupDialog extends PopupDialog {
 private final static int SHELL_STYLE = PopupDialog.INFOPOPUP_SHELLSTYLE;

 public MouseLocationPopupDialog(Shell parent, String infoText) {
    this(parent, SHELL_STYLE, true, false, false, false, false, null, infoText);
 }

 public MouseLocationPopupDialog(Shell parent, String titleText, String infoText) {
     this(parent, SHELL_STYLE, true, false, false, false, false, titleText, infoText);
 }

 public MouseLocationPopupDialog(Shell parent, String infoText, final Point size) {
     this(parent, infoText);
     getShell().setSize(size);
 }

 public MouseLocationPopupDialog(Shell parent, int shellStyle, boolean takeFocusOnOpen, boolean persistSize, boolean persistLocation, boolean showDialogMenu, boolean showPersistActions, String titleText, String infoText) {
     super(parent, shellStyle, takeFocusOnOpen, persistSize, persistLocation, showDialogMenu, showPersistActions, titleText, infoText);
 }

 @Override
 protected void adjustBounds() {
     super.adjustBounds();
     Display d = Display.getCurrent();
     if (d == null) {
         d = Display.getDefault();
     }
     Point point = d.getCursorLocation();
     getShell().setLocation(point.x + 9, point.y + 14);
 }
}

实际用法如下:

final PopupDialog dialog = new MouseLocationPopupDialog(HandlerUtil.getActiveShell(event), "Title", "Bottom bar") {
@Override
protected Control createDialogArea(Composite parent) {
  Control composite = super.createDialogArea(parent);
  Composite table = new Composite((Composite) composite, SWT.NONE);
  table.setLayout(new GridLayout(2, true));
  // text is a member variable
  text = new Text(table, SWT.SINGLE | SWT.BORDER);
  Button submit = new Button(table, SWT.PUSH);
  return composite;
}

@Override
protected Control createContents(Composite parent) {
  Control contents = super.createContents(parent);

  final Color backgroundColor = new Color(Display.getCurrent(), new RGB(255, 255, 255));
  text.setBackground(backgroundColor);
  final Color foregroundColor = new Color(Display.getCurrent(), new RGB(0,0,0));
  text.setForeground(foregroundColor);
  backgroundColor.dispose();
  foregroundColor.dispose();

  return contents;
}
};
dialog.open();

请注意,此弹出窗口独立于其他UI元素:代码不会等待完成弹出窗口 open()像其他JFace对话框一样(例如TitleAreaDialog)

Note that this Popup is independent from other UI elements: The code will not wait for the completion of the popups open() like other JFace dialogs (e.g. TitleAreaDialog)

推荐答案

首先,使用 SWT.BORDER 而不是 SWT.BORDER_SOLID 如果你很幸运,这会以某种方式导致你的问题。除此之外,仅从你的小片段就很难看出出了什么问题。除非以后有其他代码重置背景颜色,否则这应该有效。

First of all, use SWT.BORDER instead of SWT.BORDER_SOLID. If you're lucky, this somehow causes your problem. Other than that, from your small snippet alone it's hard to see what goes wrong. Unless there is some other code that resets the background color later on, this should work.

更新
尝试覆盖 getBackground() PopupDialog 的方法,让它返回你想要的颜色。您的代码可能在 createDialogArea(..) PopupDialog 将此颜色应用于基本上所有之后你的代码。
如果您只想更改特定控件的背景颜色,可以尝试以下操作:

Update: Try to override the method getBackground() of PopupDialog and let it return the color you want. Your code probably is in createDialogArea(..) and PopupDialog applies this color to basically everything after your code. If you only want to change the background color of specific controls, you could try the following:

@Override
protected Control createContents(Composite parent) {
  Composite contents = super.createContents(parent);

  // set the color here

  return contents;
}

这篇关于小部件的背景颜色在JFace PopupDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆