Show Tooltip for different position in Java

Description

The following code shows how to show Tooltip for different position.

Example


  //  w w  w .j av  a 2 s .c  o m
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main{
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Hello, World") {
      public boolean contains(int x, int y) {
        if (x < 50) {
          setToolTipText("50 java2s.com");
        } else {
          setToolTipText("java2s.com");
        }
        return super.contains(x, y);
      }
    };
    frame.add(button, "Center");
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

The code above generates the following result.

Show Tooltip for different position in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer