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

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are...

Genetic Algorithm: Population, Fitness Function, Parent Selection, Cross over, Mutation

Genetic Algo Population Population is a subset of solutions in the current generation. It can also be defined as a set of chromosomes. There are several things to be kept in mind when dealing with GA population − The diversity of the population should be maintained otherwise it might lead to premature convergence. The population size should not be kept very large as it can cause a GA to slow down, while a smaller population might not be enough for a good mating pool. Therefore, an optimal population size needs to be decided by trial and error. The population is usually defined as a two dimensional array of –  size population, size x, chromosome size . Population Initialization There are two primary methods to initialize a population in a GA. They are − Random Initialization  − Populate the initial population with completely random solutions. Heuristic initialization  − Populate the initial population using a known heuristic for the problem. It has been observed that the e...

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...