import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class ToolTipCtrl
{
private static boolean flag = true;
public static void main(String[] args)
{
JFrame frame = new JFrame();
final JButton btnSH = new JButton("Show or Hide");
final JButton btnS = new JButton("Show Only");
btnSH.setToolTipText(btnSH.getText());
btnS.setToolTipText(btnS.getText());
frame.add(btnSH, BorderLayout.WEST);
frame.add(btnS, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
btnSH.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(flag)
{
postToolTip(btnSH);
}
else
{
hideToolTip(btnSH);
}
flag = !flag;
}
});
btnS.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
postToolTip(btnS);
}
});
}
public static void postToolTip(JComponent comp)
{
Action action = comp.getActionMap().get("postTip");
if(action != null)
{
ActionEvent ae = new ActionEvent(comp, ActionEvent.ACTION_PERFORMED, "postTip", EventQueue.getMostRecentEventTime(), 0);
action.actionPerformed(ae);
}
}
public static void hideToolTip(JComponent comp)
{
Action action = comp.getActionMap().get("hideTip");
if(action != null)
{
ActionEvent ae = new ActionEvent(comp, ActionEvent.ACTION_PERFORMED, "hideTip", EventQueue.getMostRecentEventTime(), 0);
action.actionPerformed(ae);
}
}
}
11-15
11-15