JFace PopupDialog 中小部件的背景颜色

2022-01-18 00:00:00 widget java eclipse-rcp swt jface

我想将 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 进行了子类化,因为我希望在光标位置旁边打开弹出窗口:

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 元素:代码不会像其他 JFace 对话框(例如 TitleAreaDialog)那样等待弹出窗口 open() 的完成

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.如果你很幸运,这会以某种方式导致你的问题.除此之外,仅从您的小片段就很难看出哪里出了问题.除非稍后有其他代码重置背景颜色,否则这应该可以工作.

更新:尝试覆盖 PopupDialog 的方法 getBackground() 并让它返回你想要的颜色.您的代码可能在 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;
}

相关文章