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

Normalization in DBMS: 1NF, 2NF, 3NF and BCNF in Database

Normalization   is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly.  Anomalies in DBMS There are three types of anomalies that occur when the database is not normalized. These are – Insertion, update and deletion anomaly. Let’s take an example to understand this. Example : Suppose a manufacturing company stores the employee details in a table named employee that has four attributes: emp_id for storing employee’s id, emp_name for storing employee’s name, emp_address for storing employee’s address and emp_dept for storing the department details in which the employee works. At some point of time the table looks like this: emp_id emp_name emp_address emp_dept 101 Nikhil Kangra D001 101 Nikhil Kangra D002 123 Ashish Shimla D890 166 Rahul Pathankot D900 166 Rahul Pathankot D004 The above table is not normalized.  Update anomaly : In the above table we have two rows for employee Nikhil as he belongs ...