Java Swing How to - Re enable ToolTips in Java after a JOptionPane.showMessageDialog has displayed








Question

We would like to know how to re enable ToolTips in Java after a JOptionPane.showMessageDialog has displayed.

Answer

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ToolTipManager;
//from w  ww  .  j a v a2 s.  c o  m
public class Main extends JPanel {
  JButton button = new JButton("ToolTipHere");

  public Main() {    
    button.setToolTipText("Press the button !");
    button.addActionListener(e -> {
      JOptionPane.showMessageDialog(button.getParent(),
          "This disables ToolTips!");
      button.setToolTipText("This is Java! There is no help");
    });
    add(button);
  }

  void start() {
    ToolTipManager ttm = ToolTipManager.sharedInstance();
    ttm.setInitialDelay(0);
    ttm.setDismissDelay(10000);

    Main newContentPane = new Main();
    newContentPane.setOpaque(true);

    JFrame frame = new JFrame("Main");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(newContentPane);
    frame.pack();
    frame.setLocation(150, 150);
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    Main tte = new Main();
    tte.start();
  }
}