I want to draw a battery meter. For this I am painting my panel with Filled Rectangles using AWT Graphics class. Is it possible that I can add a tooltip so that whenver a mouse is pointed on the top of the rectangle a text is displayed(which will be the value of the battery)?

Hi,

You can add a mouse listener to your panel and use setToolTip() method for your Panel, like this:

// in JPanel constructor
// ...
   addMouseListener(new MouseAdapter() {	          	    	 
       public void mouseEntered(MouseEvent me){
    	   mouseEnteredHandler(me);
       }
   });
// ... constructor ends 
  // ... 
// private method
  private void mouseEnteredHandler(MouseEvent me) {	
		this.setToolTipText("value of the battery");
  }

of course you could update the tooltip text when the value of the battery is changed and remove the mouse listener ... and many other ways ... maybe :)

... if your panel is bigger then the size of your rectangles, you might need to find another component to add the tooltip

read more about using tooltips here

Hi,

You can add a mouse listener to your panel and use setToolTip() method for your Panel, like this:

// in JPanel constructor
// ...
   addMouseListener(new MouseAdapter() {	          	    	 
       public void mouseEntered(MouseEvent me){
    	   mouseEnteredHandler(me);
       }
   });
// ... constructor ends 
  // ... 
// private method
  private void mouseEnteredHandler(MouseEvent me) {	
		this.setToolTipText("value of the battery");
  }

of course you could update the tooltip text when the value of the battery is changed and remove the mouse listener ... and many other ways ... maybe :)

... if your panel is bigger then the size of your rectangles, you might need to find another component to add the tooltip

read more about using tooltips here

Thanks for a valuable suggestion. But as you said my panel is bigger than my rectangle. The panel contains so many other components. I only need that it should show the tooltip only when I enter the mouse on top of that rectangle. What other component can I use? Please suggest something. A component on which I can paint a rectangle. Then add tooltip to it at then place it at a specific location on a panel,i.e. using null Layout manager.

Another Panel with the size of the battery ?

and by Panel I mean JPanel, because the component on witch you place the tooltip must be a swing component not awt.

Another Panel with the size of the battery ?

and by Panel I mean JPanel, because the component on witch you place the tooltip must be a swing component not awt.

Well that worked. But now I have a diff problem due to this. I am having different panels below my battery panel(inseterted into tabs). So when I dont make those panels visible my tooltip shows.But when I make them visible there is no tooltip. Is it becuase of overlapping panels. I have tried setting opaque property of other panels as false but still tooltip doesnt show

This article has been dead for over six months. Start a new discussion instead.