Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions? | Software Development
 

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.

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.

                         

» View all related tutorials
Related Tags: c com graphics text time parameters object io objects sed graph page vi value state parameter change int this parent

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

 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

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

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

Copyright © 2008. All rights reserved.