Skip to main content

Java Swing: Use of ToolTip, Change TitleBarIcon

How to use ToolTip in Java Swing

You can create a tool tip for any JComponent with setToolTipText() method. This method is used to set up a tool tip for the component.

For example, to add tool tip to PasswordField, you need to add only one line of code:

  1. field.setToolTipText("Enter your Password");  

Simple ToolTip Example

  1. import javax.swing.*;    
  2. public class ToolTipExample {  
  3.     public static void main(String[] args) {    
  4.      JFrame f=new JFrame("Password Field Example");    
  5.      //Creating PasswordField and label  
  6.      JPasswordField value = new JPasswordField();   
  7.      value.setBounds(100,100,100,30);    
  8.      value.setToolTipText("Enter your Password");  
  9.      JLabel l1=new JLabel("Password:");    
  10.      l1.setBounds(20,10080,30);    
  11.      //Adding components to frame  
  12.      f.add(value);  f.add(l1);  
  13.      f.setSize(300,300);    
  14.      f.setLayout(null);    
  15.      f.setVisible(true);      
  16. }  
  17. }  

Output:

Java How to use tool tip 1


How to change TitleBar icon in Java AWT and Swing

The setIconImage() method of Frame class is used to change the icon of Frame or Window. It changes the icon which is displayed at the left side of Frame or Window.

The Toolkit class is used to get instance of Image class in AWT and Swing.

Toolkit class is the abstract super class of every implementation in the Abstract Window Toolkit(AWT). Subclasses of Toolkit are used to bind various components. It inherits Object class.


Example to change TitleBar icon in Java AWT

  1. import java.awt.*;     
  2. class IconExample {     
  3. IconExample(){     
  4. Frame f=new Frame();     
  5. Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png");    
  6. f.setIconImage(icon);    
  7. f.setLayout(null);     
  8. f.setSize(400,400);     
  9. f.setVisible(true);     
  10. }     
  11. public static void main(String args[]){     
  12. new IconExample();     
  13. }     
  14. }    

Output:

Java How to change titlebar 1

Example to change TitleBar icon in Java Swing

  1. import javax.swing.*;  
  2. import java.awt.*;  
  3. class IconExample {   
  4. IconExample(){   
  5. JFrame f=new JFrame();   
  6. Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png");  
  7. f.setIconImage(icon);  
  8. f.setLayout(null);   
  9. f.setSize(200,200);   
  10. f.setVisible(true);   
  11. }   
  12. public static void main(String args[]){   
  13. new ToolkitExample();   
  14. }   
  15. }  

Output:

Java How to change titlebar 2

Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari

C++ this Pointer, static, struct and Enumeration

C++ this Pointer In C++ programming,  this  is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. It can be used  to pass current object as a parameter to another method. It can be used  to refer current class instance variable. It can be used  to declare indexers. C++ this Pointer Example Let's see the example of this keyword in C++ that refers to the fields of current class. #include <iostream>    using   namespace  std;   class  Employee {       public :           int  id;  //data member (also instance variable)               string name;  //data member(also instance variable)            float  salary;          Employee( int  id, string name,  float  salary)             {                   this ->id = id;                  this ->name = name;                  this ->salary = salary;            }             void  display()             {                 cout<<id<< "  " <<name<&