TooltipsList Demo


import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

public class tiplist extends JFrame {

public tiplist() {
super("ToolTip Example");

String[][] strs = { { "Item1", "HDD" },
{ "Item2", "Monitor" }, { "Item3", "Ram" },
{ "Item4", "DVD-Writer" }, { "Item5", "Microprocessor" } };

JList list = new JList(createItems(strs)) {
public String getToolTipText(MouseEvent e) {
int index = locationToIndex(e.getPoint());
if (-1 <>
ToolTipItem item = (ToolTipItem) getModel().getElementAt(
index);
return item.getToolTipText();
} else {
//return super.getToolTipText();
return null;
}
}
};
list.setToolTipText("");

getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);
}

Object[] createItems(String[][] strs) {
ToolTipItem[] items = new ToolTipItem[strs.length];
for (int i = 0; i <>
items[i] = new ToolTipItem(strs[i][0], strs[i][1]);
}
return items;
}

class ToolTipItem {
String obj;

String toolTipText;

public ToolTipItem(String obj, String text) {
this.obj = obj;
this.toolTipText = text;
}

public String getToolTipText() {
return toolTipText;
}

public String toString() {
return obj;
}
}

public static void main(String args[]) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {}

tiplist frame = new tiplist();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(140, 150);
frame.setVisible(true);
}
}

0 comments:

Post a Comment