Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re enable ToolTips in Java after a JOptionPane.showMessageDialog has displayed

After a JOptionPane.showMessageDialog("Some Text"); has displayed, the tool tips are disabled, and I can't figure out how to re-enable them. Here's some code to demo the problem:

import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;

public class ToolTipError extends JPanel{ 
  JButton button;

  public ToolTipError() {
    button = new JButton("ToolTipHere"); 
    button.setToolTipText("This is Java! There is no help");

    button.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonActionPerformed(evt);
      }
    });

    add(button); 
  } 

  void buttonActionPerformed(ActionEvent env) {

    System.out.println("Button pushed."); //TEST

    JOptionPane.showMessageDialog(null, "This disables ToolTips!");

    // This does not re-enable the tool tip:
    button.setToolTipText("This is Java! There is no help");

    // Nor does this:
    ToolTipManager.sharedInstance().setEnabled(true);
 }

  void start() {
    //Create and set up the window.
   JFrame frame = new JFrame("ToolTipError");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    ToolTipError newContentPane = new ToolTipError();
    newContentPane.setOpaque(true);
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
  }

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

Note that setting the ToolTipText after displaying the dialog does not re-enable the ToolTips, nor does re-enabling them in the ToolTipManager.

Note that if you move the window around on the screen, the tool tips magically start working again!?!

The above code should compile "Out of the box" for you if you want to play around with it.

Thanks in advance!

like image 999
Boffin Avatar asked Mar 08 '26 12:03

Boffin


1 Answers

your error is simple typos in the code line

Frame frame = new JFrame("ToolTipError");

could be worked correctly if you'll use

JFrame frame = new JFrame("ToolTipError");

EDIT as mentioned works for me JDK6/7, WinXP/7

import java.awt.event.*;
import javax.swing.*;

public class ToolTipError extends JPanel {

    private static final long serialVersionUID = 1L;
    private JButton button;

    public ToolTipError() {
        button = new JButton("ToolTipHere");
        button.setToolTipText("Press the button !");
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                System.out.println("Button pushed.");
                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);

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

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

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ToolTipError tte = new ToolTipError();
                tte.start();
            }
        });
    }
}
like image 73
mKorbel Avatar answered Mar 11 '26 00:03

mKorbel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!