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:
- field.setToolTipText("Enter your Password");
Simple ToolTip Example
- import javax.swing.*;
- public class ToolTipExample {
- public static void main(String[] args) {
- JFrame f=new JFrame("Password Field Example");
-
- JPasswordField value = new JPasswordField();
- value.setBounds(100,100,100,30);
- value.setToolTipText("Enter your Password");
- JLabel l1=new JLabel("Password:");
- l1.setBounds(20,100, 80,30);
-
- f.add(value); f.add(l1);
- f.setSize(300,300);
- f.setLayout(null);
- f.setVisible(true);
- }
- }
Output:
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
- import java.awt.*;
- class IconExample {
- IconExample(){
- Frame f=new Frame();
- Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png");
- f.setIconImage(icon);
- f.setLayout(null);
- f.setSize(400,400);
- f.setVisible(true);
- }
- public static void main(String args[]){
- new IconExample();
- }
- }
Output:
Example to change TitleBar icon in Java Swing
- import javax.swing.*;
- import java.awt.*;
- class IconExample {
- IconExample(){
- JFrame f=new JFrame();
- Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png");
- f.setIconImage(icon);
- f.setLayout(null);
- f.setSize(200,200);
- f.setVisible(true);
- }
- public static void main(String args[]){
- new ToolkitExample();
- }
- }
Output:
Anurag Rana
Educator CSE/IT
Comments
Post a Comment