Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Setting Tool Tip Text for items in a JList Component

                         

In this section, you will learn how to set the tool tip text for items present in the JList component of the Java Swing. Tool Tip text is the help text of any component for user. When you rest the mouse cursor on the component then at that point a message which small font and yellow background stay there for few seconds. This text show the information about that component.

This program has used the tool tip text for items present in the JList component in Java Swing. In this program, you can add more and more items. You can enter the item name in the text box and click on the "Add" button. When you move the mouse pointer around the items in the list, it shows the specific item name as a tool tip text like the following image:

Tooltip Text for the item of the JList component of Swing

Following are some methods and APIs are explained as follows:

JScrollPane:
This is the class of javax.swing.*; package of Java Swing. This class is used to create scroll bar (Horizontal or Vertical) for any component. This program has used this for creating scroll bar for the text area. It creates scroll bar using it's constructor which holds the component name for which the scroll bar has to be created.

DefaultListModel:
This is the class of javax.swing.*; package of Java. This class is used to create a list model which is helpful for adding items for the list. This class has used own method to add items in the list.

locationToIndex():
This is the method of the MultiListUI class which is imported from the javax.swing.plaf.multi.*; package of Java. This method locate the item to the index where the mouse pointer points. This method takes a integer value for locating item from the list according to the given point.

getModel():
This is the method of JList class which holds the list of item which are shown in the JList component of Java Swing. It returns the list model.

getElementAt(index):
This is the method of DefaultListModel class which gets the item from the returned list model by getModel() method according to the given integer index no. as parameter.

addElement(String):
This is the method of DefaultListModel class which adds the item into the list.

Here is the code of the program:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TooltipTextOfList{
  private JScrollPane scrollpane = null;
  JList list;
  JTextField txtItem;
  DefaultListModel model;
  public static void main(String[] args){
    TooltipTextOfList tt = new TooltipTextOfList();
  }

  public TooltipTextOfList(){
    JFrame frame = new JFrame("Tooltip Text for List Item");
    String[] str_list = {"One""Two""Three""Four"};
    model = new DefaultListModel();
    for(int i = 0; i < str_list.length; i++)
      model.addElement(str_list[i]);
    list = new JList(model){
      public String getToolTipText(MouseEvent e) {
        int index = locationToIndex(e.getPoint());
        if (-< index) {
          String item = (String)getModel().getElementAt(index);
          return item;
        else {
          return null;
        }
      }
    };
    txtItem = new JTextField(10);
    JButton button = new JButton("Add");
    button.addActionListener(new MyAction());
    JPanel panel = new JPanel();
    panel.add(txtItem);
    panel.add(button);
    panel.add(list);
    frame.add(panel, BorderLayout.CENTER);
    frame.setSize(400400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public class MyAction extends MouseAdapter implements ActionListener{
    public void actionPerformed(ActionEvent ae){
      String data = txtItem.getText();
      if (data.equals(""))
        JOptionPane.showMessageDialog(null,"Please enter text in the Text Box.");
      else{
        model.addElement(data);
        JOptionPane.showMessageDialog(null,"Item added successfully.");
        txtItem.setText("");
      }
    }
  }
}

Download this example.

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

This Line:
String item = (String)getModel().getElementAt(index);
Should be written as:
String item = (String)list.getModel().getElementAt(index);

Posted by SammyB on Tuesday, 08.14.07 @ 19:50pm | #23382

Latest Searches:
span
Struts
netbeans jsp
Array in Action Form
Call on extensible RMI
error-Unknown entity:
timeline using javascr
Player Class in the Ja
sub classes
facelet
.getCurrencyInstance
examples
Combattons la programm
How to get data from d
is the jvm same for al
convert linked list to
count
Struts Configuration f
onetomany example in h
Bean class tag
phases of java program
Image with mouse drag
creating eclipse insta
b tree implementatio
get record using chara
ColdFusion Development
Combattons la programm
DFD
validating form using
Linux Caixa MаÐ??аÐ?
JSTL tutorials
Create JTree using XM
AND Operator
jdbc connections
CSS CSS Customize the
ะ ย ะà¸???ะÂ
net use
how to include jsf tag
IEEE Projects
mysql query
dwr reverse ajax
server dojo
Java Development Hiber
ะà???ะ ยà¸?ะà
PHP Randomizing Random
Hibernate Min() Functi
Photoshop Drawing 3D m
jdbc html jsp
login authentication
annotation in jee
How to read XML
excel style sheet
array to string
Photoshop Cut out imag
Facelet tutorial
Exercise - Pad Left
JSP:File Input Paramet
tables pdf itext
how will we set enviro
emailing file using js
MVC Architecture
form bean
howtoretrievedatabasei
weblogic 10.3
download tomcate 5.0
project for mobile pla
<html:options>
Ktoolbar TUTORIAL
drop box
rules
StringTokenizer specif
Date Examples java
basic about struts
Jtable Jbutton
sequence number genera
IBM MQ series
java is useful in netb
strating a thread in j
axis2 and ms exchange
Java example program t
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.