This discussion is archived
4 Replies Latest reply: Apr 10, 2013 6:28 AM by thomas.behr RSS

ToolToopText on focusevent

Chandan.Rattan Newbie
Currently Being Moderated
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
    thomas.behr Newbie
    Currently Being Moderated
    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) {
                ttm.hideTip();
              }
    
              final Object newOwner = e.getNewValue();
              if (newOwner instanceof JComponent) {
                final JComponent jc = (JComponent) newOwner;
                ttm.showTip(jc);
              }
            }
          }
        });
    
        // disable normal tooltips
        ToolTipManager.sharedInstance().setEnabled(false);
    
    
    
        // 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());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(contentPane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
    
      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);
        jtf.setToolTipText(toolTipText);
        return jtf;
      }
    
    
      private static final class MyToolTipManager {
        Popup popup;
    
        void showTip( final JComponent component ) {
          hideTip();
    
          final JToolTip tip = component.createToolTip();
          tip.setTipText(component.getToolTipText());
    
          final PopupFactory factory = PopupFactory.getSharedInstance();
          final Point p = component.getLocationOnScreen();
          popup = factory.getPopup(component, tip, p.x, p.y + component.getHeight());
          popup.show();
        }
    
        void hideTip() {
          if (popup != null) {
            popup.hide();
            popup = null;
          }
        }
      }
    }
  • 2. Re: ToolToopText on focusevent
    Chandan.Rattan Newbie
    Currently Being Moderated
    thanks a lot man...
  • 3. Re: ToolToopText on focusevent
    Chandan.Rattan Newbie
    Currently Being Moderated
    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
    thomas.behr Newbie
    Currently Being Moderated
    Sorry, I'm not familiar with oracle form10g.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points