Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8619945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:27:45+00:00 2026-06-12T06:27:45+00:00

I wanted to have a Text widget that could display a message in it

  • 0

I wanted to have a Text widget that could display a message in it when the user has not entered a value into the field yet. I extended composite and essentially wrapped a text field in it. Added a focus listener to remove the message on focus, and to replace the message when the focus is lost if the field is empty. That all works as expected.

The issue I am having is I wanted the prompt to be styled differently when it is placed in the text field. The font does not seem to be being used initially. Once the field has had focus and loses focus it looks correct.

For example this is how it looks when it initially loads:
Text is not styled correctly

And this is how it should look on initial load and how it looks after having and lost focus:
Text is styled correctly

It gets a little stranger, as when I run this inside a simple shell, it works how it should. When I run it as an Eclipse application is when it does not get styled correctly.

Here is the code for my composite:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;

/**
 * The <code>PromptingTextInput</code> component is a small enhancement to
 * standard <code>Text</code>. It adds the ability to specify a prompt value
 * that displays when the text is empty and the field does not have focus.
 */
public class PromptingTextInput extends Composite {

    private String prompt;
    private Text input;
    private boolean textEmpty;

    Font emptyFont;
    Font inputFont;

    public PromptingTextInput(String prompt, Composite parent, int style, boolean passwordField) {
        super(parent, style);

        this.prompt = prompt;
        setLayout(new FillLayout());

        this.textEmpty = true;
        this.input = new Text(this, (passwordField ? SWT.PASSWORD : SWT.NONE));
        setEmptyInputStyle();

        this.input.setText(this.prompt);
        this.input.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent e) {
                PromptingTextInput.this.focusGained();
            }

            public void focusLost(FocusEvent e) {
                PromptingTextInput.this.focusLost();
            }
        });
        addDisposeListener(new DisposeListener() {
             public void widgetDisposed(DisposeEvent e) {
                 disposeFonts();
             }
         });
    }   

    protected void focusGained() {
        if (this.textEmpty) {
            this.input.setText("");
            setInputStyle();
        }
    }

    protected void focusLost() {
        if (input.getText() == null || input.getText().trim().length() == 0) {
            this.input.setText(this.prompt);
            setEmptyInputStyle();
            this.textEmpty = true;
        } else {
            this.textEmpty = false;
        }
    }

    protected void setInputStyle() {    
        if (this.inputFont == null){
            this.inputFont = new Font(Display.getCurrent(), "Verdana", 8, SWT.DEFAULT);
        }
        this.input.setFont(this.inputFont);
        this.input.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
    }

    protected void setEmptyInputStyle() {   
        if (this.emptyFont == null){
            this.emptyFont = new Font(Display.getCurrent(), "Verdana", 6, SWT.ITALIC);
        }
        this.input.setFont(this.emptyFont);
        this.input.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
    }

    public String getPrompt() {
        return prompt;
    }

    public void setPrompt(String prompt) {
        this.prompt = prompt;
        if(!this.input.isFocusControl()){
            this.input.setText(this.prompt);
            setEmptyInputStyle();
        }
    }

    public Text getInput() {
        return input;
    }

    public boolean isTextEmpty() {
        return textEmpty;
    }

    public String getText() {
        return this.input.getText();
    }

    public void addModifyListener (ModifyListener listener) {
        this.input.addModifyListener(listener);
    }

    public void disposeFonts(){
        if (this.inputFont != null){
            this.inputFont.dispose();
        }
        if (this.emptyFont != null){
            this.emptyFont.dispose();
        }
    }   
}

UPDATE: As Baz has shown this is not an issue in Indigo and only seems to be an issue with an E4 app in Juno.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:27:46+00:00Added an answer on June 12, 2026 at 6:27 am

    Building upon sambi’s answer, I got the following working. Maybe this works in Juno:

    private static Font italic;
    
    public static void main(String[] args) {
        final Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1,false));
    
        italic = new Font(Display.getCurrent(), "Verdana", 6, SWT.ITALIC);
    
        final Text text = new Text(shell, SWT.BORDER);
    
        text.addListener(SWT.Paint, new Listener() {
    
            @Override
            public void handleEvent(Event event) {
    
                if(text.getText().length() < 1 && !text.isFocusControl())
                {
                    GC gc = event.gc;
                    gc.setFont(italic);
                    gc.setForeground(display.getSystemColor(SWT.COLOR_GRAY));
    
                    Point size = text.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    
                    /* Strangely the y positioning doesn't work correctly */
                    //gc.drawText("Please enter text", 1, (size.y / 2) - (italic.getFontData()[0].getHeight() / 2));
                    gc.drawText("Please enter text", 1, 4);
                }
            }
        });
    
        text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
    
        Button button = new Button(shell, SWT.PUSH);
        button.setText("Dummy");
    
        button.forceFocus();
    
        shell.setSize(200, 100);
        shell.open();
    
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        italic.dispose();
        display.dispose();
    }
    

    Without focus and empty text:

    enter image description here

    With focus or text:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file that has some invalid byte sequences. Emacs renders these
Imagine I wanted to have a single <span> with stuff inside that has its
I just wanted to ask. I have text input to allow users to type
I have a huge text file and I wanted to write a program which
I wanted to have a list of lambdas that act as sort of a
If I wanted to have a collection that described the (recursive) contents of a
I wanted to design a customized text selector that changed the text color when
I have a text area tag, wanted to write water mark functionality. So, i
I have several text files that have lots of newlines between texts that I
I have a text file that serves as a simple database, each entry spanning

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.