1

Please check out this code. Why does the compiler shows a cannot find symbol error(cannot find symbol- method setToolTiptext(java.lang.String))?

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

public class JavaToolTipExample extends JFrame
{
    private Button b;
    public JavaToolTipExample()
    {
    super("Tool Tip");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setLayout(null);
    setVisible(true);
    b=new Button("Hover on me!");
    b.setToolTipText("Click!");
    add(b);
    event e=new event();
    b.addActionListener(e);
}
public class event implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        b.setText("Clicked");
    }
}
public static void main(String[]args)
 {
    JavaToolTipText gui=new JavaToolTipText();
 }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Advice: (a) Use a proper IDE, which creates the imports for you without using `*` (which is not recommended) for any missing class, and also gives you a choice of available methods when you start typing. (b) Check whether the method you are calling actually exists in the type of the variable. – RealSkeptic May 11 '17 at 06:58
  • 1
    1) `setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) `pack();` should be called after all the components have been added. 3) `setVisible(true);` should be called last. 4) `public class event` .. – Andrew Thompson May 11 '17 at 07:01
  • 1
    .. Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson May 11 '17 at 07:04

2 Answers2

2

Change:

private Button b;

&

b=new Button("Hover on me!");

To:

private JButton b;

&

b=new JButton("Hover on me!");

A JButton inherits the method from JComponent.setToolTipText(String), whereas Button is an AWT component.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

There is no setToolTipText method in java.awt.Button, which presumably is what you are using here. You can check the docs. It does not inherit that method from Component either.

I think what you meant is a javax.swing.JButton. Try to use that instead. Here is the docs for that if you want.

private JButton b; // This line
public JavaToolTipExample()
{
super("Tool Tip");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLayout(null);
setVisible(true);
b=new JButton("Hover on me!"); // And this line
b.setToolTipText("Click!");
add(b);
event e=new event();
b.addActionListener(e);
Sweeper
  • 213,210
  • 22
  • 193
  • 313