lakshman
lakshman

Reputation: 2741

setToolTipText is not showing the tool tip

I used this code in constructor of my class.

     public CheckComboBox()
     {   
       comboBox.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseEntered(MouseEvent mEvt) {
           comboBox.setToolTipText("lakshman");
            setToolTipText("laksman");
           System.out.println("lakshman");
        }
     });

when mouse entered happening, println statement prints to the console but other two tooltip methods are not showing relevant tool tips. here combobox is an instance field of swing combobox. what is the reason for settooltip method to not show the tooltip?

Upvotes: 3

Views: 7615

Answers (1)

Tech Nerd
Tech Nerd

Reputation: 832

this should be your class A contains main method

     public class A {
     public static void main(String args[]){

        form f=new form();
        f.setSize(300,300);
        f.setDefaultCloseOperation(form.EXIT_ON_CLOSE);
        f.setVisible(true);
         }

      }

and this should be your class of containing the components as:

public class form extends JFrame{

public form(){
    Panel p=new Panel();
    final JButton b=new JButton("button");

    p.add(b);
    this.add(p);


    b.addMouseListener(new MouseAdapter() {

            public void mouseEntered(MouseEvent mEvt) {
       b.setToolTipText("lakshman");

    }

});
}
}

the imports you must have to use in the class form are:

  import java.awt.Panel;
   import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
 import javax.swing.JButton;
 import javax.swing.JFrame;

Try now lakshman brother !

/////////////////!!!!!!!!!/////////////////////////////

(one more thing this the form class can be simplified as)

public class form extends JFrame{

public form(){
    Panel p=new Panel();
    final JButton b=new JButton("button");
    b.setToolTipText("HELLO");
    p.add(b);
    this.add(p);



}

}

there is no need (not compulsory) to use an actionListener to show the setToolTipText as it can be added just by calling with the reference of component object you want to show it with thanks

Upvotes: 1

Related Questions