d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Swt Custom Tooltip Delay
Add Reply New Topic New Poll
Member
Posts: 8,635
Joined: Dec 28 2007
Gold: 87.00
May 17 2012 01:39am
So I want to have a delay in my tooltip to close it when mouse exit, lets say 1 second example, and if you have returned in that 1 second to tooltipShell you dont close tooltip. Is this possible and how should I do it if it is?

Code

import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class Linkki {

private static Listener listener = new MouseEnterExitListener();
public static Shell shell;

 public static void main(String[] args) {
   Display display = new Display();
   shell = new Shell(display);
   Label label = new Label(shell, SWT.SHADOW_IN | SWT.CENTER);

   label.setText("Point your cursor here ...");
   label.setBounds(10, 10, 200, 15);

   label.addListener(SWT.MouseEnter, listener);
   label.addListener(SWT.MouseExit, listener);

   shell.setSize(260, 120);
   shell.open();

   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
 }

}

class MouseEnterExitListener implements Listener {
private static Shell tooltipShell;
private static Label tooltipLabel;
public void handleEvent(Event e) {
    switch (e.type) {
    case SWT.MouseEnter:
     if(tooltipShell!=null && !tooltipShell.isDisposed())
      tooltipShell.dispose();
     tooltipShell = new Shell(Linkki.shell, SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
        FillLayout layout = new FillLayout();
        layout.marginWidth = 5;
        tooltipShell.setLayout(layout);
        tooltipLabel = new Label(tooltipShell, SWT.NONE);
        tooltipLabel.setText("kek");
        tooltipShell.pack();
        PointerInfo a = MouseInfo.getPointerInfo();
  Point b = a.getLocation();
  int x = (int) b.getX();
  int y = (int) b.getY();
  tooltipShell.setLocation(x-10, y-10);
  tooltipShell.setVisible(true);
  tooltipShell.addListener(SWT.MouseExit, new Listener() {
        public void handleEvent(Event e) {
         tooltipShell.dispose();
          tooltipShell = null;
          tooltipLabel = null;
          }
        });
     break;
    }
}
}
Member
Posts: 4,554
Joined: Dec 1 2008
Gold: 0.50
May 17 2012 07:09am
I'm not sure if this helps, but there's a method Thread.sleep(long milliseconds) which adds a delay to a program. For example, if I want a 1 second delay before printing something I can put:

Code

 try {
  Thread.sleep(1000);
 } catch (InterruptedException e) {
  //do something
 }
 System.out.println("Delayed for one second!");
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Member
Posts: 8,635
Joined: Dec 28 2007
Gold: 87.00
May 17 2012 01:27pm
Quote (carteblanche @ 17 May 2012 15:11)


I'm not using the tooltipfunction, I made my own tooltip.

Got this working now one way, not the way I was looking for but it will do it just fine.
Member
Posts: 4,554
Joined: Dec 1 2008
Gold: 0.50
May 18 2012 07:57am
Quote (vittujenkevat @ May 17 2012 11:27am)
I'm not using the tooltipfunction, I made my own tooltip.

Got this working now one way, not the way I was looking for but it will do it just fine.


Pls share how. Might run into this in the future
Member
Posts: 8,635
Joined: Dec 28 2007
Gold: 87.00
May 18 2012 11:22am
Quote (Furio @ 18 May 2012 15:57)
Pls share how. Might run into this in the future


Your thread solution probably will work, but I couldnt bother to go with it :D I just added it so it will be shown untill user clicks anything as it will be fine in my application.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll