Solved

How to add a title and a context info to the textbox in SWT?

Posted on 2013-06-19
1
312 Views
Last Modified: 2013-06-21
Hi,
I have a GUI and in this GUI I have a toolbar and a textbox under the toolbar.

I would like to add a title (named "Files in List") to the textbox field. (I marked it in red in the attachment)

Also, I would like to put some note into the textbox in light gray color to give some information to the user about how to use the textbox. But this light gray text should disappear when you click on the textbox field. (I marked it in blue in the attachment)

This is the code I used to create the text box. I added this code after the creation of all the tabs.

        //This is the text box
        GridData txtGridData = new GridData(GridData.FILL_BOTH); txtGridData.horizontalSpan = 2;
        Text txt = new Text(compTab1, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
        txt.setLayoutData(txtGridData); 

Open in new window


I am sending a screen shot in the attachment to show the view.
text-box-title.png
0
Comment
Question by:Tolgar
1 Comment
 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
ID: 39264330
there is another widget called Group, it's used to implement your requirement. Just replace the code above with this one. Also add to your project attached file. To the text box is added FocusListener. When the text box gain the focus and the text is the one you want/description text/ it's cleared and foreground color is set to black. when focus is lost and there is nothig entered into the box the description text is set back and foreground color is set to gray.

GridLayout groupLayout = new GridLayout();
        GridData groupGD = new GridData(GridData.FILL_BOTH);
        Group groupText = new Group(compTab1, SWT.NONE);
        groupText.setLayout(groupLayout);
        groupText.setLayoutData(groupGD);
        groupText.setText("Files in List");
       
        GridData txtGridData = new GridData(GridData.FILL_BOTH);
        Text txt = new Text(groupText, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
        TxtFocusListener txtFL = new TxtFocusListener(compTab1);
        txt.setLayoutData(txtGridData);
        txt.addFocusListener(txtFL);
        txt.setText(TxtFocusListener.introText); txt.setForeground(compTab1.getDisplay().getSystemColor(SWT.COLOR_GRAY));
TxtFocusListener.java
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java Message handling in Service Layer 3 37
Image decoding from Camera 3 45
Java Timer (static) 9 15
Java Loop 4 14
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now