Martin Röbert
Martin Röbert

Reputation: 664

Background color of widgets in 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.

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?

Thanks in advance!

Text SWT.Multi on the left, SWT.SINGLE on the right side

EDIT: 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);
 }
}

The actual usage is as follows:

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();

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)

Upvotes: 2

Views: 2989

Answers (1)

Frettman
Frettman

Reputation: 2271

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.

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;
}

Upvotes: 2

Related Questions