4 Replies Latest reply on Apr 10, 2013 1:28 PM by thomas_behr

    ToolToopText on focusevent

      hi guyz...I m beginner in java. Is it possible to show tooltip on focus event? i mean when cursor is on field tooltip should display automatically .....not on mouse event. If possible, then plz plz can tell me code with simple example?

      It would be great help for me.

      Thank in advance.
        • 1. Re: ToolToopText on focusevent
          Yes, that is possible. Whenever the focus owner changes, display a popup with the desired tool tip for the new focus owner.

          One possible way to do this:
          import java.awt.EventQueue;
          import java.awt.GridBagConstraints;
          import java.awt.GridBagLayout;
          import java.awt.KeyboardFocusManager;
          import java.awt.Point;
          import java.beans.PropertyChangeEvent;
          import java.beans.PropertyChangeListener;
          import javax.swing.BorderFactory;
          import javax.swing.JComponent;
          import javax.swing.JFrame;
          import javax.swing.JLabel;
          import javax.swing.JPanel;
          import javax.swing.JTextField;
          import javax.swing.JToolTip;
          import javax.swing.Popup;
          import javax.swing.PopupFactory;
          import javax.swing.ToolTipManager;
          public class FocusToolTipTest {
            private FocusToolTipTest() {
            private void start() {
              // custom tool tip handling
              final KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
              kfm.addPropertyChangeListener(new PropertyChangeListener() {
                final MyToolTipManager ttm = new MyToolTipManager();
                public void propertyChange( final PropertyChangeEvent e ) {
                  if ("focusOwner".equals(e.getPropertyName())) {
                    final Object oldOwner = e.getOldValue();
                    if (oldOwner instanceof JComponent) {
                    final Object newOwner = e.getNewValue();
                    if (newOwner instanceof JComponent) {
                      final JComponent jc = (JComponent) newOwner;
              // disable normal tooltips
              // sample GUI
              final JPanel contentPane = new JPanel(new GridBagLayout());
              contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
              final GridBagConstraints gbc = new GridBagConstraints();
              gbc.fill = GridBagConstraints.NONE;
              gbc.gridx = 0;
              gbc.gridy = 0;
              gbc.weightx = 0;
              gbc.weighty = 0;
              contentPane.add(createLabel("Field 1:"), gbc);
              gbc.fill = GridBagConstraints.HORIZONTAL;
              gbc.gridx = 1;
              gbc.weightx = 1;
              contentPane.add(createTextField("A text field"), gbc);
              gbc.fill = GridBagConstraints.NONE;
              gbc.gridx = 0;
              gbc.gridy = 1;
              gbc.weightx = 0;
              contentPane.add(createLabel("Field 2:"), gbc);
              gbc.fill = GridBagConstraints.HORIZONTAL;
              gbc.gridx = 1;
              gbc.weightx = 1;
              contentPane.add(createTextField("Another text field"), gbc);
              final JFrame frame = new JFrame(FocusToolTipTest.class.getName());
            public static void main( String[] args ) {
              EventQueue.invokeLater(new Runnable() {
                public void run() {
                  (new FocusToolTipTest()).start();
            private static JLabel createLabel( final String text ) {
              final JLabel jl = new JLabel(text);
              jl.setToolTipText("A label to display information");
              return jl;
            private static JTextField createTextField( final String toolTipText ) {
              final JTextField jtf = new JTextField(50);
              return jtf;
            private static final class MyToolTipManager {
              Popup popup;
              void showTip( final JComponent component ) {
                final JToolTip tip = component.createToolTip();
                final PopupFactory factory = PopupFactory.getSharedInstance();
                final Point p = component.getLocationOnScreen();
                popup = factory.getPopup(component, tip, p.x, p.y + component.getHeight());
              void hideTip() {
                if (popup != null) {
                  popup = null;
          1 person found this helpful
          • 2. Re: ToolToopText on focusevent
            thanks a lot man...
            • 3. Re: ToolToopText on focusevent
              hi man,

              i have some doubt. Can we use this same code for java beans also ? actually now same functionality need to work in oracle form10g. There when we have focus on any field then tooltip should populate automatically. when i using code to convert to java beans, its not working.

              In oracle forms 10g, we are implementing java beans class. Java beans we are developing using Oracle Jdeveloper.
              • 4. Re: ToolToopText on focusevent
                Sorry, I'm not familiar with oracle form10g.