I am looking for a method to add a kind of watermark to a SWT CTabFolder.
My goal is that the tab folder does not look as "boring" if there are no tabs present.
I am aware of the setBackgroundImage method of CTabFolder. Unfortunately, this seems to be non adjustable and can only display an image in "tiled" format.
Do you know of any way to add a centered image to an empty tab folder?
You'll have to add your own Listeners for SWT.Paint, and SWT.Resize. Then draw your Image on the GC. Here is an example:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
final Image image = new Image(null, "info.png");
final CTabFolder folder = new CTabFolder(shell, SWT.TOP);
folder.addListener(SWT.Paint, new Listener()
{
#Override
public void handleEvent(Event event)
{
if (image.isDisposed())
return;
Rectangle parentSize = folder.getBounds();
int tabHeight = folder.getTabHeight();
Rectangle imageSize = image.getBounds();
event.gc.drawImage(image, (parentSize.width - imageSize.width) / 2, (parentSize.height - imageSize.height + tabHeight) / 2);
}
});
folder.addListener(SWT.Resize, new Listener()
{
#Override
public void handleEvent(Event event)
{
folder.redraw();
}
});
CTabItem item = new CTabItem(folder, SWT.CLOSE);
item.setText("TEST");
Composite content = new Composite(folder, SWT.NONE);
content.setLayout(new FillLayout());
new Label(content, SWT.NONE).setText("bla");
item.setControl(content);
shell.pack();
shell.setSize(400, 200);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
image.dispose();
}
Looks like this when you have a CTabItem:
And like this when you close the item:
Related
I try to print a swt TreeViewer to png file. With:
Tree tree = treeViewer.getTree();
Image image = new Image(display, tree.getSize().x, tree.getParent().getSize().y);
GC gc = new GC(image);
System.out.println(new File(pathToSave).getAbsolutePath());
tree.print(gc);
ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] { image.getImageData() };
loader.save(pathToSave, SWT.IMAGE_PNG);
gc.dispose();
image.dispose();
the png contains only the visible part of the tree. The tree has a scrollbar because it contains more elements than fit on the form.
I would like to print the tree with all elements visible and without scrollbar. Any ideas?
On swing components i could use .paintall().. swt components don't seem to know that.
First, the size of the image should have the size the tree would have without scrolls, and not the current size. For that you should use computeSize(SWT.DEFAULT, SWT.DEFAULT, true). Then you should resize the tree that size, print, and then resize it back. Since you don't want users to notice that, during this operation you should disabled draws with setRedraw(false).
Here is a full snippet that does all this:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new FillLayout());
final Tree tree = new Tree(composite, SWT.NONE);
for (int i = 0; i < 100; i++) {
final TreeItem treeItem = new TreeItem(tree, SWT.NONE);
treeItem.setText(String.format("item %d long name", i));
}
tree.addListener(SWT.DefaultSelection, new Listener() {
#Override
public void handleEvent(Event event) {
tree.getParent().setRedraw(false);
final Point originalSize = tree.getSize();
final Point size = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
final Image image = new Image(display, size.x, size.y);
final GC gc = new GC(image);
tree.setSize(size);
tree.print(gc);
tree.setSize(originalSize);
final ImageLoader loader = new ImageLoader();
loader.data = new ImageData[]{image.getImageData()};
final String pathToSave = "out.png";
System.out.println(new File(pathToSave).getAbsolutePath());
loader.save(pathToSave, SWT.IMAGE_PNG);
gc.dispose();
image.dispose();
tree.getParent().setRedraw(true);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
Press enter to save the file.
I am implementing CTRL+F functionality for a view in my RCP application.( using SWT widgets)
For that, whenever I press CTRL+F, a small text box is popped up for typing and searching in the view.
But, if I dont enter anything or dont focus anything else, it remains popped up.
I want to display it for just 5 seconds of time.
So, please anyone can help?
Thanks in advance!
Adding code for more clarification :-
final Text findTextBox = new Text(viewer.getTable(), SWT.BORDER);
if ((((e.stateMask & SWT.CTRL) == SWT.CTRL) && (e.keyCode == 'f'))) {
Rectangle rect = viewer.getTable().getBounds();
findTextBox.setVisible(true);
findTextBox.setFocus();
findtextBox.setLocation(rect.x + rect.width -120, rect.y + rect.height - 25);
findTextBox.setSize(120, 25);
}
Here is some code that just utilizes the basic Java libraries and SWT:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Text text = new Text(shell, SWT.BORDER);
text.setVisible(false);
final Runnable timer = new Runnable()
{
public void run()
{
if (text.isDisposed())
return;
text.setVisible(true);
}
};
display.timerExec(5000, timer);
shell.pack();
shell.setSize(400, 200);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
I want to use a text field which will expand when the user enters some data and when the user is done editing and move out of text field than it should collapse. After that when ever user focus on the text field it should expand like a tooltip. Any pointer regarding this will help me.
This code should give you an idea of how to do it:
public static void main(String[] args)
{
Display display = Display.getDefault();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
final Text text = new Text(shell, SWT.BORDER | SWT.MULTI);
text.setText("edasdasdas\n\nasdasda\n\nasdasd");
final GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
data.heightHint = 100;
text.setLayoutData(data);
text.addListener(SWT.FocusIn, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
data.heightHint = 100;
shell.layout(true);
}
});
text.addListener(SWT.FocusOut, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
data.heightHint = 50;
shell.layout(true);
}
});
Button button = new Button(shell, SWT.PUSH);
button.setText("Button");
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
It basically changes the heightHint of the GridData when focus is lost/gained. Afterwards you need to re-layout the parent.
You will have to make some adjustments for the "focused height" and "unfocused height" values yourself.
Here are some screenshots:
With focus:
Without focus:
Just press the Button to lose focus.
Just add a FocusListener to your text field:
text.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
text.setSize(); // put in the size you want
}
#Override
public void focusLost(FocusEvent e) {
text.setSize(); // put in the size you want
}
});
Note that for setSize to work properly, parent of text can't have a layout.
I have an issue about the tray items display in Gnome Shell.
The item icon appears in the lower bar, and I'm fine with that, and just like the other ones, when moving the mouse over the icon, a text is displayed.
My problem is that this text cannot be changed: setting a text with .setText does not work, neither the class support any event, but Selected and MenuDetect, which detect the left and right click, respectively.
Has anybody experienced the same problem?
Thanks a lot in advance for your answers,
Gianluca
Not quite sure what problem you are facing, but this works for me:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Tray tray = display.getSystemTray();
TrayItem item;
if (tray != null) {
item = new TrayItem(tray, SWT.NONE);
item.setToolTipText("A");
item.setImage(display.getSystemImage(SWT.ICON_ERROR));
}
Button button = new Button(shell, SWT.PUSH);
button.setText("Change tray text");
button.addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event arg0) {
if(tray != null)
{
TrayItem item = tray.getItem(0);
item.setToolTipText(item.getToolTipText() + "A");
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
Note that I don't use setText(), but rather setToolTipText().
i am using eclipse rcp and swt for developing application.i have a list and i need to set its visibility false on a movement of title bar or when user click a titleBar. i am unable to find any event of titleBar. Is there any event of titleBar so that i can solve my problem? Or any thing that could probably solve my problem? i searched but could find for flex only as
function panelClick ( event:MouseEvent ) : void
{
trace( event.localX + '/' + event.localY );
}
is there same thing for swt using eclispe.
thanks in advance.
The following code will output "Move" and "Minimize" when the corresponding events happen:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Button dummy = new Button(shell, SWT.PUSH);
dummy.setText("Dummy");
shell.addListener(SWT.Move, new Listener() {
#Override
public void handleEvent(Event arg0) {
System.out.println("Move");
}
});
shell.addListener(SWT.Iconify, new Listener() {
#Override
public void handleEvent(Event arg0) {
System.out.println("Minimize");
}
});
shell.pack();
shell.setSize(400, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
However, the SWT.MOVE event is only fired after the shell has been moved, i.e. when the "move" is over.