2

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)

Martin Röbert
  • 664
  • 8
  • 25
  • Just tried this on Eclipse Indigo on Windows 7 (without applyBackground, just comment.setBackground) and text box showed a black background. What OS are you using? Also, don't forget to dispose of Color objects once you've finished with them. – Nick Wilson Apr 05 '12 at 09:04
  • I use OSX. I'll try it with Windows and Linux. – Martin Röbert Apr 05 '12 at 09:17
  • just offtopic: can you post the whole code? i'm interested in a dialog like this – Luiz E. Apr 09 '12 at 12:06
  • The code has the same effect on Windows 7 Pro with Java 7 - the text boxes have the same background color as the popup. – Martin Röbert Apr 10 '12 at 13:55

1 Answers1

2

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;
}
Frettman
  • 2,251
  • 1
  • 13
  • 9
  • This is my problem - there is no other code after that. As mentioned above it seems to work with Indigo and Windows 7. But one good news: border is displayed with `SWT.BORDER` – Martin Röbert Apr 05 '12 at 09:43
  • It works if the text field is a member variable and - as above suggested - I set the background color in createContents – Martin Röbert Apr 10 '12 at 14:12
  • Unfortunately if I set the backgroundcolor it also effects the foreground color - so I also have to set this. – Martin Röbert Apr 11 '12 at 09:20