Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to make labels selectable in SWT?

Is there any way to make labels selectable in SWT? or are there any other widgets in SWT that are selectable and can be used as labels? I am building a calendar.

like image 602
Baber Avatar asked Aug 05 '14 08:08

Baber


1 Answers

I think you want to select the Label text to clipboard.

You can use Text Widget as Label but caret will appear when user clicks on it. If you don't want caret then use StyledText and set caret to null.

Example:

package testplugin;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SWTHelloWorld {

public static void main (String [] args) {
    Display display = new Display ();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Label labelWidget = new Label(shell, SWT.NONE);
    labelWidget.setText("Hello");

    Text textWidget = new Text(shell, SWT.NONE);
    textWidget.setText("Hello");

    //Set background color same as its container
    textWidget.setBackground(shell.getBackground());
    //Make editable false
    textWidget.setEditable(false);

    StyledText styledTextWidget = new StyledText(shell, SWT.NONE);
    styledTextWidget.setText("Hello");  
    styledTextWidget.setBackground(shell.getBackground());
    styledTextWidget.setEditable(false);
    //Set caret null this will hide caret
    styledTextWidget.setCaret(null);

    shell.pack();
    shell.open ();
    shell.setSize(200, 300);
    while (!shell.isDisposed ()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();
}
}

You can refer these links to listen mouse,keyboard,focus etc etc listeners on various widgets.

  • Typed listeners

  • Un typed listeners

  • Introduction to SWT Widget

like image 108
Chandrayya G K Avatar answered Nov 04 '22 09:11

Chandrayya G K